not sure what I changed

master
Tait Hoyem 2 years ago
parent 1290d876cf
commit ca770be68f

3
.gitignore vendored

@ -56,3 +56,6 @@ build-iPhoneSimulator/
# .rubocop-https?--*
.jekyll-cache
# Seperate private repository for work
/transcriptions/

@ -12,8 +12,6 @@ gem "kramdown-math-katex"
gem "execjs"
gem "duktape"
gem "addressable", "~> 2.8"
gem "ffi", "~> 1.15"
gem "webrick", "~> 1.7"
gem "ffi"
group :jekyll_plugins do

@ -2,6 +2,7 @@ plugins:
- jekyll-katex
- jekyll-minifier
- kramdown-math-katex
- jekyll-katex
highlihger: rouge

@ -1,7 +1,7 @@
- company: "Zone4 Systems Inc."
position: "Software Developer"
years: "June 2021-present"
description: "Software development and QA (testing) for an international race timing company based in Canmore, AB. Testing using unittest and Selenium. Development done in Javascript and Tornado. Zone4 was founded in 2001 by Canadian Olympian Dan Roycroft.."
description: "Software development and QA (testing) for an international race timing company based in Canmore, AB. Testing using unittest and Selenium. Development done in Javascript and Tornado. Zone4 was founded in 2001 by Canadian Olympian Dan Roycroft."
- company: "Bytetools Technologies Inc."
position: "Founder"
years: "2020-present"

@ -1,30 +1,24 @@
- name: "Quote Retrieval System"
link: "https://github.com/TTWNO/quote_pdf"
description: "Written for a local roofing business to securely distribute quotes."
- name: "Yggdrasil Screen Reader"
- name: "Odilia Screen Reader"
link: "https://yggdrasil-sr.github.io/"
description: "Some small pieces of code I've written have made it into a new screen reader for Linux, written in Rust."
description: "Significant contributor to a new screen reader for Linux, written in the Rust programming language."
- name: "Programming Tutorials For The Visually Impaired"
link: "/emacspeak-tutorials/"
description: "Amateur-level production quality videos with all file buffers, written text, and shell commands read out by <a href=\"http://emacspeak.sourceforge.net/\">Emacspeak</a>, an Emacs extention for the blind."
- name: "Simple Markdown Editor"
link: "https://tait.tech/md/"
description: "A simple online (and offline) browser-based markdown editor that supports the automatic creation of accessible math (MathML)."
- name: "Lichess"
link: "https://github.com/ornicar/lila/pull/7772"
description: "A patch for Lichess.org which vastly extends support for screen reader users. The website has millions of active players."
- name: "Quote Retrieval System"
link: "https://github.com/TTWNO/quote_pdf"
description: "Written for a local roofing business to securely distribute quotes."
- name: "epub-with-pinyin"
link: "https://github.com/TTWNO/epub-with-pinyin"
description: "A program to add Pinyin above Chinese characters in .epub files to assist those learning Mandarin Chinese."
- name: "Programming Tutorials For The Visually Impaired"
link: "/emacspeak-tutorials/"
description: "Amateur-level production quality videos with all file buffers, written text, and shell commands read out by <a href=\"http://emacspeak.sourceforge.net/\">Emacspeak</a>, an Emacs extention for the blind."
- name: "lamegames"
link: "https://github.com/TTWNO/lamegames.io"
description: "A little games website I made for some demonstrations of Django and websocket functionality. Very, very lame. Would not recommend."
- name: "chess"
link: "https://github.com/TTWNO/chess"
description: "A command-line chess game in C++. It is compileable on nearly any system."
- name: "Emacspeak C Tutorials"
link: "https://tait.tech/emacspeak-tutorials/"
description: "C Tutorials for the Visually Impaired"
- name: "Simple Markdown Editor"
link: "https://tait.tech/md/"
description: "A simple online (and offline) browser-based markdown editor that supports the automatic creation of MathML."
#- name: "Emacspeak C Tutorials"
# link: "https://tait.tech/emacspeak-tutorials/"
# description: "C Tutorials for the Visually Impaired"

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ page.title }} | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
{% if page.math %}
<link rel="stylesheet" href="/assets/css/katex.css" id="math-css">
{% endif %}
{% if page.code %}
<link rel="stylesheet" href="/assets/css/highlighting.css" id="code-css">
{% endif %}
</head>
<body>
<main>
<div id="wrapper">
{{ content }}
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

@ -0,0 +1,3 @@
table {
table-layout: fixed;
}

@ -0,0 +1,3 @@
table { table-layout: fixed; }
/*# sourceMappingURL=transcription.css.map */

@ -0,0 +1,14 @@
{
"version": 3,
"file": "transcription.css",
"sources": [
"transcription.scss",
"_sass/transcription.scss"
],
"sourcesContent": [
"@import \"transcription\"\n",
"table {\n table-layout: fixed;\n}\n"
],
"names": [],
"mappings": "ACAA,AAAA,KAAK,CAAC,EACJ,YAAY,EAAE,KAAK,GACpB"
}

@ -33,7 +33,7 @@
<article>
<header>
<h1 class="post-title">Canadian History For Dummies</h1>
<time datetime="21-11-16" class="post-date">Tuesday, November 16 2021</time>
<time datetime="21-11-25" class="post-date">Thursday, November 25 2021</time>
</header>
<hr>

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.2.0">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2021-11-16T23:18:30-07:00</updated><id>/feed.xml</id><entry><title type="html">How To Produce Semantically Correct MathML From XaTeX/LaTeX (and other accessibility ideas)</title><link href="/2021/09/18/how-to-generate-proper-content-mathml-from-katex-or-latex/" rel="alternate" type="text/html" title="How To Produce Semantically Correct MathML From XaTeX/LaTeX (and other accessibility ideas)" /><published>2021-09-18T00:00:00-06:00</published><updated>2021-09-18T00:00:00-06:00</updated><id>/2021/09/18/how-to-generate-proper-content-mathml-from-katex-or-latex</id><content type="html" xml:base="/2021/09/18/how-to-generate-proper-content-mathml-from-katex-or-latex/">&lt;p&gt;During a recent run-in with the Simon Fraser Fraser University accessibility department,
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.2.0">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2021-11-25T21:47:55-07:00</updated><id>/feed.xml</id><entry><title type="html">How To Produce Semantically Correct MathML From XaTeX/LaTeX (and other accessibility ideas)</title><link href="/2021/09/18/how-to-generate-proper-content-mathml-from-katex-or-latex/" rel="alternate" type="text/html" title="How To Produce Semantically Correct MathML From XaTeX/LaTeX (and other accessibility ideas)" /><published>2021-09-18T00:00:00-06:00</published><updated>2021-09-18T00:00:00-06:00</updated><id>/2021/09/18/how-to-generate-proper-content-mathml-from-katex-or-latex</id><content type="html" xml:base="/2021/09/18/how-to-generate-proper-content-mathml-from-katex-or-latex/">&lt;p&gt;During a recent run-in with the Simon Fraser Fraser University accessibility department,
I learned that theyre writers are so well-trained as to write “image” where a simple diagram is shown,
and “print out picture of output” where a piece of code lies.
I figure the geniuses over there could use some help creating files for the visually impaired.

@ -99,6 +99,10 @@ This should be quite simple given how easy the lichess API is, and the <a href="
<li>The list of servers will be updated manually with <code class="language-plaintext highlighter-rouge">bt update</code>.</li>
</ul>
<p>I love this idea, but unfortunately, Canada has data retention laws that would stop me from protecting the privacy of anyone using a system delivered by me.
Unless I incorporate in Switzerland or the Seychelles, this is not a viable option.
Doing the above costs a fair amount in up-front investment that I am not willing to make at this point in time.</p>
<h2 id="5-3d-printing-of-google-mapsopenstreetmaps-data-for-the-visually-impaired">5. 3d printing of Google Maps/OpenStreetMaps data for the visually impaired.</h2>
<p>A larger project, to be sure, but one I think could be of interest.
@ -125,6 +129,57 @@ Then, add 4 more roads and figure out how to intersect them.</p>
<p>This is being worked on through the <a href="https://github.com/skarkkai/touch-mapper">touch-mapper</a> project.
They do not, however, have labels yet.</p>
<h2 id="6-3d-printed-binary-trees">6. 3D Printed Binary Trees</h2>
<p>A simple hub/connection system to connect nodes of a binary tree together to have a physical object for visually impaired computer science students to use for initial introduction into the subject of (binary) trees.</p>
<h2 id="65-javascript-binary-trees">6.5 Javascript Binary Trees</h2>
<p>Have a simple module for loading in an SVG of a tree, along with Javascript to make the diagram accessible by jumping left/right with the arrow keys and up to a parent with the up arrow.</p>
<h2 id="7-legopi-powered-logic-gates">7. Lego/Pi-Powered Logic Gates</h2>
<p>Lego or 3d printed logic gates with physical switches for in and out.
Again, sore of as an introductory tool for blind students learning computer science.</p>
<h2 id="8-more-tutorialsmaterials">8. More Tutorials/Materials</h2>
<p>Perhaps a broader selection of materials for computer science students with proper transcriptions for everything in this list:</p>
<ul>
<li>UML diagrams</li>
<li>UML sequence diagrams</li>
<li>Gantt charts</li>
<li>Advanced math equations</li>
<li>Stacks, Queues and Linked Lists</li>
<li>Memory layout tables</li>
<li>Logic gate diagrams</li>
<li>Decision charts; I think I could add some macros on top of</li>
<li>Timing diagrams (i.e., watching the state of various inputs and outputs over time [usually in relation to a clock cycle]); my current idea for this is a dynamic audiogram where you can listen to the clock + any other tracks at the same time to “see” the diagram</li>
<li>Circuit diagrams (not 100% relevant, but I have seen it in CS courses)</li>
<li>Database tables as HTML tables</li>
<li>Properly transcribed security content like buffer-overflows, underflows and similar things</li>
</ul>
<p>Although developing these is good, I think it is worthwhile to also create tools that make creation of these easier for both sighted and blind individuals.
This will make it easier for course transcribers who are not tech-savy and will enable the blind student to create the diagrams and send them back to their teachers.
Preferably have a “plain text” version which can be rendered as an SVG for use by visual learners, then make sure the SVG can be accessible with a Javascript hook.
This would (in theory) make it possible for a teacher to create the graphic in the specialized tool for that kind of chart, put it in their slides/course info/textbook/whatever and have the student able to extract the SVG and paste it somewhere where a script could make it readable.
Yes, the best case is the teacher cooperates 100%, but considering that is never the case, I figure making it easier to convert between the two is the best I can hope for.</p>
<p>Some other things I would like to do, if I could find the time:</p>
<ul>
<li>How to get started with Linux.</li>
<li>Text-based tutorials from start-to-finish with various frameworks: SpringBoot (Java), Django/Flask (Python), JavaEE.</li>
<li>Specific programming language instruction in all the basics: C/C++, Bash, Python, Javascript, HTML, Java. Perhaps in the future branch into Rust, Golang, Zig or Haskell.</li>
<li>Custom-made braille manuals for a set of affordable computer parts which can be purchased to assemble a computer from scratch, without sighted assistance (see <a href="/2021/06/21/uefi-audio/">current status of accessible BIOSes</a>).</li>
</ul>
<p>This would all be licensed as CC-BY-NC-SA.
I may drop the NC. As long as I have specified SA, then anyone (even for-profit companies) can use it as long as any changes are shared to the public as well.</p>
</main>
<hr>
<footer>

@ -50,55 +50,49 @@ I have all of my code projects hosted on <a href="https://github.com/TTWNO">my G
<li>
<p>
<a class="namelink" href="https://github.com/TTWNO/quote_pdf">Quote Retrieval System</a> &mdash; Written for a local roofing business to securely distribute quotes.
<a class="namelink" href="https://yggdrasil-sr.github.io/">Odilia Screen Reader</a> &mdash; Significant contributor to a new screen reader for Linux, written in the Rust programming language.
</p>
</li>
<li>
<p>
<a class="namelink" href="https://yggdrasil-sr.github.io/">Yggdrasil Screen Reader</a> &mdash; Some small pieces of code I've written have made it into a new screen reader for Linux, written in Rust.
</p>
</li>
<li>
<p>
<a class="namelink" href="https://github.com/ornicar/lila/pull/7772">Lichess</a> &mdash; A patch for Lichess.org which vastly extends support for screen reader users. The website has millions of active players.
<a class="namelink" href="/emacspeak-tutorials/">Programming Tutorials For The Visually Impaired</a> &mdash; Amateur-level production quality videos with all file buffers, written text, and shell commands read out by <a href="http://emacspeak.sourceforge.net/">Emacspeak</a>, an Emacs extention for the blind.
</p>
</li>
<li>
<p>
<a class="namelink" href="https://github.com/TTWNO/epub-with-pinyin">epub-with-pinyin</a> &mdash; A program to add Pinyin above Chinese characters in .epub files to assist those learning Mandarin Chinese.
<a class="namelink" href="https://tait.tech/md/">Simple Markdown Editor</a> &mdash; A simple online (and offline) browser-based markdown editor that supports the automatic creation of accessible math (MathML).
</p>
</li>
<li>
<p>
<a class="namelink" href="/emacspeak-tutorials/">Programming Tutorials For The Visually Impaired</a> &mdash; Amateur-level production quality videos with all file buffers, written text, and shell commands read out by <a href="http://emacspeak.sourceforge.net/">Emacspeak</a>, an Emacs extention for the blind.
<a class="namelink" href="https://github.com/ornicar/lila/pull/7772">Lichess</a> &mdash; A patch for Lichess.org which vastly extends support for screen reader users. The website has millions of active players.
</p>
</li>
<li>
<p>
<a class="namelink" href="https://github.com/TTWNO/lamegames.io">lamegames</a> &mdash; A little games website I made for some demonstrations of Django and websocket functionality. Very, very lame. Would not recommend.
<a class="namelink" href="https://github.com/TTWNO/quote_pdf">Quote Retrieval System</a> &mdash; Written for a local roofing business to securely distribute quotes.
</p>
</li>
<li>
<p>
<a class="namelink" href="https://github.com/TTWNO/chess">chess</a> &mdash; A command-line chess game in C++. It is compileable on nearly any system.
<a class="namelink" href="https://github.com/TTWNO/epub-with-pinyin">epub-with-pinyin</a> &mdash; A program to add Pinyin above Chinese characters in .epub files to assist those learning Mandarin Chinese.
</p>
</li>
<li>
<p>
<a class="namelink" href="https://tait.tech/emacspeak-tutorials/">Emacspeak C Tutorials</a> &mdash; C Tutorials for the Visually Impaired
<a class="namelink" href="https://github.com/TTWNO/lamegames.io">lamegames</a> &mdash; A little games website I made for some demonstrations of Django and websocket functionality. Very, very lame. Would not recommend.
</p>
</li>
<li>
<p>
<a class="namelink" href="https://tait.tech/md/">Simple Markdown Editor</a> &mdash; A simple online (and offline) browser-based markdown editor that supports the automatic creation of MathML.
<a class="namelink" href="https://github.com/TTWNO/chess">chess</a> &mdash; A command-line chess game in C++. It is compileable on nearly any system.
</p>
</li>

@ -19,7 +19,7 @@
<li>
<strong>Zone4 Systems Inc.</strong> &mdash; <em>Software Developer</em> (June 2021-present)
<p>Software development and QA (testing) for an international race timing company based in Canmore, AB. Testing using unittest and Selenium. Development done in Javascript and Tornado. Zone4 was founded in 2001 by Canadian Olympian Dan Roycroft..</p>
<p>Software development and QA (testing) for an international race timing company based in Canmore, AB. Testing using unittest and Selenium. Development done in Javascript and Tornado. Zone4 was founded in 2001 by Canadian Olympian Dan Roycroft.</p>
</li>
<li>
@ -85,15 +85,21 @@
<ul class="clear-list">
<li>
<strong><a href="https://github.com/TTWNO/quote_pdf">Quote Retrieval System</a></strong>
<strong><a href="https://yggdrasil-sr.github.io/">Odilia Screen Reader</a></strong>
<p>Written for a local roofing business to securely distribute quotes.</p>
<p>Significant contributor to a new screen reader for Linux, written in the Rust programming language.</p>
</li>
<li>
<strong><a href="https://yggdrasil-sr.github.io/">Yggdrasil Screen Reader</a></strong>
<strong><a href="/emacspeak-tutorials/">Programming Tutorials For The Visually Impaired</a></strong>
<p>Some small pieces of code I've written have made it into a new screen reader for Linux, written in Rust.</p>
<p>Amateur-level production quality videos with all file buffers, written text, and shell commands read out by <a href="http://emacspeak.sourceforge.net/">Emacspeak</a>, an Emacs extention for the blind.</p>
</li>
<li>
<strong><a href="https://tait.tech/md/">Simple Markdown Editor</a></strong>
<p>A simple online (and offline) browser-based markdown editor that supports the automatic creation of accessible math (MathML).</p>
</li>
<li>
@ -103,15 +109,15 @@
</li>
<li>
<strong><a href="https://github.com/TTWNO/epub-with-pinyin">epub-with-pinyin</a></strong>
<strong><a href="https://github.com/TTWNO/quote_pdf">Quote Retrieval System</a></strong>
<p>A program to add Pinyin above Chinese characters in .epub files to assist those learning Mandarin Chinese.</p>
<p>Written for a local roofing business to securely distribute quotes.</p>
</li>
<li>
<strong><a href="/emacspeak-tutorials/">Programming Tutorials For The Visually Impaired</a></strong>
<strong><a href="https://github.com/TTWNO/epub-with-pinyin">epub-with-pinyin</a></strong>
<p>Amateur-level production quality videos with all file buffers, written text, and shell commands read out by <a href="http://emacspeak.sourceforge.net/">Emacspeak</a>, an Emacs extention for the blind.</p>
<p>A program to add Pinyin above Chinese characters in .epub files to assist those learning Mandarin Chinese.</p>
</li>
<li>
@ -126,18 +132,6 @@
<p>A command-line chess game in C++. It is compileable on nearly any system.</p>
</li>
<li>
<strong><a href="https://tait.tech/emacspeak-tutorials/">Emacspeak C Tutorials</a></strong>
<p>C Tutorials for the Visually Impaired</p>
</li>
<li>
<strong><a href="https://tait.tech/md/">Simple Markdown Editor</a></strong>
<p>A simple online (and offline) browser-based markdown editor that supports the automatic creation of MathML.</p>
</li>
</ul>
<h2 id="skills">Skills</h2>

File diff suppressed because it is too large Load Diff

@ -0,0 +1,12 @@
#!/bin/bash
if [ -d "targe"t ]; then
rm -r ./target
fi
if [ -d "target_tmp" ]; then
rm -r ./target_tmp
fi
find ./src/ -name *md -exec ~/Documents/transcription-tools/create_html/save_html.sh '{}' ./target \;
./zip.sh

@ -0,0 +1,154 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
</head>
<body>
<main>
<div id="wrapper">
<h1 id="cmpt-225---introduction">CMPT 225 - Introduction</h1>
<h2 id="cmpt-225-d100--data-structures--programming">CMPT: 225, D100 — Data Structures &amp; Programming</h2>
<h2 id="graded-work">Graded Work</h2>
<ul>
<li>4 Assignments, 32%
<ul>
<li>Programming &amp; Written Components</li>
<li>Programs:
<ul>
<li>In C++</li>
<li>running on ESIL Linux machines</li>
<li>using command line tools</li>
</ul>
</li>
</ul>
</li>
<li>3 Tests, 33%
<ul>
<li>In class</li>
</ul>
</li>
<li>1 Final Exam, 35%</li>
</ul>
<h2 id="lectures">Lectures</h2>
<ul>
<li>Slides + audio recording will be posted <em>after</em> lectures.</li>
</ul>
<h2 id="labs">Labs</h2>
<ul>
<li>There are <em>help times</em> (first labs are <em>next week</em>)</li>
</ul>
<h2 id="instructor-office-hours">Instructor Office Hours</h2>
<ul>
<li>Will be primarily online — times TBA.</li>
</ul>
<h2 id="instructor-contact">Instructor Contact</h2>
<ul>
<li>Via Canvas or email (for email: write from your SFU account, use subject “225: …”)</li>
</ul>
<h2 id="some-basic-rules">Some Basic Rules</h2>
<ul>
<li>In class, do not distract others.</li>
<li>In class, no taking photos/videos/recording</li>
<li>Help each other, but submit your own work.</li>
<li>Masks are mandatory in class:
<ul>
<li>may be removed while asking a question</li>
<li>if you cannot wear a mask, please email me</li>
</ul>
</li>
<li>Please stay on “your” side of the table
<ul>
<li>and stay 6 away if you dont have a mask on</li>
</ul>
</li>
</ul>
<h2 id="course-content">Course Content</h2>
<ul>
<li>Algorithms: processes that operate on data</li>
<li>Programs: implementations of algorithms</li>
<li>Data in programs: stored in data structures
<ul>
<li>simple D.S.: variables</li>
<li>most algorithms required compound D.S.s: e.g. arrays, lists, …</li>
<li>most non-trivial applications require non-trivial D.S.s</li>
</ul>
</li>
<li>Data Type: collection of values + operations on these values</li>
<li>Multiple data types:
<ul>
<li>Cleaner/simpler slg. design</li>
<li>better code</li>
</ul>
</li>
<li>Abstract Data Type: defined by values + operations without reference to how things are implemented</li>
<li>ADTs provide an abstract view of data:
<ul>
<li>let us reason about algorithms at a high level, ignoring implementation details</li>
</ul>
</li>
<li>Good ADT choices reduce program complexity &amp; increase likelihood of success ( which is good).</li>
<li>a[i] vs. a(i)</li>
</ul>
<h2 id="we-will-look-at">We will look at:</h2>
<ul>
<li>fundamental ADTs</li>
<li>fundamental data structures (to implement them)</li>
<li>sorting algorithms</li>
<li>with attention to:
<ul>
<li>correctness</li>
<li>efficiency/speed</li>
<li>how to implement</li>
<li>how to choose (for an application)</li>
</ul>
</li>
</ul>
<h2 id="pre-regs">Pre-regs:</h2>
<ul>
<li>Basic Programming (e.g. CMPT 125)</li>
<li>Discrete Math (e.g. MACM 101)</li>
</ul>
<p>(students get these in many different ways)</p>
<h2 id="to-do">To Do</h2>
<ul>
<li>Read chapters 1 + 3 of text</li>
<li>Lear about CSIL</li>
</ul>
<h2 id="end">End</h2>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

@ -0,0 +1,869 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
</head>
<body>
<main>
<div id="wrapper">
<h1 id="stacks--queues---1">Stacks &amp; Queues - 1</h1>
<p>CMPT 225, Fall 2021, Lecture 2</p>
<h2 id="stack">Stack</h2>
<ul>
<li>ADT that stores a collection of objects/values</li>
<li>Insertions and removals follow last-in-first-out pattern</li>
<li>Fundamental Operations:
<ul>
<li>push: inserts an element on the “top”</li>
<li>pop: removes + returns the top element</li>
</ul>
</li>
</ul>
<p>e.g.</p>
<table>
<tbody>
<tr>
<td>Stack index</td>
<td>Value</td>
</tr>
</tbody>
</table>
<p>push a</p>
<table>
<tbody>
<tr>
<td>Stack index</td>
<td>Value</td>
</tr>
<tr>
<td>0</td>
<td>a</td>
</tr>
</tbody>
</table>
<p>push b</p>
<table>
<tbody>
<tr>
<td>Stack index</td>
<td>Value</td>
</tr>
<tr>
<td>0</td>
<td>b</td>
</tr>
<tr>
<td>1</td>
<td>a</td>
</tr>
</tbody>
</table>
<p>push c</p>
<table>
<tbody>
<tr>
<td>Stack index</td>
<td>Value</td>
</tr>
<tr>
<td>0</td>
<td>c</td>
</tr>
<tr>
<td>1</td>
<td>b</td>
</tr>
<tr>
<td>2</td>
<td>a</td>
</tr>
</tbody>
</table>
<p>pop</p>
<table>
<tbody>
<tr>
<td>Stack index</td>
<td>Value</td>
</tr>
<tr>
<td>0</td>
<td>b</td>
</tr>
<tr>
<td>1</td>
<td>a</td>
</tr>
</tbody>
</table>
<ul>
<li>conventient optional operations
<ul>
<li>size: return # of elements on stack // encapsulation</li>
<li>empty: check for emptiness // better than “size=0?”</li>
<li>top: return top element, but dont remove it // better than x = pop(); push(x) then use x</li>
</ul>
</li>
</ul>
<h2 id="algorithm-applications">Algorithm Applications</h2>
<ul>
<li>parasing/evaluation/transformation of expressions</li>
<li>speech recognition coding/decoding</li>
<li>shared network access control</li>
<li>programming languages &amp; execution
<ul>
<li>Postscript, Forth, the call stack</li>
</ul>
</li>
<li>things we do daily with computers</li>
</ul>
<h2 id="parenthasies-checking">Parenthasies Checking</h2>
<p>Parenthasies: <code class="language-plaintext highlighter-rouge">(()(((()())()()(()))</code></p>
<p>A diagram showing:
For every left parenthasies, add one to a counter.
For every right parthenthasies, remove one from the counter.
If the counter is not zero by the end of the sequence, there are too many on one side of the parenthasies.</p>
<p>Can be done by counting unmached left parenthasies:</p>
<p>Successful example:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td> </td>
<td>0</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>(</td>
<td>3</td>
</tr>
<tr>
<td>)</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
<tr>
<td>Yay!</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>Failed example:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Country</td>
</tr>
<tr>
<td> </td>
<td>0</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td> </td>
<td>Oh no its not zero!</td>
</tr>
</tbody>
</table>
<p>Another fail:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td> </td>
<td>0</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
<tr>
<td>)</td>
<td>-1</td>
</tr>
<tr>
<td> </td>
<td>Negative 1 this time!</td>
</tr>
</tbody>
</table>
<p>Do this one yourself:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td>(</td>
<td> </td>
</tr>
<tr>
<td>)</td>
<td> </td>
</tr>
<tr>
<td>)</td>
<td> </td>
</tr>
<tr>
<td>(</td>
<td> </td>
</tr>
</tbody>
</table>
<p>But consider multiple grouping symbols: <code class="language-plaintext highlighter-rouge">({{)</code>, <code class="language-plaintext highlighter-rouge">({){</code>, <code class="language-plaintext highlighter-rouge">({[( ... ]</code>.
Counting is not enough.</p>
<h2 id="parenthasies-checking-1">Parenthasies Checking</h2>
<p>Diagram showing failure on the last character of this sequence: <code class="language-plaintext highlighter-rouge">(()((()()))()(())))</code></p>
<p>Can be done by counting unmatched left parenthasies:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>(</td>
<td>3</td>
</tr>
<tr>
<td>)</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>Example 2:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
<tr>
<td>(</td>
<td>2</td>
</tr>
<tr>
<td>)</td>
<td>1</td>
</tr>
</tbody>
</table>
<p>Example 3:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
<tr>
<td>)</td>
<td>-1</td>
</tr>
</tbody>
</table>
<p>Example 4:</p>
<table>
<tbody>
<tr>
<td>Character</td>
<td>Counter</td>
</tr>
<tr>
<td>(</td>
<td>1</td>
</tr>
<tr>
<td>)</td>
<td>0</td>
</tr>
<tr>
<td>)</td>
<td>-1</td>
</tr>
<tr>
<td>(</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>But consider multiple grouping symbols:</p>
<ul>
<li>sequence <code class="language-plaintext highlighter-rouge">({})</code> will and should work</li>
<li>sequence <code class="language-plaintext highlighter-rouge">({)}</code> will work and should not</li>
<li>sequence <code class="language-plaintext highlighter-rouge">({[( ...</code> ???</li>
</ul>
<p>Counting is not enough.</p>
<h2 id="stack-based-algorithm-for-checking-grouping-symbols">Stack-based Algorithm for Checking Grouping Symbols</h2>
<p>Pseudo-code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>I is an input stream of symbols.
S is a new empty stack.
while there are signals to read from I {
c is next symbol from I
if c is a left grouping signal {
push c on S
} else if c is a right grouping signal{
if s in empty { report error and stop }
d is pop S
if c and d do not match { report error and stop }
endif // if c is not a grouping symbol, ignore it
end while
if S is not empty { report error and exit }
report ok
</code></pre></div></div>
<p>Diagram showing how each set of parenthasies are matched, innermost first torwards the outermost.</p>
<h2 id="stack-partially-filled-array-implementation">Stack: Partially-Filled Array Implementation</h2>
<p>Variables:</p>
<ul>
<li>A - an array of stack elements</li>
<li>capacity - size of array</li>
<li>top - index in array of top stack element
<ul>
<li>-1 if stack is empty</li>
</ul>
</li>
</ul>
<p>A starts out as:</p>
<table>
<tbody>
<tr>
<td>Index</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td></td>
<td>capacity-1</td>
</tr>
<tr>
<td>Value</td>
<td>a</td>
<td>b</td>
<td>c</td>
<td></td>
<td> </td>
</tr>
</tbody>
</table>
<ul>
<li>Capacity = &lt;size of A&gt;</li>
<li>top = 2</li>
</ul>
<p>push e</p>
<p>A now equals</p>
<table>
<tbody>
<tr>
<td>Index</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td></td>
<td>capacity-1</td>
</tr>
<tr>
<td>Value</td>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
<td></td>
<td> </td>
</tr>
</tbody>
</table>
<ul>
<li>capacity = &lt;size of A&gt;</li>
<li>top = 3</li>
</ul>
<h2 id="the-queue-adt">The Queue ADT</h2>
<p>A container that stores a collection of items with insertion and removal in “first-in-first-out” order.</p>
<p>Stack: a digram showing circled being placed on top of eachother, pushing circles into the tube, then popping them out from the same size (the top). Like a pringles can of elements.</p>
<p>Queue: a diagram showing circles being unqueued in on the left and dequeued on the right. Like waiting in line for food.</p>
<p>Basic operations:</p>
<ul>
<li>enqueue: add an item to the back of a list</li>
<li>dequeue: remove &amp; return the item at the front of the list</li>
</ul>
<h2 id="example-palindrome-checking">Example: Palindrome Checking</h2>
<p>Sequence <code class="language-plaintext highlighter-rouge">aba</code> should succeed.
Sequence <code class="language-plaintext highlighter-rouge">aab</code> should not.</p>
<p>In an array: a diagram showing the following pattern with the variable <code class="language-plaintext highlighter-rouge">i</code> set to the leftmost element and <code class="language-plaintext highlighter-rouge">j</code> set to the rightmost element.</p>
<table>
<tbody>
<tr>
<td>Values</td>
<td>a</td>
<td>b</td>
<td>c</td>
<td></td>
<td>s</td>
<td>b</td>
<td>a</td>
</tr>
</tbody>
</table>
<p>With a stack and queue:</p>
<ol>
<li>read symbols, starting each in a stack and a queue</li>
<li>while the stack, queue are not empty:
<ul>
<li>pop and element from the stack + delete an element from the queue</li>
<li>if different -&gt; not a palindrome</li>
</ul>
</li>
<li>palindrome</li>
</ol>
<h2 id="queue-array-implementations">Queue: Array Implementations</h2>
<p>Enqueue: a,b,c,d.
Front is now a, last is now d.</p>
<table>
<tbody>
<tr>
<td>Values</td>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td></td>
</tr>
</tbody>
</table>
<p>Dequeue returns and deletes first element.
Front is now b, last is now d.</p>
<table>
<tbody>
<tr>
<td>Values</td>
<td>?</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td></td>
</tr>
</tbody>
</table>
<p>Many enquques/dequeues later:</p>
<table>
<tbody>
<tr>
<td>Values</td>
<td>s</td>
<td></td>
<td>m</td>
<td>n</td>
<td>o</td>
</tr>
</tbody>
</table>
<p>Front element is now <code class="language-plaintext highlighter-rouge">m</code>, last element is now s.</p>
<table>
<tbody>
<tr>
<td>Values</td>
<td>?</td>
<td>t</td>
<td>u</td>
<td>v</td>
<td>w</td>
<td></td>
</tr>
</tbody>
</table>
<p>A similar slide is here, but I cant see the differnece.</p>
<p>Variables:</p>
<ul>
<li>array A is array of size capacity</li>
<li>front is 0</li>
<li>back is 0</li>
<li>size is 0</li>
<li>capacity = &lt;size of array&gt;</li>
</ul>
<p>Pseudo-code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>array A = array of size capacity // holds queue contents
front = 0 // index in A of front element, if queue not empty
back = 0 // index where next unqueued element will go
size = 0 // number of elements in queue
capacity = &lt;size of array&gt; // ???(can't read) queue size supported
enqueue(x) { // adds x to the back of the queue
// requires size&lt;capacity&gt;
A[back] = x
size = size + 1
back = (back + 1) % capacity
}
dequeue() { // removes front element of queue and returns it
// required size &gt; 0
temp = A[front]
front = (front + 1) % capacity
size = size - 1
return temp
}
</code></pre></div></div>
<p>Is <code class="language-plaintext highlighter-rouge">size == front - back</code>?</p>
<p>Diagram showing only true if front and back havent been switched around due to wrapping around back to the beginning o the array.</p>
<h2 id="circular-array-queue-example">“Circular Array” Queue Example</h2>
<ul>
<li>size = 0</li>
<li>front = 0</li>
<li>back = 0</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
</tr>
</tbody>
</table>
<p>enqueue: a,b,c,d</p>
<ul>
<li>front = 0</li>
<li>back = 4</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>empty</td>
<td>empty</td>
</tr>
</tbody>
</table>
<p>dequque 3 times</p>
<ul>
<li>front = 3</li>
<li>back = 4</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>d</td>
<td>empty</td>
<td>empty</td>
</tr>
</tbody>
</table>
<p>enqueue e,f,g,h</p>
<ul>
<li>front = 3</li>
<li>back = 2</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>g</td>
<td>h</td>
<td>empty</td>
<td>d</td>
<td>e</td>
<td>f</td>
</tr>
</tbody>
</table>
<p>unqueue j</p>
<ul>
<li>size = 6</li>
<li>font = 3</li>
<li>back = 3</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>g</td>
<td>h</td>
<td>j</td>
<td>d</td>
<td>e</td>
<td>f</td>
</tr>
</tbody>
</table>
<p>dequeue 3 times</p>
<ul>
<li>back = 3</li>
<li>front = 0</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>g</td>
<td>h</td>
<td>j</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
</tr>
</tbody>
</table>
<p>dequeue 3 times</p>
<ul>
<li>back = 3</li>
<li>front = 3</li>
</ul>
<table>
<tbody>
<tr>
<td>Values</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
<td>empty</td>
</tr>
</tbody>
</table>
<h2 id="end">End</h2>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

@ -0,0 +1,173 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
</head>
<body>
<main>
<div id="wrapper">
<h1 id="memory--pointers-1">Memory &amp; Pointers 1</h1>
<p>CMPT-225, Fall 2021</p>
<h2 id="computer-memory">Computer Memory</h2>
<ul>
<li>A sequence of locations</li>
<li>Indexed by address: 0,1,2,…</li>
<li>Each location stores a data <em>byte</em></li>
<li>Processor can <em>read</em> or <em>write</em> the byte at each address.</li>
<li>Regions of memory are allocated to processes <em>as needed</em>, according to some scheme.</li>
</ul>
<p>Diagram displaying “Code + Data for running part of OS” at the start of memory, “Free memory” in the middle, and “Code + Data for running user processes” and the end.</p>
<h2 id="variables--memory">Variables &amp; Memory</h2>
<p>A variable is (roughly) and names &amp; tagged collection of bytes:</p>
<p>Diagram showing:</p>
<ul>
<li>“int x; x = 6” taking 4 bytes of memory,</li>
<li>“char c; c = x;” taking 1 byte of memory</li>
<li>
<p>“bool b; b = true;” taking 1 byte of memory</p>
</li>
<li>So, <em>at run time</em>, each variable has an <em>address</em> in memory.</li>
<li>In C, C++ we can:
<ul>
<li>access the address of a variable</li>
<li>access a variable or memory location by its address</li>
<li>declare variables by storing addresses (pointers).</li>
</ul>
</li>
</ul>
<h2 id="addresses--pointers---by-example">Addresses &amp; Pointers - By Example</h2>
<ul>
<li>“int i = 5;”
<ul>
<li>allocate space for an <em>int</em>,</li>
<li>name the space “i”,</li>
<li>store 5 there</li>
</ul>
</li>
<li>“int *p;”
<ul>
<li>allocate space for an address,</li>
<li>name it p,</li>
<li>record its type as “pointer to int”</li>
</ul>
</li>
<li>“p = ∧i;”
<ul>
<li>&amp;i” is the address of “i”</li>
<li>store ∧i in p</li>
<li>so, p becomes a <em>pointer to i</em></li>
</ul>
</li>
<li>“cout « i;”
<ul>
<li>outputs the value stored in i, <em>5</em></li>
</ul>
</li>
<li>“cout « p;”
<ul>
<li>outputs the address of i: 0xbffffbbc</li>
</ul>
</li>
<li>“cout « *p;”
<ul>
<li>”*” <em>dereferences</em> p. That is, *p is the value <em>pointed to by p</em>. In this case <em>5</em></li>
</ul>
</li>
</ul>
<p>Diagram of memory:</p>
<table>
<tbody>
<tr>
<td>Address</td>
<td>Name</td>
<td>Value</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td><span id="point">254</span></td>
<td>i</td>
<td>5</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>923</td>
<td>p</td>
<td><a href="#point">254</a></td>
</tr>
</tbody>
</table>
<p>Second diagram of memory:</p>
<table>
<tbody>
<tr>
<td>Address</td>
<td>Name</td>
<td>Value</td>
<td>Size</td>
</tr>
<tr>
<td>0x6fffbe</td>
<td>i</td>
<td>5</td>
<td>4 bytes</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>???</td>
<td>p</td>
<td>0x6fffbe</td>
<td>4 bytesa</td>
</tr>
</tbody>
</table>
<p>Slide showing program code: see pointers.c</p>
<p>Slide showing output: see output.txt</p>
<p>Slide showing same program with different highlights.</p>
<p>Slide showing same output with different highlights.</p>
<h2 id="end">End</h2>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

@ -0,0 +1,38 @@
main() begin
XXXX = 111111111
XXXX is stored at &XXXX = 0xfffff40750e0
YYYY is a pointer to XXXX: YYYY = 0xfffff40750e0
* dereferences the pointer: *YYYY = 111111111
Array AAAA can be accessed with array notaions:
AAAA[0] = 222222222
AAAA[1] = 333333333
AAAA[2] = 444444444
Array variable AAAA is a pointer to A[0]: AAAA = 0xfffff40750f8
So, dereferencing AAAA should give us A[0]: *AAAA = 222222222
Adding 1 to an int pointer makes it point to the next int
AAAA = 0xfffff40750f8
AAAA+1 = 0xfffff40750fc
*(AAAA+1) = 333333333
We can look at contents of a chunk of memory:
Peeking at the memory in the neighbourhood of &XXXX, we see:
Address Contents in Hex Contents in Decimal
0xfffff40750fc: 13de4355 = 333333333
0xfffff40750f8: d3ed78e = 222222222
0xfffff40750f4: ffff = 65535
0xfffff40750f0: f40750e0 = -200847136
0xfffff40750ec: ffff = 65535
0xfffff40750e8: f40750e8 = -200847128
0xfffff40750e4: 6 = 6
0xfffff40750e0: 69f6bc7 = 111111111
0xfffff40750dc: ffff = 65535
0xfffff40750d8: a478f4e0 = -1535576864
0xfffff40750d4: aaaa = 43690
0xfffff40750d0: c46a13e0 = -999681056
0xfffff40750cc: ffff = 65535
0xfffff40750c8: a478f538 = -1535576776
0xfffff40750c4: ffff = 65535
main() ends

@ -0,0 +1,43 @@
/* a small program to demonstrate C++ addresses, pointers and arrays */
#include <iostream> /* Tait: missing name for import */
#include <iomanip> /* Tait: missing name from import */
using namespace std;
int main(){
cout << "main() begin\n";
int XXXX = 111111111;
cout << "XXXX = " << XXXX << endl;
cout << "XXXX is stored at &XXXX = " << &XXXX << endl;
int * YYYY = &XXXX;
cout << "YYYY is a pointer to XXXX: YYYY = " << YYYY << endl;
cout << "* dereferences the pointer: *YYYY = " << *YYYY << endl;
int AAAA[3] = { 222222222, 333333333, 444444444 };
cout << "Array AAAA can be accessed with array notaions: " << endl;
cout << " AAAA[0] = " << AAAA[0] << endl;
cout << " AAAA[1] = " << AAAA[1] << endl;
cout << " AAAA[2] = " << AAAA[2] << endl << endl;
cout << "Array variable AAAA is a pointer to A[0]: AAAA = " << AAAA << endl;
cout << "So, dereferencing AAAA should give us A[0]: *AAAA = " << *AAAA << endl << endl;
cout << "Adding 1 to an int pointer makes it point to the next int" << endl;
cout << "AAAA = " << AAAA << endl;
cout << "AAAA+1 = " << (AAAA+1) << endl;
cout << "*(AAAA+1) = " << *(AAAA+1) << endl << endl;
cout << "We can look at contents of a chunk of memory:" << endl;
cout << "Peeking at the memory in the neighbourhood of &XXXX, we see: " << endl << endl;
cout << "Address Contents in Hex Contents in Decimal " << endl;
int * p = (&XXXX)+7;
for (int i = 0; i < 15; i++) {
cout << p << ": " << setw(8) << hex << *p << " = " << setw(11) << dec << *p << endl;
p -= 1;
}
cout << "main() ends" << endl;
}

@ -0,0 +1,103 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
<link rel="stylesheet" href="/assets/css/katex.css" id="math-css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="vector-implementation-basics">Vector Implementation Basics</h1>
<ul>
<li>CMPT 225</li>
<li>Fall 2021</li>
<li>Lecture 4</li>
</ul>
<h2 id="example-using-a-simple-vector-class">Example Using a Simple “Vector” Class</h2>
<p>Take a look at the <a href="./simple_vector.cpp">simple_vector.cpp</a> file.</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="y1">y</span></td>
<td>float</td>
<td>(empty)</td>
</tr>
<tr>
<td><span id="x1">x</span></td>
<td>int</td>
<td>5</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>p</td>
<td>int *</td>
<td><a href="#x1">address</a></td>
</tr>
<tr>
<td>g</td>
<td>int *</td>
<td><a href="#y1">p+1</a></td>
</tr>
</tbody>
</table>
<p>What is *g?</p>
<h2 id="implementation-in-ivectorh">Implementation in <em>IVector.h</em></h2>
<p>See <a href="./IVector.h">IVector.h</a></p>
<h2 id="templates">Templates</h2>
<ul>
<li>Often, we have algorithms that will work on many data types, with few or no changes.</li>
<li>In <em>strongly typed</em> languages, we need a way to produce “generic” codecode that can work on different types in different places.</li>
<li>In C++, <em>templates</em> let us write generic code.</li>
<li>A template function or class defineition has a <em>placeholder</em> for one or more data types that is instanciated at compile time.</li>
<li>The instanciation may be different at different places in the same code.</li>
</ul>
<h2 id="test-program">Test Program</h2>
<p>See <a href="./test_program.cpp">test_program.cpp</a></p>
<p>See <a href="./simple_vector2.cpp">simple_vector2.cpp</a></p>
<h2 id="tvector-is-a-templated-version-of-ivector">TVector is a templated version of IVector</h2>
<p>See <a href="./TVector.h">TVector.h</a></p>
<p>See <a href="./Vector.h">Vector.h</a></p>
<p>See <a href="./TestVector.cpp">TestVector.cpp</a></p>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

@ -0,0 +1,84 @@
/*Transcriber's note: these first two lines of ifndef and define are added to make compilation work.*/
#ifndef _H_IVECTOR
#define _H_IVECTOR
#include <iostream>
class IVector
{
public:
// Constructor
IVector(int initSize=0)
: theSize{initSize}, theCapacity{initSize+10}
{
objects = new int(theCapacity);
}
// Destructor
~IVector()
{delete [] objects;}
// Check for emptyness
bool empty() const {return size() == 0;}
// Return size of list
int size() const {return theSize;}
// Access the element at a given index
// This is the non-const version, so you can change the element.
int& operator[](int index)
{
return objects[index];
}
// Access the element at a given index
// This is the const version, for accessing the value only
const int& operator[](int index) const
{
return objects[index];
}
// Increase the capacity (i.e., array size)
void reserve(int newCapacity)
{
if(newCapacity>theSize){
int *newArray = new int[newCapacity];
for (int k=0;k<theSize;++k){
newArray[k] = std::move(objects[k]);
}
theCapacity = newCapacity;
std::swap(objects, newArray);
delete [] newArray;
}
}
// add a new element to end of the list
void push_back(const int& x)
{
if(theSize==theCapacity) reserve(2*theCapacity+1);
objects[theSize++]=x;
}
// remove the last element from the list
void pop_back()
{
--theSize;
}
// Print out the size and contents of the list
void display() const
{
std::cout << "size=" << theSize << std::endl;
for(int i=0;i<theSize;++i){
std::cout<<"["<<i<<"]"<<"="<<objects[i]<<std::endl;
}
}
private:
int theSize;
int theCapacity;
int* objects; // The array is of type int.
// In C, C++ a variable of type int array is just a pointer to an int.
};
#endif

@ -0,0 +1,87 @@
/*
Transcriber's note: these first two lines of ifndef and define are added to make compilation work.
Note 2: all changed lines are labled with "//CHANGE" (without the quotes)
*/
#ifndef _H_TVECTOR
#define _H_TVECTOR
#include <iostream>
template <typename Object>//CHANGE
class TVector
{
public:
// Constructor
TVector(int initSize=0)
: theSize{initSize}, theCapacity{initSize+10}
{
objects = new Object(theCapacity);//CHANGE: annotation: in IVector object(...) was int(...)
}
// Destructor
~TVector()
{delete[] objects;}
// Check for emptyness
bool empty() const {return size() == 0;}
// Return size of list
int size() const {return theSize;}
// Access the element at a given index
// This is the non-const version, so you can change the element.
Object& operator[](int index)//CHANGE
{
return objects[index];
}
// Access the element at a given index
// This is the const version, for accessing the value only
const Object& operator[](int index) const //CHANGE
{
return objects[index];
}
// Increase the capacity (i.e., array size)
void reserve(int newCapacity)
{
if(newCapacity>theSize){
Object *newArray = new int[newCapacity]; //CHANGE
for (int k=0;k<theSize;++k){
newArray[k] = std::move(objects[k]);
}
theCapacity = newCapacity;
std::swap(objects, newArray);
delete [] newArray;
}
}
// add a new element to end of the list
void push_back(const Object& x)//CHANGE
{
if(theSize==theCapacity) reserve(2*theCapacity+1);
objects[theSize++]=x;
}
// remove the last element from the list
void pop_back()
{
--theSize;
}
// Print out the size and contents of the list
void display() const
{
std::cout << "size=" << theSize << std::endl;
for(int i=0;i<theSize;++i){
std::cout<<"["<<i<<"]"<<"="<<objects[i]<<std::endl;
}
}
private:
int theSize;
int theCapacity;
Object* objects;//CHANGE
};
#endif

@ -0,0 +1,48 @@
#include "Vector.h"
#include <iostream>
#include <algorithm>
using namespace std;
void print(const Vector<Vector<int>> arr)
{
int N=arr.size();
for(int i=0;i<N;++i)
{
cout<<"arr["<<i<<"]:";
for(int j=0;j<arr[i].size();++j)
cout<<" "<<arr[i][j];
cout<<endl;
}
}
class CompareVector
{
public:
bool operator()(const Vector<int>& lhs, const Vector<int>& rhs)const
{return lhs.size()<rhs.size();}
};
int main()
{
const int N=20;
Vector<Vector<int>> arr(N);
Vector<int> v;
for(int i=N-1;i>0;--i)
{
v.push_back(i);
arr[i]=v;
}
print(arr);
clock_t start=clock();
std::sort(begin(arr),end(arr),CompareVector{});
clock_t end=clock();
cout<<"Sorting time: "<<(end-start)<<endl;
print(arr);
return 0;
}

@ -0,0 +1,119 @@
// transcriber's note: include is added from slide because otherwise it will not compile
#include <utility>
#include <stdexcept>
template <typename Object>
class Vector
{
public:
explicit Vector(int initSize=0)
: theSize{initSize}, theCapacity{initSize + SPARE_CAPACITY}
{objects = new Object[theCapacity];}
Vector(const Vector& rhs)
: theSize{rhs.theSize}, theCapacity{rhs.theCapacity}, objects{nullptr}
{
objects = new Object[theCapacity];
for(int k=0;k<theSize;++k){
objects[k] = rhs.objects[k];
}
}
Vector& operator=(const Vector& rhs)
{
Vector copy = rhs;
std::swap(*this,copy);
return *this;
}
~Vector()
{delete[] objects;}
Vector(Vector&& rhs)
: theSize{rhs.theSize}, theCapacity{rhs.theCapacity}, objects{rhs.objects}
{
rhs.objects=nullptr;
rhs.theSize=0;
rhs.theCapacity=0;
}
Vector& operator=(Vector&& rhs)
{
std::swap(theSize,rhs.theSize);
std::swap(theCapacity,rhs.theCapacity);
std::swap(objects,rhs.objects);
return *this;
}
//stacky stuff
void push_back(Object x)
{
if(theSize==theCapacity)
reserve(2*theCapacity+1);
objects[theSize++]=std::move(x);
}
void pop_back()
{
if(empty())
// transcriber's note: the original line here was the following, however, it was changed to compile
// throw UnderflowException();
throw std::underflow_error::underflow_error;
--theSize;
}
const Object& back() const
{
if(empty())
// See note on line 58
throw std::underflow_error::underflow_error;
return objects[theSize-1];
}
// transcriber's note: these three functions added to make compilation work
const int size() const
{
return theSize;
}
Object& operator[](int index)
{
return objects[index];
}
const Object& operator[](int index) const
{
return objects[index];
}
bool empty() const {return size() == 0;}
void reserve(int newCapacity)
{
if(newCapacity>theSize){
Object *newArray = new int[newCapacity]; //CHANGE
for (int k=0;k<theSize;++k){
newArray[k] = std::move(objects[k]);
}
theCapacity = newCapacity;
std::swap(objects, newArray);
delete [] newArray;
}
}
typedef Object* iterator;
typedef const Object* const_iterator;
iterator begin()
{return &objects[0];}
const_iterator begin() const
{return &objects[0];}
iterator end()
{return &objects[size()];}
const_iterator end() const
{return &objects[size()];}
static const int SPARE_CAPACITY=2;
private:
int theSize;
int theCapacity;
Object* objects;
};

@ -0,0 +1,22 @@
#include "IVector.h"
#include <iostream>
using namespace std;
int main()
{
const int N = 20;
IVector v; // make an int vector
v.display(); // prints its contents
// Stores N ints in the Vector
for (int i=0; i<N; ++i)
{
v.push_back(i);
}
// print the contents
v.display();
return 0;
}

@ -0,0 +1,22 @@
#include "TVector.h"
#include <iostream>
using namespace std;
int main()
{
const int N = 20;
TVector<int> v; // make an int vector
v.display(); // prints its contents
// Stores N ints in the Vector
for (int i=0; i<N; ++i)
{
v.push_back(i);
}
// print the contents
v.display();
return 0;
}

@ -0,0 +1,46 @@
//
// Test Program for Basic Stack Class
//
#include <iostream> // for I/O facilities
using namespace std;
#include "MinimalStack.h" // basic_stackj declaration
int main(int argc, char* const argv[]){
cout << "\n\nMinimalStack Template Class Test Proceedure - START\n\n";
// Make some stacks, and verify that empty() says they are empty.
MinimalStack<int> s1;
MinimalStack<float> s2;
cout<<"s1.isEmpty() = "<<s1.isEmpty()<<"\n";
cout<<"s2.isEmpty() = "<<s2.isEmpty()<<"\n";
// Put some things on them
cout<<"s1.push("<<1<<")\n";
s1.push(1);
cout<<"s1.push("<<2<<")\n";
s1.push(2);
cout<<"s2.push("<<1.5<<")\n";
s2.push(1.5);
cout<<"s2.push("<<2.5<<")\n";
s2.push(2.5);
// Verify that isEmpty() reports that they are empty
// and that the right things are on top
cout<<"s1.isEmpty()="<<s1.isEmpty()<<"\n";
cout<<"s1.top()="<<s1.top()<<"\n";
cout<<"s2.isEmpty()="<<s2.isEmpty()<<"\n";
cout<<"s2.top()="<<s2.top()<<"\n";
// Empty them and verify that isEmpty() again reports that they are empty.
while(!s1.isEmpty()){
cout<<"s1.pop()="<<s1.pop()<<"\n";
}
cout<<"s1.isEmpty()="<<s1.isEmpty()<<"\n";
while(!s2.isEmpty()){
cout<<"s2.pop()="<<s2.pop()<<"\n";
}
// transcriber's note: the following lines are not contained on the slide, but I am assuming they are supposed to be there.
cout<<"s2.isEmpty()="<<s2.isEmpty()<<"\n";
return 0;
}

@ -0,0 +1,370 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
<link rel="stylesheet" href="/assets/css/katex.css" id="math-css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="dynamic-data-structures---linked-lists">Dynamic Data Structures - Linked Lists</h1>
<p>Transcribers note:
Each row in the Value|Pointer/Link tables represents a node.
Despite the fact that the link/pointer takes you to the “value” column, in reality it is taking you to the node “row”, its not just a pointer to the value.</p>
<h2 id="dynamic-data-structures">Dynamic Data Structures</h2>
<ul>
<li>Basic types (e.g. int, char, bool, …), and arrays of these store a <em>fixed</em> amount of data.</li>
<li>We want implementations of ADTs like stacks + queues to <em>grow &amp; shrink</em> (their memory use) <em>as needed</em>.
<ul>
<li>e.g. like Vector, Array-something? (cant read), String classes</li>
</ul>
</li>
</ul>
<p>Basic Idea:</p>
<p>Store data in a collection of (simple) objects.
add/delete these as needed; link them all together to make the main object.</p>
<h2 id="linked-lists">Linked Lists</h2>
<ul>
<li>A sequence of simple objects (nodes): each storing one datum (plus a link…) linked together in a chain</li>
<li>E.g., to store the list &lt;3, 5, 7&gt;:</li>
</ul>
<table>
<thead>
<tr>
<th>Datum</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="z1">3</span></td>
<td><a href="#z2">link</a></td>
</tr>
<tr>
<td><span id="z2">5</span></td>
<td><a href="#z3">link</a></td>
</tr>
<tr>
<td><span id="z3">7</span></td>
<td>link to nowhere</td>
</tr>
</tbody>
</table>
<ul>
<li>These objects have no names, (in contrast to declared values)
<ul>
<li>we access them by following links
<ul>
<li>in Java, references (implemented as pointers)</li>
<li>in C++, pointers</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>Need one named place to start like so:</p>
<p>A normal variable of type “pointer to a node”: <a href="#y1">First</a></p>
<table>
<thead>
<tr>
<th>Data</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="y1">3</span></td>
<td><a href="#y2">pointer</a></td>
</tr>
<tr>
<td><span id="y2">5</span></td>
<td><a href="#y3">pointer</a></td>
</tr>
<tr>
<td><span id="y3">7</span></td>
<td>null pointer</td>
</tr>
</tbody>
</table>
<h2 id="implementing-a-stack-with-a-linked-list-by-example">Implementing a Stack with a Linked List (By example)</h2>
<p>Code to run, followed by the current stack.</p>
<h3 id="stack-s"><code>stack s;</code></h3>
<p><label for="tof">top or front</label>
<span id="tof">node</span></p>
<h3 id="spush3"><code>s.push(3)</code></h3>
<p><a href="#x1">front</a></p>
<table>
<thead>
<tr>
<th>Data</th>
<th>Pointer</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="x1">3</span></td>
<td>null pointer</td>
</tr>
</tbody>
</table>
<h3 id="etc">etc…</h3>
<p></p>
<h3 id="spush5"><code>s.push(5)</code></h3>
<p><a href="#w1">front</a></p>
<table>
<thead>
<tr>
<th>Data</th>
<th>Pointer</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="w1">5</span></td>
<td><a href="#w2">pointer</a></td>
</tr>
<tr>
<td><span id="w2">8</span></td>
<td><a href="#w3">pointer</a></td>
</tr>
<tr>
<td><span id="w3"></span></td>
<td>… (<a href="#w4">pointer</a>)</td>
</tr>
<tr>
<td><span id="w4">3</span></td>
<td>null pointer</td>
</tr>
</tbody>
</table>
<h3 id="spush7"><code>s.push(7)</code></h3>
<p><a href="#v1">front</a></p>
<table>
<tbody>
<tr>
<td>Value</td>
<td>Pointer</td>
</tr>
<tr>
<td><span id="v1">7</span></td>
<td><a href="#v2">pointer</a></td>
</tr>
<tr>
<td><span id="v2">5</span></td>
<td><a href="#v3">pointer</a></td>
</tr>
<tr>
<td><span id="v3">8</span></td>
<td><a href="#v4">pointer</a></td>
</tr>
<tr>
<td><span id="v4"></span></td>
<td></td>
</tr>
</tbody>
</table>
<p>To perform the push(7):</p>
<ol>
<li>Make a new node to store the 7</li>
<li>modify links to insert it correctly</li>
</ol>
<p>Original:</p>
<p><a href="#u1">front</a></p>
<table>
<thead>
<tr>
<th>Data</th>
<th>Pointer</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="u1">5</span></td>
<td><a href="#u2">pointer</a></td>
</tr>
<tr>
<td><span id="u2">8</span></td>
<td></td>
</tr>
</tbody>
</table>
<p>After operation:</p>
<p><a href="#t1">front</a> now points at node with value 7; node with value 7s pointer now points to the <a href="#u1">old front</a></p>
<table>
<thead>
<tr>
<th>Value</th>
<th>Pointer</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="t1">7</span></td>
<td><a href="#t2">pointer</a></td>
</tr>
<tr>
<td><span id="t2">5</span></td>
<td><a href="#t3">pointer</a></td>
</tr>
<tr>
<td><span id="t3">8</span></td>
<td></td>
</tr>
</tbody>
</table>
<h3 id="spop"><code>s.pop()</code></h3>
<p><a href="#s1">front</a></p>
<table>
<thead>
<tr>
<th>Value</th>
<th>Pointer</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="s1">5</span></td>
<td><a href="#s2">pointer</a></td>
</tr>
<tr>
<td><span id="s2">8</span></td>
<td></td>
</tr>
</tbody>
</table>
<p>How to perform the pop():</p>
<ol>
<li>Change the “top” link</li>
<li>Return the old top node</li>
</ol>
<p>Linked list after running operation (note that two links go to the same node):</p>
<p><a href="#r0">top</a> now points to second row</p>
<table>
<thead>
<tr>
<th>Value</th>
<th>Pointer</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="r1">7</span> (return this value)</td>
<td><a href="#r0">pointer</a></td>
</tr>
<tr>
<td><span id="r0">5</span></td>
<td><a href="#r2">pointer</a></td>
</tr>
<tr>
<td><span id="r2">8</span></td>
<td></td>
</tr>
</tbody>
</table>
<ul>
<li>Caveat 1: dont lose the old top value</li>
<li>Caveat 2: dont ignore the old top node (it still consumes space)</li>
</ul>
<h3 id="improved-pop">“Improved” pop():</h3>
<ol>
<li>Store the old top value in “temp”</li>
<li>make <em>top</em> link to the new top node</li>
<li><em>free</em> the space for the old top node</li>
<li>return temp</li>
</ol>
<h2 id="the-list-class-a-doubly-linked-list-implementation-of-a-list">The List Class (A doubly-linked list implementation of a list)</h2>
<p>See <a href="./list.cpp">list.cpp</a></p>
<p>&lt;…5,6,7,…&gt; in a double linked list:</p>
<table>
<thead>
<tr>
<th>Prev</th>
<th>Value</th>
<th>Next</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td><span id="d0">5</span></td>
<td><a href="#d1">next</a></td>
</tr>
<tr>
<td><a href="#d0">prev</a></td>
<td><span id="d1">6</span></td>
<td><a href="#d2">next</a></td>
</tr>
<tr>
<td><a href="#d1">prev</a></td>
<td><span id="d2">7</span></td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="end">End</h2>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

@ -0,0 +1,22 @@
#include <utility> // not in original, needed for compilation
template <typename Object>
class List
{
private:
// the basic doubly linked list node
// Nested inside a list, can be public
// because the node itself is private
struct Node
{
Object data; // (annotation: list element)
Node *prev; // (annotation: pointer to next node)
Node *next; // (annotation: pointer to previous node)
Node(const Object& d = Object{}, Node* p = nullptr, Node* n = nullptr)
: data{d}, prev{p}, next{n} {}
Node(Object&& d, Node* p = nullptr, Node* n = nullptr)
: data{std::move(d)}, prev{p}, next{n} {}
};
}; // not in original, needed for compilation

@ -0,0 +1,716 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
<link rel="stylesheet" href="/assets/css/katex.css" id="math-css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="cmpt-225call-stack--heap-memory">CMPT 225Call Stack &amp; Heap Memory</h1>
<h2 id="the-call-stack">The call stack</h2>
<ul>
<li>Suppose a function <code class="language-plaintext highlighter-rouge">A</code> calls a function <code class="language-plaintext highlighter-rouge">B</code>, which calls <code class="language-plaintext highlighter-rouge">C</code>.</li>
<li>During execution control passes from (the code for) <code class="language-plaintext highlighter-rouge">A</code>, to <code class="language-plaintext highlighter-rouge">B</code>, then to <code class="language-plaintext highlighter-rouge">C</code>.</li>
<li>The execution of <code class="language-plaintext highlighter-rouge">C</code> ends, control must return to <code class="language-plaintext highlighter-rouge">B</code> and then to <code class="language-plaintext highlighter-rouge">A</code>.</li>
</ul>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mo></mo><mi>A</mi><mo></mo><mi>B</mi><mo></mo><mi>C</mi><mo></mo><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi></mrow><annotation encoding="application/x-tex">
... \leftrightarrow A \leftrightarrow B \leftrightarrow C \leftrightarrow ...
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.36687em;vertical-align:0em;"></span><span class="mord">.</span><span class="mord">.</span><span class="mord">.</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathdefault">A</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathdefault" style="margin-right:0.05017em;">B</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathdefault" style="margin-right:0.07153em;">C</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.10556em;vertical-align:0em;"></span><span class="mord">.</span><span class="mord">.</span><span class="mord">.</span></span></span></span></span></p>
<ul>
<li>At each function call, the system records where control should return to be <em>pushing an activation record on the call stack</em></li>
<li>The call stack also records all local variables, including the arguments to the function call.</li>
</ul>
<h2 id="call-stack-illustration">Call Stack Illustration</h2>
<p>Code is in <a href="./call_stack.cpp">call_stack.cpp</a></p>
<p>Call stack for this process:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Data</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>main</td>
<td>a=1, b=0</td>
</tr>
<tr>
<td>f(2)</td>
<td>c=2, d=3, e=8</td>
</tr>
<tr>
<td>g(4)</td>
<td>i=4</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>g</td>
<td>&lt;code for g&gt;</td>
</tr>
<tr>
<td>f</td>
<td>&lt;code for f&gt;</td>
</tr>
<tr>
<td>main</td>
<td>&lt;code for main&gt;</td>
</tr>
</tbody>
</table>
<h2 id="stack-code">Stack Code</h2>
<p>Picture of code, which can be found in <a href="./stack_pointers.cpp">stack_pointers.cpp</a></p>
<p>Picture of code output, which can be found in <a href="stack_pointers_output.txt">stack_pointers_output.txt</a></p>
<h2 id="dynamic-memory-or-heap">Dynamic Memory <em>or</em> Heap</h2>
<ul>
<li>Vairables declared in functions are stored on the <em>call stack</em></li>
<li>These variables:
<ul>
<li>are of fixed size</li>
<li>are destoryed when the function they are defined in terminates</li>
</ul>
</li>
<li>We often want a function <code class="language-plaintext highlighter-rouge">f</code> to create data that can be used after it returns.
<ul>
<li>In particular, dynamic data structures require this!</li>
</ul>
</li>
<li>This data is stored in “the heap”, a region of memory that is allocated dynamically as needed!</li>
</ul>
<h2 id="in-c">In C++:</h2>
<ul>
<li>Basic (or primitive) types can be stored on the call stack <em>or</em> on the heap.</li>
<li>Objects (e.g. instances of classes) can be stored on the call stack <em>or</em> on the heap.</li>
<li>Variables declared in functions are on the stack</li>
<li>Allocation on the heap is denoted by “new”.</li>
</ul>
<h2 id="ex-basic-types-on-call-stack--heap">Ex: Basic Types on Call Stack &amp; Heap</h2>
<p>Code snippet below:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>f(){
int n; //n is on stack
n=6;
int * np;//np is on stack
np = new int;// new int is stored in heap
*np = 7; // np points to the location.
}
</code></pre></div></div>
<h2 id="ex-basic-types-on-call-stack--heap-1">Ex: Basic Types on Call Stack &amp; Heap</h2>
<p>See code above.</p>
<p>Call stack:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Note</th>
</tr>
</thead>
<tbody>
<tr>
<td>n</td>
<td>6</td>
<td> </td>
</tr>
<tr>
<td>np</td>
<td><a href="#h1">pointer</a></td>
<td><code class="language-plaintext highlighter-rouge">np</code> is a pointer to the location on the heap.</td>
</tr>
</tbody>
</table>
<p>Heap:</p>
<table>
<thead>
<tr>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>...</td>
</tr>
<td><span id="h1">7</span></td>
<tr>
<td>...</td>
</tr>
</tbody>
</table>
<p>Note: When <code class="language-plaintext highlighter-rouge">f</code> ends, <code class="language-plaintext highlighter-rouge">np</code> is gone (the stack is popped), but the space it pointed to <em>is not</em>.</p>
<h2 id="class-instances-on-heap--stack-combines-notes-on-two-slides">Class Instances on Heap &amp; Stack (combines notes on two slides)</h2>
<p>Code snippet:</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">List</span> <span class="o">*</span> <span class="nf">f</span><span class="p">(){</span>
<span class="n">List</span> <span class="n">L</span><span class="p">;</span>
<span class="n">List</span> <span class="o">*</span> <span class="n">Lp</span><span class="p">;</span>
<span class="n">Lp</span> <span class="o">=</span> <span class="k">new</span> <span class="n">List</span><span class="p">();</span>
<span class="k">return</span> <span class="n">Lp</span><span class="p">;</span> <span class="c1">// returns pointer</span>
<span class="p">}</span>
<span class="n">main</span><span class="p">(){</span>
<span class="p">...</span>
<span class="n">List</span> <span class="o">*</span> <span class="n">lst</span> <span class="o">=</span> <span class="n">f</span><span class="p">();</span> <span class="c1">// lst becomes a pointer to the list object</span>
<span class="p">...</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Call Stack:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Values</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>lst</td>
<td><a href="#o2">pointer to heap</a></td>
<td>in main</td>
</tr>
<tr>
<td>L</td>
<td>&lt;List Object&gt;</td>
<td>entire List object; in function <code class="language-plaintext highlighter-rouge">f</code> (destroyed when f ends)</td>
</tr>
<tr>
<td>Lp</td>
<td><a href="#o2">pointer to &lt;List Object&gt;</a></td>
<td>in function <code class="language-plaintext highlighter-rouge">f</code> (this <em>pointer</em> is also destroyed when f exits); annotation: this instanciates the list class</td>
</tr>
</tbody>
</table>
<p>Heap:</p>
<table>
<tbody>
<tr>
<td>Value</td>
<td>Notes</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td><span id="o2">&lt;List Object&gt;</span></td>
<td>entire list object</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="accessing-instance-members-in-c-combination-of-notes-in-two-slides">Accessing Instance Members in C++ (combination of notes in two slides)</h2>
<p>Suppose a class <em>Store</em> with:</p>
<ul>
<li>a data member <code class="language-plaintext highlighter-rouge">x</code>. (an int)</li>
<li>a function <code class="language-plaintext highlighter-rouge">put(v)</code> that stores <code class="language-plaintext highlighter-rouge">v</code> in <code class="language-plaintext highlighter-rouge">x</code>.</li>
<li>a function <code class="language-plaintext highlighter-rouge">get()</code> that returns the value of <code class="language-plaintext highlighter-rouge">x</code>.</li>
</ul>
<p>Consider this code fragment:</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">f</span><span class="p">(){</span>
<span class="p">...</span>
<span class="n">Store</span> <span class="n">s1</span><span class="p">;</span>
<span class="n">s1</span><span class="p">.</span><span class="n">put</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span>
<span class="n">y</span><span class="o">=</span><span class="n">s1</span><span class="p">.</span><span class="n">get</span><span class="p">();</span><span class="c1">//y=5</span>
<span class="p">...</span>
<span class="n">Store</span> <span class="o">*</span><span class="n">s2</span><span class="p">;</span>
<span class="n">s2</span><span class="o">=</span><span class="k">new</span> <span class="n">Store</span><span class="p">();</span><span class="c1">// (annotation: XX)</span>
<span class="n">s2</span><span class="p">.</span><span class="n">put</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span><span class="c1">// (annotation: X)</span>
<span class="n">y</span><span class="o">=</span><span class="n">s2</span><span class="p">.</span><span class="n">get</span><span class="p">()</span><span class="c1">// (annotation: X)</span>
<span class="p">...</span>
<span class="o">*</span><span class="n">s2</span><span class="p">.</span><span class="n">put</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span><span class="c1">// (annotation: X)</span>
<span class="n">y</span><span class="o">=*</span><span class="n">s2</span><span class="p">.</span><span class="n">get</span><span class="p">();</span><span class="c1">// (annotation: X)</span>
<span class="p">...</span>
<span class="p">(</span><span class="o">*</span><span class="n">s2</span><span class="p">).</span><span class="n">put</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span><span class="c1">// (annotation: check mark)</span>
<span class="n">y</span><span class="o">=</span><span class="p">(</span><span class="o">*</span><span class="n">s2</span><span class="p">).</span><span class="n">get</span><span class="p">();</span><span class="c1">// (annotation: check mark)</span>
<span class="p">...</span>
<span class="n">s2</span><span class="o">-&gt;</span><span class="n">put</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span> <span class="c1">// equiv. to (*s).put(5)// (annotation: check mark)</span>
<span class="n">y</span><span class="o">=</span><span class="n">s2</span><span class="o">-&gt;</span><span class="n">get</span><span class="p">();</span> <span class="c1">// equiv. to y=(*s).get()// (annotation: check mark)</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Call Stack:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>s2</td>
<td><a href="#pts">pointer to &lt;Store Object&gt;</a></td>
<td> </td>
</tr>
<tr>
<td>s1</td>
<td>&lt;Store Object [x=5]&gt;</td>
<td> </td>
</tr>
</tbody>
</table>
<p>Heap:</p>
<table>
<thead>
<tr>
<th>Value</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="pts">&lt;Store Object [x=5]&gt;</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="using-the-textbook-list-class">Using the Textbook List Class</h2>
<p>See <a href="./textbook_list.cpp">textbook_list.cpp</a>
and <a href="./List.h">List.h</a>.</p>
<p>Call stack during the run of the program:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>N</td>
<td>5</td>
<td>within main</td>
</tr>
<tr>
<td>lst</td>
<td>&lt;List Object [size=5, head=0, tail=4]&gt;</td>
<td>within main</td>
</tr>
</tbody>
</table>
<p>The heap is empty.</p>
<p>See “doubly linked list” example in lecture 05.</p>
<h2 id="the-list-class">The List Class</h2>
<p>Two images of the doubly linked list slides from lecture 05 with a big red X written overtop each image.</p>
<h2 id="the-list-class---constructor">The List Class - Constructor</h2>
<p>Code snippet:</p>
<pre><code class="language-C++">private:
int theSize;
Node *head;
Node *tail;
void init()
{
theSize = 0;
head = new Node;
tail = new Node;
head-&gt;next = tail;
tail-&gt;prev = head;
}
};
</code></pre>
<p>After constructor:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Next</th>
<th>Value</th>
<th>Prev</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="t0">head</span></td>
<td><a href="#t1">pointer</a></td>
<td>nullptr</td>
<td>nullptr</td>
</tr>
<tr>
<td><span id="t1">tail</span></td>
<td>nullptr</td>
<td>nullptr</td>
<td><a href="#t0">pointer</a></td>
</tr>
</tbody>
</table>
<p>Code snippet form <a href="./List.h">List.h</a>:</p>
<pre><code class="language-C++">Node(const Object&amp; d = Object{}, Node* p = nullptr, Node* n = nullptr)
: data{d}, prev{p}, next{n} {}
</code></pre>
<h2 id="the-list-class---the-iterators">The List Class - The iterators</h2>
<ul>
<li>Data member (Node* current)
<ul>
<li>a pointer to a Node. (the list iterators are implemented with pointers.)</li>
</ul>
</li>
<li>Constructors:
<ul>
<li>turn a pointer into an iterator:
<ul>
<li><code class="language-plaintext highlighter-rouge">iterator(Node* p): const_iterator{p}{}</code></li>
<li><code class="language-plaintext highlighter-rouge">const_iterator(Node* p): current{p}</code></li>
</ul>
</li>
</ul>
</li>
<li>function:
<ul>
<li><code class="language-plaintext highlighter-rouge">iterator end(){ return iterator(tail}</code>
<ul>
<li>turns the tail pointer into the iter to “end”. (???cant read completely)</li>
<li>it corresponds to “just past the end” of the list</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="the-list-class---the-push_back-function">The List Class - the push_back function</h2>
<pre><code class="language-C++">// add an element to the tail end of the list
// end() is the end iterator
// x is the element to add
void push_back(const Object&amp; x){insert(end(), x);}
iterator insert(iterator itr, const Object&amp; x){
Node *p = itr.current; // turns the iterator into a pointer
++theSize; //increments size variable
return iterator(p-&gt;prev=p-&gt;prev-&gt;next=new Node(x, p-&gt;prev, p)) // turns the pointer into an iterator
// p-&gt;prev=p-&gt;prev-&gt;next=new Node(x, p-&gt;prev, p)) does two things:
// 1. Makes a new node `N`
// 2. stores a pointer to `N` in p-&gt;prev and it p-&gt;prev-&gt;next
}
</code></pre>
<h2 id="the-list-class---inserting-the-first-element-combination-of-notes-from-two-slides">The List Class - Inserting the first element (combination of notes from two slides)</h2>
<p>Code snippet:</p>
<pre><code class="language-C++">// itr is the end
iterator insert(iterator itr, const Object&amp; x){
Node *p = itr.current;// itr.current is the tail
++theSize;
return iterator(p-&gt;prev=p-&gt;prev-&gt;next=new Node(x, p-&gt;prev, p)); // turns pointer into iterator
// x is new list element
// p-&gt;prev is initial value of prev
// p is initial value of next
}
</code></pre>
<p>List pointed to by <a href="#d1">pointer p</a> (original)</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Next</th>
<th>Value</th>
<th>Prev</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="d0">head</span></td>
<td><a href="#d1">pointer</a></td>
<td>nullptr</td>
<td>nullptr</td>
</tr>
<tr>
<td><span id="d1">tail</span></td>
<td>nullptr</td>
<td>nullptr</td>
<td><a href="#d0">pointer</a></td>
</tr>
</tbody>
</table>
<p>The “new Node(…)” object:</p>
<table>
<thead>
<tr>
<th>Next</th>
<th>Value</th>
<th>Prev</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="#d1">pointer</a></td>
<td>nullptr</td>
<td><a href="#d0">pointer</a></td>
</tr>
</tbody>
</table>
<h2 id="transcribers-addition-implied-in-diagram-what-does-p-prevp-prev-nextnew-node-do-to-this-list">Transcribers addition (implied in diagram): What does <code class="language-plaintext highlighter-rouge">p-&gt;prev=p-&gt;prev-&gt;next=New Node(...)</code> do to this list?</h2>
<p>Transcibers note: It inserts the <code class="language-plaintext highlighter-rouge">new Node(...)</code> into the list like so:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Next</th>
<th>Value</th>
<th>Prev</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="l0">head</span></td>
<td><a href="#l2">pointer</a></td>
<td>nullptr</td>
<td>nullptr</td>
</tr>
<tr>
<td><span id="l2"><code class="language-plaintext highlighter-rouge">new Node(...)</code></span></td>
<td><a href="#l1">pointer</a></td>
<td>nullptr</td>
<td><a href="#l0">pointer</a></td>
</tr>
<tr>
<td><span id="l1">tail</span></td>
<td>nullptr</td>
<td>nullptr</td>
<td><a href="#l2">pointer</a></td>
</tr>
</tbody>
</table>
<p>Transcibers note: This cancels the previous connection between <code class="language-plaintext highlighter-rouge">head</code> and <code class="language-plaintext highlighter-rouge">tail</code> directly.</p>
<h2 id="using-the-textbook-list-class-1">Using the Textbook List Class</h2>
<p>Code snippet:</p>
<pre><code class="language-C++">#include "dsexceptions.h"
#include "List.h"
int main(){
const int N = 5;
List&lt;int&gt; = lst;
for(int i=N-1; i&lt;0; --i){
lst.push_front(i);
}
return 0;
}
</code></pre>
<p>When lst contains the two values [1, 2]:</p>
<p>Call Stack:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>N</td>
<td>5</td>
<td> </td>
</tr>
<tr>
<td>lst</td>
<td>&lt;List Object [theSize=2, head=<a href="#g3v">pointer</a>, tail=<a href="#g1v">pointer</a>&gt;</td>
<td> </td>
</tr>
</tbody>
</table>
<p>Heap (transcribers note: the link takes you to the value, but the pointer is to the group of values [next, value, prev), A.K.A. a Node):</p>
<table>
<tbody>
<tr>
<td>Value</td>
<td>Notes</td>
</tr>
<tr>
<td><a href="#g2v">Node.next</a></td>
<td>Node index 0</td>
</tr>
<tr>
<td><span id="g0v">Node Value: 1</span></td>
<td>Node index 0</td>
</tr>
<tr>
<td><a href="#g3v">Node.prev</a></td>
<td>Node index 0</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>Node.next (nullptr)</td>
<td>Ending Node</td>
</tr>
<tr>
<td><span id="g1v">Node Value: ?</span></td>
<td>Ending Node</td>
</tr>
<tr>
<td><a href="#g2v">Node.prev</a></td>
<td>Ending Node</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td><a href="#g1v">Node.next</a></td>
<td>Node index 1</td>
</tr>
<tr>
<td><span id="g2v">Node Value: 2</span></td>
<td>Node index 1</td>
</tr>
<tr>
<td><a href="#g0v">Node.prev</a></td>
<td>Node index 1</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td><a href="#g0v">Node.next</a></td>
<td>Beginning Node</td>
</tr>
<tr>
<td><span id="g3v">Node Value: ?</span></td>
<td>Beginning Node</td>
</tr>
<tr>
<td>Node.prev (nullptr)</td>
<td>Beginning Node</td>
</tr>
</tbody>
</table>
<h2 id="linked-list-ends">Linked List Ends</h2>
<p>Transcribers note: tell me if you like this way of transcribing simplified linked lists where the individual values dont matter, but only a linear relationship between the nodes:</p>
<p>For [4, 5, 6]:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mn>4</mn><mo></mo><mn>5</mn><mo></mo><mn>6</mn><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">6</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>versus</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mn>4</mn><mo></mo><mn>5</mn><mo></mo><mn>6</mn><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">6</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>For [5]:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mn>5</mn><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 5 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>versus</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo></mo><mn>5</mn><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} \leftrightarrow 5 \leftrightarrow \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<p>For []:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} \leftrightarrow \text{beginning node} \leftrightarrow \text{ending node} \leftrightarrow \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<p>versus (transcribers note: the lack of connection is intentional)</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo separator="true">,</mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} , \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<h2 id="end">End</h2>
<p>After the “End” slide, some previous slides are repeated for no apparent reason.</p>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

@ -0,0 +1,22 @@
#include <utility> // not in original, needed for compilation
template <typename Object>
class List
{
private:
// the basic doubly linked list node
// Nested inside a list, can be public
// because the node itself is private
struct Node
{
Object data; // (annotation: list element)
Node *prev; // (annotation: pointer to next node)
Node *next; // (annotation: pointer to previous node)
Node(const Object& d = Object{}, Node* p = nullptr, Node* n = nullptr)
: data{d}, prev{p}, next{n} {}
Node(Object&& d, Node* p = nullptr, Node* n = nullptr)
: data{std::move(d)}, prev{p}, next{n} {}
};
}; // not in original, needed for compilation

@ -0,0 +1,19 @@
/*
Transcriber's note: In the slide, the order of the functions is: main, f, g
But I have reordered them g, f, main to allow compilation.
*/
int g(int i){
return 2*i;
} // return control to f
int f(int c){
int d=3;
int e=g(4); // go to g
return e;
} // return control to main
int main(){
int a=1;
int b=f(2); // go to function f
return 0;
} // return from program

@ -0,0 +1,38 @@
/* A program to demonstrate printing out partial contents of the call stack */
#include <iostream> /* Note: only "#include" shown in image */
#include <iomanip> /* Note: only "#include" shwon in image */
using namespace std;
int print_stack(int k, int j){
cout << "print_stack() begins" << endl;
cout << "argument k is at &k=" << &k << " and k=" << k << endl;
cout << "argument j is at &j=" << &j << " and j=" << j << endl;
int CCC[2] = {77777777, 88888888};
cout << "Peeking from &j up, for the space of k ints" << endl;
int *p = (&j)+k;
for (int l=k; l>0; l--){
cout << p << ": " << setw(8) << hex << *p << " = " << setw(11) << dec << *p << endl;
p-=j;// subtracts j from an int pointer sets it to the j-th previous int
}
cout << "End of: print_stack()" << endl;
}
int ffff(int fun_arg){
cout << "fun() begins" << endl;
cout << "fun_arg is at &fun_arg=" << &fun_arg << endl;
int BBB[2] = {44444444,55555555};
cout << "BBB is at BBB=" << BBB << endl;
print_stack(40, +1);
cout << "fun ends" << endl;
}
int main(){
cout << "main() begins" << endl;
int XXXX = 99999999;
int AAAA[2] = {11111111,22222222};
ffff(33333333);
cout << "main() ends" << endl;
}

@ -0,0 +1,51 @@
main() begins
fun() begins
fun_arg is at &fun_arg=0xffffe9fe9e5c
BBB is at BBB=0xffffe9fe9e60
print_stack() begins
argument k is at &k=0xffffe9fe9e1c and k=40
argument j is at &j=0xffffe9fe9e18 and j=1
Peeking from &j up, for the space of k ints
0xffffe9fe9eb8: 0 = 0
0xffffe9fe9eb4: 0 = 0
0xffffe9fe9eb0: 0 = 0
0xffffe9fe9eac: aaaa = 43690
0xffffe9fe9ea8: b3e70b38 = -1276703944
0xffffe9fe9ea4: 0 = 0
0xffffe9fe9ea0: 0 = 0
0xffffe9fe9e9c: 195a3d0f = 425344271
0xffffe9fe9e98: 2b7fe700 = 729802496
0xffffe9fe9e94: 153158e = 22222222
0xffffe9fe9e90: a98ac7 = 11111111
0xffffe9fe9e8c: 5f5e0ff = 99999999
0xffffe9fe9e88: aba454fc = -1415293700
0xffffe9fe9e84: ffff = 65535
0xffffe9fe9e80: e9fe9ea0 = -369189216
0xffffe9fe9e7c: ffff = 65535
0xffffe9fe9e78: aba45538 = -1415293640
0xffffe9fe9e74: ffff = 65535
0xffffe9fe9e70: e9fe9ea0 = -369189216
0xffffe9fe9e6c: 195a3d0f = 425344271
0xffffe9fe9e68: 2b7fe700 = 729802496
0xffffe9fe9e64: 34fb5e3 = 55555555
0xffffe9fe9e60: 2a62b1c = 44444444
0xffffe9fe9e5c: 1fca055 = 33333333
0xffffe9fe9e58: b3e71074 = -1276702604
0xffffe9fe9e54: ffff = 65535
0xffffe9fe9e50: e9fe9e70 = -369189264
0xffffe9fe9e4c: aaaa = 43690
0xffffe9fe9e48: b3e710a4 = -1276702556
0xffffe9fe9e44: ffff = 65535
0xffffe9fe9e40: e9fe9e70 = -369189264
0xffffe9fe9e3c: 195a3d0f = 425344271
0xffffe9fe9e38: 2b7fe700 = 729802496
0xffffe9fe9e34: 54c5638 = 88888888
0xffffe9fe9e30: 4a2cb71 = 77777777
0xffffe9fe9e2c: ffff = 65535
0xffffe9fe9e28: e9fe9e28 = -369189336
0xffffe9fe9e24: 3 = 3
0xffffe9fe9e20: e9fe9e40 = -369189312
0xffffe9fe9e1c: 28 = 40
End of: print_stack()
fun ends
main() ends

@ -0,0 +1,15 @@
#include "dsexception.h" // transcriber's note: I have no idea where this file is or what it does???
#include "List.h"
using namespace std;
int main()
{
const int N = 5;
List<int> lst;
for (int i=N-1; i>0; --i)
{
lst.push_forward(i);
}
return 0;
}

@ -0,0 +1,357 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
<link rel="stylesheet" href="/assets/css/katex.css" id="math-css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="ll-queues--ll-traversal">L.L. Queues &amp; L.L. Traversal</h1>
<h2 id="basic-ll-queue">Basic L.L. Queue</h2>
<ul>
<li><a href="#frontele1">front</a></li>
<li><a href="#backele1">back</a></li>
</ul>
<table>
<thead>
<tr>
<th>Value</th>
<th>Next</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="frontele1">a</span></td>
<td><a href="#ele11">pointer</a></td>
</tr>
<tr>
<td><span id="ele11">b</span></td>
<td><a href="#ele12">pointer</a></td>
</tr>
<tr>
<td><span id="ele12"></span></td>
<td></td>
</tr>
<tr>
<td><span id="ele13">g</span></td>
<td><a href="#backele1">pointer</a></td>
</tr>
<tr>
<td><span id="backele1">h</span></td>
<td>nullptr</td>
</tr>
</tbody>
</table>
<p>as a stack, this L.L. would be:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>a</mi><mo></mo><mi>b</mi><mo></mo><mo></mo><mo></mo><mi>h</mi></mrow><annotation encoding="application/x-tex">a \rightarrow b \rightarrow \dots \rightarrow h</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.36687em;vertical-align:0em;"></span><span class="minner"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">h</span></span></span></span></span>
<p>Pseudo-code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class Node{
Type data
Node * next
}
</code></pre></div></div>
<h2 id="enqueue--dequeue--first--versions">Enqueue + Dequeue First Versions</h2>
<p>Variables:</p>
<ul>
<li>float (pointer to stack)</li>
<li>back (pointer to stack)</li>
<li>size (# elements in queue)</li>
</ul>
<h3 id="dequeue">Dequeue:</h3>
<p>Pseudo-code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>dequeue(){
temp = front
val = front-&gt;data
front = front-&gt;next
delete temp
size = size-1
return val
}
</code></pre></div></div>
<p>Diagram:</p>
<ul>
<li><del><a href="#frontele2">front</a></del>
<ins><a href="#ele21">front</a></ins></li>
<li><a href="#backele2">back</a></li>
<li><a href="#frontele2">temp</a></li>
</ul>
<table>
<thead>
<tr>
<th>Value</th>
<th>Next</th>
</tr>
</thead>
<tbody>
<tr>
<td><del><span id="frontele2">a</span></del></td>
<td><del><a href="#ele21">pointer</a></del></td>
</tr>
<tr>
<td><span id="ele21">b</span></td>
<td><a href="#ele22">pointer</a></td>
</tr>
<tr>
<td><span id="ele22"></span></td>
<td></td>
</tr>
<tr>
<td><span id="ele13">g</span></td>
<td><a href="#backele2">pointer</a></td>
</tr>
<tr>
<td><span id="backele2">h</span></td>
<td>nullptr</td>
</tr>
</tbody>
</table>
<h3 id="unqueue">Unqueue</h3>
<p>Pseudo-code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>enqueue(x){
n = new node containing x
back-&gt;next = n
back = back-&gt;next
size = size+1
}
</code></pre></div></div>
<p>Diagram:</p>
<ul>
<li><a href="#frontele3">front</a></li>
<li><del><a href="#backele3">back</a></del>
<ins><a href="#backele32">back</a></ins></li>
</ul>
<table>
<thead>
<tr>
<th>Value</th>
<th>Next</th>
</tr>
</thead>
<tbody>
<tr>
<td><span id="frontele3">a</span></td>
<td><a href="#ele31">pointer</a></td>
</tr>
<tr>
<td><span id="ele31">b</span></td>
<td><a href="#ele32">pointer</a></td>
</tr>
<tr>
<td><span id="ele32"></span></td>
<td></td>
</tr>
<tr>
<td><span id="ele33">g</span></td>
<td><a href="#backele3">pointer</a></td>
</tr>
<tr>
<td><span id="backele3">h</span></td>
<td><del>nullptr</del> <ins><a href="#backele32">pointer</a></ins></td>
</tr>
<tr>
<td><ins><span id="backele32">x</span></ins></td>
<td><ins>nullptr</ins></td>
</tr>
</tbody>
</table>
<h2 id="empty-queue">Empty Queue</h2>
<p>A stack with a</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>a</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow a \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>After dequeue</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo separator="true">,</mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front}, \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>After enqueue(b)</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Enqueue &amp; Dequeue are different from empty/non-empty queues.</p>
<h2 id="enqueue">Enqueue</h2>
<p>Example 1: Empty queue</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo separator="true">,</mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front}, \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Enqueue b</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Example 2: Starter queue</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mo></mo><mo></mo><mi>x</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow \dots \rightarrow x \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.36687em;vertical-align:0em;"></span><span class="minner"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>enqueue b</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mo></mo><mo></mo><mi>x</mi><mo></mo><mi>b</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow \dots \rightarrow x \rightarrow b \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.36687em;vertical-align:0em;"></span><span class="minner"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Pseudo code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>enqueue(x){
if size &gt; 0{
back-&gt;next = new Node(x)
back = back-&gt;next
} else {
back = new Node(x)
front = back
}
size = size+1
}
</code></pre></div></div>
<h2 id="dequeue-1">Dequeue</h2>
<p>Pseudo code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>front = front-&gt;next
return value
</code></pre></div></div>
<p>That code has the following effect upon a queue.</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>a</mi><mo></mo><mi>b</mi><mo></mo><mi>c</mi><mo></mo><mi>d</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow a \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>becomes</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mi>c</mi><mo></mo><mi>d</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>while returning a.</p>
<h3 id="example-1">Example 1:</h3>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>dequeue (b)</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo separator="true">,</mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front}, \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<h3 id="example-2">Example 2:</h3>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>b</mi><mo></mo><mi>c</mi><mo></mo><mi>d</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>dequeue</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>front</mtext><mo></mo><mi>c</mi><mo></mo><mi>d</mi><mo></mo><mtext>back</mtext></mrow><annotation encoding="application/x-tex">\text{front} \rightarrow c \rightarrow d \leftarrow \text{back}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">front</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">back</span></span></span></span></span></span>
<p>Return b</p>
<h3 id="pseudo-code">Pseudo code</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>dequeue(){
temp = front
val = front-&gt;data
front = front-&gt;next
delete temp
size = size-1
return val
}
</code></pre></div></div>
<h2 id="traversing-the-list">Traversing the List</h2>
<p>Pseudo code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>displayList(){
Node * cur = front;
while(cur!=nullptr){
output cur-&gt;data
cur = cur-&gt;next
}
}
</code></pre></div></div>
<p>A diagram which shows the cursor going from c, to d to be (in order from front to back).</p>
<h2 id="linked-list-ends">Linked List Ends</h2>
<p>Transcribers note: this is the same as in 06. The left/right arrows denote a double linked list, even though the current set of slides is talking about only singly linked lists.</p>
<p>For [4, 5, 6]:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mn>4</mn><mo></mo><mn>5</mn><mo></mo><mn>6</mn><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">6</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>versus</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mn>4</mn><mo></mo><mn>5</mn><mo></mo><mn>6</mn><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">6</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>For [5]:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mn>5</mn><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">
\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 5 \leftrightarrow \text{ending node} \leftrightarrow \text{tail}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span></p>
<p>versus</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo></mo><mn>5</mn><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} \leftrightarrow 5 \leftrightarrow \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<p>For []:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo></mo><mtext>beginning node</mtext><mo></mo><mtext>ending node</mtext><mo></mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} \leftrightarrow \text{beginning node} \leftrightarrow \text{ending node} \leftrightarrow \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">beginning node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">ending node</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<p>versus</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>head</mtext><mo separator="true">,</mo><mtext>tail</mtext></mrow><annotation encoding="application/x-tex">\text{head} , \text{tail}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">head</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord text"><span class="mord">tail</span></span></span></span></span></span>
<h2 id="list-class-traversing-the-list">List Class: Traversing the List</h2>
<p>Pseudo code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>displayList(){
Node * cur = head-&gt;next
while(cur!=tail){
output cur-&gt;data
cur = cur-&gt;next
}
}
</code></pre></div></div>
<p>Graphic of <code class="language-plaintext highlighter-rouge">cur</code> moving through each (empty) element of the queue.</p>
<h2 id="end">End</h2>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -0,0 +1,177 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
</head>
<body>
<main>
<div id="wrapper">
<aside>Hello everyone!</aside>
<h1 id="welcome-to-cmpt-295">Welcome to CMPT 295</h1>
<h2 id="introduction-to-computer-systems">Introduction to Computer Systems</h2>
<p>My name is Anne Lavergne</p>
<p>Lecture 1 Course Overview + Activity</p>
<aside>How does it feel to be back on campus?</aside>
<h2 id="todays-menu-1">Todays Menu [1]</h2>
<ul>
<li>COVID Protocol</li>
<li>What is CMPT 295?
<ul>
<li>What shall we learn in CMPT 295?</li>
<li>What should we already know?</li>
<li>Which resources do we have to help us learn all this?</li>
</ul>
</li>
<li>Activity</li>
<li>Questions</li>
</ul>
<h2 id="covid-protocol--about-masks-2">COVID protocol About masks! [2]</h2>
<p>Here is a message from Elizabeth Elle, SFU Vice Provost Learning &amp; Teaching, based on the public health order:</p>
<ul>
<li>Unless we have an approved exemption, we are required to wear a mask in all indoor common and learning spaces, including classrooms. Please come to campus prepared with a non-medical mask.
<ul>
<li>If we forget our mask, disposable masks are available from Student Central in Burnaby and at the information desks in Vancouver and Surrey.</li>
<li>If we require a mask exemption in the classroom for medical reasons, please contact the Centre for Accessible Learning at cal_admin@sfu.ca for assistance.</li>
<li>If we are requesting mask exemptions on other protected grounds, such as religion, we can contact the Office of Student Support, Rights and Responsibilities at student_support@sfu.ca.</li>
</ul>
</li>
<li>And please remember to be kind to each other. If we see someone not wearing a mask, do not make assumptions or judgments as that person may be exempt.</li>
</ul>
<h2 id="what-is-cmpt-295-3">What is CMPT 295? [3]</h2>
<ul>
<li>The goal of this course is to give us, software developers, a look “under the hood” of a computer, i.e., to learn about Computer Systems =&gt; microprocessor, memory, … <img src="" alt="a car with its hood up." /></li>
<li>This knowledge will allow us to become more efficient software developers</li>
</ul>
<h2 id="the-big-picture-4">The big picture: [4]</h2>
<p>In CMPT 295, we shall learn …</p>
<ul>
<li>C programs (.c) How our code and data are represented in memory</li>
<li>Assembly programs (.s) How a compiler transforms our code into machine executable code in several steps</li>
<li>Object (.o) file an executable How a compiler optimizes (or not) our code</li>
<li>Computer executes it How a microprocessor is designed and how it executes our code</li>
<li>CPU, Memory How memory is designed</li>
</ul>
<p>How all of this can impact the execution of our code How to write more efficient and reliable code:</p>
<ul>
<li>Be able to find and eliminate bugs
more efficiently</li>
<li>Be able to ascertain program performance and tune it by optimizing our code</li>
</ul>
<h2 id="what-should-we-already-know-5">What should we already know? [5]</h2>
<ul>
<li>Write correct C programs
<ul>
<li>C constructs (variables, data types, pointers, if/else, switch/case, for/while/do while, function calls, arrays, …)</li>
</ul>
</li>
<li>What a stack is and how it works</li>
<li>Binary/decimal/hexadecimal numeral systems
<ul>
<li>How to convert from one numeral system to the others</li>
<li>Basic arithmetic</li>
</ul>
</li>
<li>Perform Boolean algebra using and, or, not, xor</li>
</ul>
<h2 id="which-resources-do-we-have-6">Which resources do we have? [6]</h2>
<ul>
<li>Course web site
https://www2.cs.sfu.ca/CourseCentral/295/alavergn/index.html</li>
<li>Textbook
<ul>
<li>Computer Systems: A Programmers Perspective, 3/E, Randal E. Bryant, David R. OHallaron, Pearson, 2016</li>
</ul>
</li>
<li>Labs in CSIL (Computing Science Instructional Lab)
<ul>
<li>Target Machine: CSIL workstation
<ul>
<li>Linux platform (or OS)</li>
<li>C programming language</li>
<li>x86-64 assembly language</li>
<li>gcc compiler</li>
</ul>
</li>
</ul>
</li>
<li>Instructor and TAs - Office hours</li>
</ul>
<h2 id="activity---discover-our-resources-7">Activity - Discover our resources [7]</h2>
<p>Instructions:</p>
<ol>
<li>Form teams of 3 to 4</li>
<li>Do Lecture 1 Activity on CourSys</li>
<li>Time: about 30 minutes</li>
</ol>
<h2 id="question-8">Question? [8]</h2>
<p>Blank page.</p>
<h2 id="summary-9">Summary [9]</h2>
<ul>
<li>COVID Protocol</li>
<li>What is CMPT 295?
<ul>
<li>What shall we learn in CMPT 295?</li>
<li>What should we already know?</li>
<li>Which resources do we have to help us learn all this?</li>
</ul>
</li>
<li>Activity</li>
<li>Questions</li>
</ul>
<h2 id="next-lecture-10">Next Lecture [10]</h2>
<ul>
<li>Data Representation
<ul>
<li>Representing information as bits</li>
</ul>
</li>
<li>To get ready for our next lecture:
<ul>
<li>Optional: Read Chapter 1 of textbook</li>
<li>Not so optional: Read Section 2.1 of Chapter 2</li>
<li>Download the partial lecture notes found under the column Lecture in the table on our course web site</li>
</ul>
</li>
</ul>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

@ -0,0 +1,22 @@
Title: Activity - Code
Video of CERT Secure Coding Initiative Conference 2015 - Robert C. Seacord
https://www.youtube.com/watch?v=1ew0GvB3NpE
Blue box with code in it:
char *copy (size_t n, const char *a) {
if (n == 0) return NULL;
if (a == NULL) return NULL;
char *p = (char *)malloc(n);
if (p == NULL) return NULL;
for (int i = 0; i < n; i++) p[i] = *a++;
return p;
}
White text over-laying the blue box: Spot the defect

@ -0,0 +1,4 @@
Computer Bus
Two images side by side,
Left image of a ribbon cable,
right image of a specialty device with many ribbon cables attached to it.

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

@ -0,0 +1,590 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
<link rel="stylesheet" href="/assets/css/katex.css" id="math-css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="cmpt-295">CMPT 295</h1>
<p>Unit - Data Representation</p>
<p>Lecture 4 Representing integral numbers in memory Arithmetic operations</p>
<h2 id="warm-up-question">Warm up question</h2>
<ul>
<li>What is the value of …
<ul>
<li>TMin (in hex) for signed char in C: _________________</li>
<li>TMax (in hex) for signed int in C: _________________</li>
<li>TMin (in hex) for signed short in C: ________________</li>
</ul>
</li>
</ul>
<h2 id="last-lecture">Last Lecture</h2>
<ul>
<li>Interpretation of bit pattern B into either unsigned value U or signed value T
<ul>
<li>B2U(X) and U2B(X) encoding schemes (conversion)</li>
<li>B2T(X) and T2B(X) encoding schemes (conversion)
<ul>
<li>Signed value expressed as twos complement =&gt; T</li>
</ul>
</li>
</ul>
</li>
<li>Conversions from unsigned &lt;-&gt; signed values
<ul>
<li>U2T(X) and T2U(X) =&gt; adding or subtracting 2w</li>
</ul>
</li>
<li>Implication in C: when converting (implicitly via promotion and explicitly via casting):
<ul>
<li>Sign:
<ul>
<li>Unsigned &lt;-&gt; signed (of same size) -&gt; Both have same bit pattern, however, this bit pattern may be interpreted differently
<ul>
<li>Can have unexpected effects -&gt; producing a different value</li>
</ul>
</li>
</ul>
</li>
<li>Size:
<ul>
<li>Small -&gt; large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int)
<ul>
<li>sign extension: For unsigned -&gt; zeros extension, for signed -&gt; sign bit extension</li>
<li>Both yield expected result &gt; resulting value unchanged</li>
</ul>
</li>
<li>Large -&gt; small (for signed, e.g., int to short and for unsigned, e.g., unsigned int to unsigned short)
<ul>
<li>truncation: Unsigned/signed -&gt; most significant bits are truncated (discarded)</li>
<li>May not yield expected results -&gt; original value may be altered</li>
</ul>
</li>
</ul>
</li>
<li>Both (sign and size): 1) size conversion is first done then 2) sign conversion is done</li>
</ul>
</li>
</ul>
<h2 id="todays-menu">Todays Menu</h2>
<ul>
<li>Representing data in memory Most of this is review
<ul>
<li>“Under the Hood” - Von Neumann architecture</li>
<li>Bits and bytes in memory
<ul>
<li>How to diagram memory -&gt; Used in this course and other references</li>
<li>How to represent series of bits -&gt; In binary, in hexadecimal (conversion)</li>
<li>What kind of information (data) do series of bits represent -&gt; Encoding scheme</li>
<li>Order of bytes in memory -&gt; Endian</li>
</ul>
</li>
<li>Bit manipulation bitwise operations
<ul>
<li>Boolean algebra + Shifting</li>
</ul>
</li>
</ul>
</li>
<li>Representing integral numbers in memory
<ul>
<li>Unsigned and signed</li>
<li>Converting, expanding and truncating</li>
<li>Arithmetic operations</li>
</ul>
</li>
<li>Representing real numbers in memory
<ul>
<li>IEEE floating point representation</li>
<li>Floating point in C casting, rounding, addition, …</li>
</ul>
</li>
</ul>
<p>Lets first illustrate what we covered last lecture with a demo!</p>
<h2 id="demo--looking-at-size-and-sign-conversions-in-c">Demo Looking at size and sign conversions in C</h2>
<ul>
<li>What does the demo illustrate?
<ul>
<li>Size conversion:
<ul>
<li>Converting to a larger (wider) data type -&gt; Converting short to int</li>
<li>Converting to a smaller (narrower) data type -&gt; Converting short to char</li>
</ul>
</li>
<li>Sign conversion:
<ul>
<li>Converting from signed to unsigned -&gt; Converting short to unsigned short</li>
<li>Converting from unsigned to signed -&gt; Converting unsigned short to short</li>
</ul>
</li>
<li>Size and Sign conversion:
<ul>
<li>Converting from signed to unsigned larger (wider) data type -&gt; Converting short to unsigned int</li>
<li>Converting from signed to unsigned smaller (narrower) data type -&gt;
Converting short to unsigned char</li>
</ul>
</li>
</ul>
</li>
<li>This demo (code and results) posted on our course web site</li>
</ul>
<h2 id="integer-addition-unlimited-space">Integer addition (unlimited space)</h2>
<ul>
<li>
<p>What happens when we add two decimal numbers? (Highlights show carring the one to complete the equation)
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>107</mtext><mn>10</mn></msub><mo>+</mo><msub><mtext>938</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>1045</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">
\text{107}_{10} + \text{938}_{10} = \text{1045}_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">107</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">938</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">1045</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
</li>
<li>
<p>Same thing happens when we add two binary numbers:</p>
</li>
</ul>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>101100</mtext><mn>2</mn></msub><mo>+</mo><msub><mtext>101110</mtext><mn>2</mn></msub><mo>=</mo><msub><mtext>1011010</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">
\text{101100}_{2} + \text{101110}_{2} = \text{1011010}_{2}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">101100</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">101110</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">1011010</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<h2 id="unsigned-addition-limited-space-ie-fixed-size-in-memory">Unsigned addition (limited space, i.e., fixed size in memory)</h2>
<p>What happens when we add two unsigned values?</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>w</mi><mo>=</mo><mn>8</mn></mrow><annotation encoding="application/x-tex">w=8</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">8</span></span></span></span></span>
<p>a)</p>
<p>Binary:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>00111011</mtext><mn>2</mn></msub><mo>+</mo><msub><mtext>01011010</mtext><mn>2</mn></msub><mo>=</mo><mo stretchy="false">?</mo></mrow><annotation encoding="application/x-tex">
\text{00111011}_{2} + \text{01011010}_{2} = ?
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">00111011</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">01011010</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mclose">?</span></span></span></span></span></p>
<p>Decimal:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>59</mtext><mn>10</mn></msub><mo>+</mo><msub><mtext>90</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>149</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">
\text{59}_{10} + \text{90}_{10} = \text{149}_{10}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">59</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">90</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">149</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<p>b)</p>
<p>Binary:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>10101110</mtext><mn>2</mn></msub><mo>+</mo><msub><mtext>11001011</mtext><mn>2</mn></msub><mo>=</mo><mo stretchy="false">?</mo></mrow><annotation encoding="application/x-tex">
\text{10101110}_{2} + \text{11001011}_{2} = ?
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">10101110</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">11001011</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mclose">?</span></span></span></span></span></p>
<p>Decimal:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>174</mtext><mn>10</mn></msub><mo>+</mo><msub><mtext>203</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>377</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">
\text{174}_{10} + \text{203}_{10} = \text{377}_{10}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">174</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">203</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">377</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<h2 id="unsigned-addition-u_w-and-overflow">Unsigned addition (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mo>+</mo><mi>w</mi><mi>u</mi></msubsup></mrow><annotation encoding="application/x-tex">+^{u}_{w}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.911392em;vertical-align:-0.247em;"></span><span class="mord"><span class="mbin">+</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">u</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span></span></span></span>) and overflow</h2>
<ul>
<li>True/expected sum is the result of integer addition with unlimited space.</li>
<li>Actual sum is the result of unsigned addition with limited space. Discarding the carry out bit.</li>
<li>Discarding carry out bit has same effect as applying modular arithmetic</li>
</ul>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mo>=</mo><mi>U</mi><msubsup><mo>+</mo><mi>w</mi><mi>u</mi></msubsup><mi>v</mi><mo>=</mo><mo stretchy="false">(</mo><mi>u</mi><mo>+</mo><mi>v</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="1em"></mspace><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup></mrow><annotation encoding="application/x-tex">
s = U +^{u}_{w}v = (u + v) \mod{2^{w}}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">s</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.9613919999999999em;vertical-align:-0.247em;"></span><span class="mord mathdefault" style="margin-right:0.10903em;">U</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"><span class="mbin">+</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight">u</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">v</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathdefault">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">v</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:1em;"></span></span><span class="base"><span class="strut" style="height:0.7143919999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">m</span><span class="mord mathrm">o</span><span class="mord mathrm">d</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<ul>
<li>Operands: w bits</li>
<li>True Sum: w+1 bits</li>
</ul>
<h2 id="closer-look-at-unsigned-addition-overflow">Closer look at unsigned addition overflow</h2>
<ul>
<li>w = 8 -&gt; [0..255]</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mtext>255</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>11111111</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{255}_{10} = \text{11111111}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">255</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">11111111</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mtext>90</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>01011010</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{90}_{10} = \text{01011010}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">90</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">01011010</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mtext>45</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>00101101</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{45}_{10} = \text{00101101}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">45</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">00101101</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
</ul>
<h3 id="example-1">Example 1:</h3>
<p>Decimal (carry out the 1 in 135):</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>90</mtext><mn>10</mn></msub><mo>+</mo><msub><mtext>45</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>135</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">
\text{90}_{10} + \text{45}_{10} = \text{135}_{10}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">90</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">45</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">135</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<p>Binary (carry out the 1 in <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>10000111</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{10000111}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">10000111</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>):</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>01011010</mtext><mn>2</mn></msub><mo>+</mo><msub><mtext>00101101</mtext><mn>2</mn></msub><mo>=</mo><msub><mtext>10000111</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">
\text{01011010}_{2} + \text{00101101}_{2} = \text{10000111}_{2}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">01011010</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">00101101</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">10000111</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<ul>
<li>True sum, w=8: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>10000111</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{10000111}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">10000111</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></li>
<li>Actual (overflowed) sum, w=8: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>10000111</mtext><mn>2</mn></msub><mo>=</mo><msub><mtext>135</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">\text{10000111}_{2} = \text{135}_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">10000111</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">135</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></li>
</ul>
<h3 id="example-2">Example 2:</h3>
<p>Decimal (carry 1 to the 3 in 300):</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>255</mtext><mn>10</mn></msub><mo>+</mo><msub><mtext>45</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>300</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">
\text{255}_{10} + \text{45}_{10} = \text{300}_{10}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">255</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">45</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">300</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<p>Binary (carry the 1 at the beginning of the result):</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>11111111</mtext><mn>2</mn></msub><mo>+</mo><msub><mtext>00101101</mtext><mn>2</mn></msub><mo>=</mo><msub><mtext>100101100</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">
\text{11111111}_{2} + \text{00101101}_{2} = \text{100101100}_{2}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">11111111</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">00101101</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">100101100</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<ul>
<li>True sum, w=9: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>100101100</mtext><mn>2</mn></msub><mo>=</mo><msub><mtext>300</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">\text{100101100}_{2} = \text{300}_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">100101100</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">300</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></li>
<li>Actual (overflowed) sum, w=8: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>00101100</mtext><mn>2</mn></msub><mo>=</mo><msub><mtext>44</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">\text{00101100}_{2} = \text{44}_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">00101100</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">44</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></li>
</ul>
<h2 id="comparing-integer-addition-with-overflow-effect-of-unsigned-addition-w--4">Comparing integer addition with Overflow: Effect of unsigned addition (w = 4)</h2>
<p>Annotation: with unlimited space:</p>
<p>A 3d chart showing two numbers being added.
a and b on the z and x axies, the sum on the y axis.
y goes to a maximum height of 32
(a = 15) + (b = 15) = (y = 30)
Annotation: With limited space (fixed-size memory):</p>
<p>A 3d chart showing two numbers being added.
a and b on the z and x axies, the sum on the y axis.
y goes to a maximum height of 15
(a = 15) + (b = 15) = (y = 14)</p>
<p>An overflow occurs when there is a carry out</p>
<p>For example: 15 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>1111</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{1111}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">1111</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) + 15 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>1111</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{1111}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">1111</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) = 30 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>11110</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{11110}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">11110</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) as a true sum, and 14 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>11110</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{11110}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">11110</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) as an actual sum.</p>
<h2 id="signed-addition-limited-space-ie-fixed-size-in-memory">Signed addition (limited space, i.e., fixed size in memory)</h2>
<p>What happens when we add two signed values:</p>
<p>w=8</p>
<p>a)</p>
<p>Binary:
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>00111011</mtext><mn>2</mn></msub><mo>+</mo><msub><mtext>01011010</mtext><mn>2</mn></msub><mo>=</mo><mo stretchy="false">?</mo></mrow><annotation encoding="application/x-tex">\text{00111011}_{2} + \text{01011010}_{2} = ?</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">00111011</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">01011010</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mclose">?</span></span></span></span></p>
<p>Decimal:
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>59</mtext><mn>10</mn></msub><mo>+</mo><msub><mtext>90</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>149</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">\text{59}_{10} + \text{90}_{10} = \text{149}_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">59</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">90</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">149</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>b)</p>
<p>Binary: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>10101110</mtext><mn>2</mn></msub><mo>+</mo><msub><mtext>11001011</mtext><mn>2</mn></msub><mo>=</mo><mo stretchy="false">?</mo></mrow><annotation encoding="application/x-tex">\text{10101110}_{2} + \text{11001011}_{2} = ?</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">10101110</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">11001011</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mclose">?</span></span></span></span></p>
<p>Decimal: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>-82</mtext><mn>10</mn></msub><mo>+</mo><msub><mtext>-53</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>-135</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">\text{-82}_{10} + \text{-53}_{10} = \text{-135}_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">-82</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">-53</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">-135</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>Observation: Unsigned and signed additions have identical behavior @ the bit level, i.e., their sum have the same bit-level representation, but their interpretation differs</p>
<h2 id="signed-addition-t_w-and-overflow">Signed addition (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mo>+</mo><mi>w</mi><mi>t</mi></msubsup></mrow><annotation encoding="application/x-tex">+^{t}_{w}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.040556em;vertical-align:-0.247em;"></span><span class="mord"><span class="mbin">+</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.7935559999999999em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">t</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span></span></span></span>) and overflow</h2>
<p>True sum would be the result of integer addition with unlimited space:
Actual sum is the result of signed addition with limited space:</p>
<p>Operands: w bits
True Sum: w+1 bits</p>
<p>After discarduing carry out bit: w bits (overflow)</p>
<ul>
<li>Discarding carry out bit has same effect as applying modular arithmetic</li>
</ul>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mo>=</mo><mi>u</mi><msubsup><mo>+</mo><mi>w</mi><mi>t</mi></msubsup><mi>v</mi><mo>=</mo><msub><mtext>U2T</mtext><mi>w</mi></msub><mo stretchy="false">[</mo><mo stretchy="false">(</mo><mi>u</mi><mo>+</mo><mi>v</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="1em"></mspace><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">
s = u +^{t}_{w} v = \text{U2T}_{w}[(u + v) \mod 2^{w}]
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">s</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.0905559999999999em;vertical-align:-0.247em;"></span><span class="mord mathdefault">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"><span class="mbin">+</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.8435559999999999em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight">t</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">v</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord text"><span class="mord">U2T</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.151392em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.02691em;">w</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mopen">[</span><span class="mopen">(</span><span class="mord mathdefault">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">v</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:1em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">m</span><span class="mord mathrm">o</span><span class="mord mathrm">d</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span><span class="mclose">]</span></span></span></span></span></p>
<p>Negative overflow and positive overflows are possible.
Diagram showing negative overflows becoming positive, and positive overflows becoming negative.</p>
<h2 id="closer-look-at-signed-addition-overflow">Closer look at signed addition overflow</h2>
<ul>
<li>w = 8 -&gt; [-128..127]</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mtext>90</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>01011010</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{90}_{10} = \text{01011010}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">90</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">01011010</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mtext>45</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>00101101</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{45}_{10} = \text{00101101}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">45</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">00101101</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mtext>-45</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>11010011</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{-45}_{10} = \text{11010011}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">-45</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">11010011</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mtext>-90</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>10100110</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{-90}_{10} = \text{10100110}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">-90</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">10100110</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
</ul>
<h3 id="example-1-1">Example 1:</h3>
<p>Decimal: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>90</mtext><mn>10</mn></msub><mo>+</mo><msub><mtext>45</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>135</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">\text{90}_{10} + \text{45}_{10} = \text{135}_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">90</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">45</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">135</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>Binary: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>01011010</mtext><mn>2</mn></msub><mo>+</mo><msub><mtext>00101101</mtext><mn>2</mn></msub><mo>=</mo><msub><mtext>010000111</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{01011010}_{2} + \text{00101101}_{2} = \text{010000111}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">01011010</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">00101101</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">010000111</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>The binary result is -121</p>
<h3 id="example-2-1">Example 2:</h3>
<p>Decimal: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>-90</mtext><mn>10</mn></msub><mo>+</mo><msub><mtext>-45</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>-135</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">\text{-90}_{10} + \text{-45}_{10} = \text{-135}_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">-90</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">-45</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">-135</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>Binary: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>10100110</mtext><mn>2</mn></msub><mo>+</mo><msub><mtext>11010011</mtext><mn>2</mn></msub><mo>=</mo><msub><mtext>101111001</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{10100110}_{2} + \text{11010011}_{2} = \text{101111001}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">10100110</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">11010011</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">101111001</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>Binary result is 121</p>
<h3 id="example-3">Example 3:</h3>
<p>Decimal: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>-90</mtext><mn>10</mn></msub><mo>+</mo><msub><mtext>45</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>-45</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">\text{-90}_{10} + \text{45}_{10} = \text{-45}_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">-90</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">45</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">-45</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>Binary: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>10100110</mtext><mn>2</mn></msub><mo>+</mo><msub><mtext>00101101</mtext><mn>2</mn></msub><mo>=</mo><msub><mtext>011010011</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{10100110}_{2} + \text{00101101}_{2} = \text{011010011}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">10100110</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">00101101</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">011010011</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<h3 id="example-4">Example 4:</h3>
<p>Decimal: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>90</mtext><mn>10</mn></msub><mo>+</mo><msub><mtext>-45</mtext><mn>10</mn></msub><mo>=</mo><msub><mtext>45</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">\text{90}_{10} + \text{-45}_{10} = \text{45}_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">90</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">-45</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">45</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>Binary: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>01011010</mtext><mn>2</mn></msub><mo>+</mo><msub><mtext>11010011</mtext><mn>2</mn></msub><mo>=</mo><msub><mtext>100101101</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{01011010}_{2} + \text{11010011}_{2} = \text{100101101}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">01011010</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">11010011</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">100101101</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>A chart showing the relationship between true sum and actual (overflowed) sum.
Actual sum has a possible value between 127 to -128.
A true sum, however has a range between 255 and -256.
As your true sum goes further down from -128, its actual sum becomes lower as well. Starting at -129 = 127.
As you true sum goes futher up from 127, the actual sum also rises, satrting with 128 = -128.</p>
<h2 id="visualizing-signed-addition-overflow-w--4">Visualizing signed addition overflow (w = 4)</h2>
<p>A 3D chart which has its x axis go from -8 to +7, its z axis go from -8 to +6, and a y axis which goes from :wq</p>
<p>Positive Overflow</p>
<p>For example: 7 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>0111</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{0111}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">0111</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) + 1 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>0001</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{0001}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">0001</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) = 8 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>1000</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{1000}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">1000</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) is the true sum and = -8 (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>1000</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{1000}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">1000</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>) is the actual sum</p>
<h2 id="what-about-subtraction---addition">What about subtraction? -&gt; Addition</h2>
<p>x + (-x) = 0</p>
<ul>
<li>Subtracting a number is equivalent to adding its additive inverse
<ul>
<li>Instead of subtracting a positive number, we could add its negative version:</li>
</ul>
</li>
</ul>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>107</mn><mo></mo><mn>118</mn><mo>=</mo><mo></mo><mn>11</mn></mrow><annotation encoding="application/x-tex">
107 - 118 = -11
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1</span><span class="mord">0</span><span class="mord">7</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord">1</span><span class="mord">8</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord"></span><span class="mord">1</span><span class="mord">1</span></span></span></span></span></p>
<p>becomes:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>107</mn><mo>+</mo><mo stretchy="false">(</mo><mo></mo><mn>118</mn><mo stretchy="false">)</mo><mo>=</mo><mo></mo><mn>18</mn></mrow><annotation encoding="application/x-tex">
107 + (-118) = -18
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1</span><span class="mord">0</span><span class="mord">7</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord"></span><span class="mord">1</span><span class="mord">1</span><span class="mord">8</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord"></span><span class="mord">1</span><span class="mord">8</span></span></span></span></span></p>
<ul>
<li>Lets try:</li>
</ul>
<p>Decimal:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mtext>107</mtext><mn>10</mn></msub><mo></mo><msub><mtext>118</mtext><mn>10</mn></msub><mo>=</mo><mo></mo><mn>11</mn></mrow><annotation encoding="application/x-tex">\text{107}_{10} - \text{118}_{10} = -11</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">107</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">118</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord"></span><span class="mord">11</span></span></span></span></span>
<p>Binary:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mtext>01101011</mtext><mn>2</mn></msub><mo></mo><msub><mtext>01110110</mtext><mn>2</mn></msub><mo>=</mo></mrow><annotation encoding="application/x-tex">\text{01101011}_{2} - \text{01110110}_{2} =</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">01101011</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">01110110</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span></span></span></span></span>
<p>Binary subtraction by addition:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mtext>01101011</mtext><mn>2</mn></msub><mo>+</mo><msub><mtext>10001010</mtext><mn>2</mn></msub><mo>=</mo><msub><mtext>11110101</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{01101011}_{2} + \text{10001010}_{2} = \text{11110101}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">01101011</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">10001010</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">11110101</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
<p>Binary subtraction by addition is equal to -11</p>
<p>Check: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo></mo><mn>128</mn><mo>+</mo><mn>64</mn><mo>+</mo><mn>32</mn><mo>+</mo><mn>16</mn><mo>+</mo><mn>4</mn><mo>+</mo><mn>1</mn><mo>=</mo><msub><mtext>-11</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">-128 + 64 + 32 + 16 + 4 + 1 = \text{-11}_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord"></span><span class="mord">128</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">64</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">32</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">16</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">4</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">-11</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></p>
<p>T2B(X) conversion:</p>
<ol>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo stretchy="false">(</mo><mtext> </mtext><mo stretchy="false">(</mo><mtext>U2B</mtext><mo stretchy="false">(</mo><mi mathvariant="normal"></mi><mi>X</mi><mi mathvariant="normal"></mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">(~(\text{U2B}(|X|)))+1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mspace nobreak"> </span><span class="mopen">(</span><span class="mord text"><span class="mord">U2B</span></span><span class="mopen">(</span><span class="mord"></span><span class="mord mathnormal" style="margin-right:0.07847em;">X</span><span class="mord"></span><span class="mclose">)))</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo stretchy="false">(</mo><mtext> </mtext><mo stretchy="false">(</mo><mtext>U2B</mtext><mo stretchy="false">(</mo><mi mathvariant="normal"></mi><mo></mo><mn>118</mn><mi mathvariant="normal"></mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">(~(\text{U2B}(|-118|)))+1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mspace nobreak"> </span><span class="mopen">(</span><span class="mord text"><span class="mord">U2B</span></span><span class="mopen">(</span><span class="mord"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">118</span><span class="mclose">)))</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo stretchy="false">(</mo><mtext> </mtext><mo stretchy="false">(</mo><mtext>U2B</mtext><mo stretchy="false">(</mo><mn>118</mn><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">(~(\text{U2B}(118)))+1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mspace nobreak"> </span><span class="mopen">(</span><span class="mord text"><span class="mord">U2B</span></span><span class="mopen">(</span><span class="mord">118</span><span class="mclose">)))</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo stretchy="false">(</mo><mo></mo><mo stretchy="false">(</mo><mn>0111011</mn><msub><mn>0</mn><mn>2</mn></msub><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">(\sim(01110110_{2}))+1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">0111011</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">))</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo stretchy="false">(</mo><msub><mtext>10001001</mtext><mn>2</mn></msub><mo stretchy="false">)</mo><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">(\text{10001001}_{2})+1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord"><span class="mord text"><span class="mord">10001001</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mtext>10001010</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{10001010}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">10001010</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
</ol>
<h2 id="multiplication-u_w-t_w-and-overflow">Multiplication (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mo></mo><mi>w</mi><mi>u</mi></msubsup><mo separator="true">,</mo><msubsup><mo></mo><mi>w</mi><mi>t</mi></msubsup></mrow><annotation encoding="application/x-tex">*^{u}_{w}, *^{t}_{w}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.040556em;vertical-align:-0.247em;"></span><span class="mord"><span class="mbin"></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">u</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mbin"></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.7935559999999999em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">t</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span></span></span></span>) and overflow</h2>
<p>True product would be the result of integer multiplication with unlimited space: expected product
Actual product is the result of multiplication with limited space.</p>
<ul>
<li>Operands: w bits</li>
<li>True Product: 2w bits <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mo>×</mo><mi>v</mi></mrow><annotation encoding="application/x-tex">u \times v</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span></span></span></span></li>
<li>
<p>Discard: w bits</p>
</li>
<li>Discarding high order w bits has same effect as applying modular arithmetic</li>
</ul>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>p</mi><mo>=</mo><mi>u</mi><msubsup><mo></mo><mi>w</mi><mi>u</mi></msubsup><mi>v</mi><mo>=</mo><mo stretchy="false">(</mo><mi>u</mi><mo>×</mo><mi>v</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="1em"/><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup></mrow><annotation encoding="application/x-tex">p = u *^{u}_{w}v = (u \times v) \mod 2^{w}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal">p</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.9613919999999999em;vertical-align:-0.247em;"></span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"><span class="mbin"></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">u</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:1em;"></span></span><span class="base"><span class="strut" style="height:0.7143919999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">mod</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>p</mi><mo>=</mo><mi>u</mi><msubsup><mo></mo><mi>w</mi><mi>t</mi></msubsup><mi>v</mi><mo>=</mo><msub><mtext>U2T</mtext><mi>w</mi></msub><mo stretchy="false">[</mo><mo stretchy="false">(</mo><mi>u</mi><mo>×</mo><mi>v</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="1em"/><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">p = u *^{t}_{w}v = \text{U2T}_{w}[(u \times v) \mod 2^{w}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal">p</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.0905559999999999em;vertical-align:-0.247em;"></span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"><span class="mbin"></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.8435559999999999em;"><span style="top:-2.4530000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">t</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.247em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord text"><span class="mord">U2T</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.151392em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mopen">[(</span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:1em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">mod</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span><span class="mclose">]</span></span></span></span></span>
<ul>
<li>Example: w = 4</li>
</ul>
<p>Decimal:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mn>5</mn><mn>10</mn></msub><mo>×</mo><msub><mn>5</mn><mn>10</mn></msub><mo>=</mo><msub><mtext>25</mtext><mn>10</mn></msub></mrow><annotation encoding="application/x-tex">5_{10} \times 5_{10} = \text{25}_{10}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord">5</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">25</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
<p>Binary:</p>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mtext>0101</mtext><mn>2</mn></msub><mo>×</mo><msub><mtext>0101</mtext><mn>2</mn></msub><mo>=</mo><menclose notation="horizontalstrike"><mn>001</mn></menclose><msub><mtext>1001</mtext><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\text{0101}_{2} \times \text{0101}_{2} = \sout{001}\text{1001}_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">0101</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord text"><span class="mord">0101</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.64444em;"><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">001</span></span></span><span style="top:-3.2155em;"><span class="pstrut" style="height:3em;"></span><span class="stretchy sout"></span></span></span></span></span></span><span class="mord"><span class="mord text"><span class="mord">1001</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
<h2 id="multiplication-with-power-of-2-versus-shifting">Multiplication with power-of-2 versus shifting</h2>
<ul>
<li>If <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo>×</mo><mi>y</mi></mrow><annotation encoding="application/x-tex">x \times y</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span> where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>y</mi><mo>=</mo><msup><mn>2</mn><mi>k</mi></msup></mrow><annotation encoding="application/x-tex">y = 2^{k}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.849108em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.849108em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span></span></span></span></span></span></span></span></span></span></span></span> then <code class="language-plaintext highlighter-rouge">x &lt;&lt; k</code>
<ul>
<li>For both signed and unsigned</li>
</ul>
</li>
<li>Example:
<ul>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo>×</mo><mn>8</mn><mo>=</mo><mi>x</mi><mo>×</mo><msup><mn>2</mn><mn>3</mn></msup></mrow><annotation encoding="application/x-tex">x \times 8 = x \times 2^{3}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">8</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">3</span></span></span></span></span></span></span></span></span></span></span></span> = <code class="language-plaintext highlighter-rouge">x &lt;&lt; 3</code></li>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo>×</mo><mn>24</mn><mo>=</mo><mo stretchy="false">(</mo><mi>x</mi><mo>×</mo><mn>25</mn><mo stretchy="false">)</mo><mtext></mtext><mo stretchy="false">(</mo><mi>x</mi><mo>×</mo><mn>23</mn><mo stretchy="false">)</mo><mo>=</mo><mo stretchy="false">(</mo><mi>x</mi><mo>×</mo><mn>32</mn><mo stretchy="false">)</mo><mtext></mtext><mo stretchy="false">(</mo><mi>x</mi><mo>×</mo><mn>8</mn><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">x \times 24 = (x \times 25) (x \times 23) = (x \times 32) (x \times 8)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">24</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">25</span><span class="mclose">)</span><span class="mord"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">23</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">32</span><span class="mclose">)</span><span class="mord"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">8</span><span class="mclose">)</span></span></span></span> = (x « 5) (x « 3) (decompose 24 in powers of 2 =&gt; 32 8)</li>
</ul>
</li>
<li>Most machines shift and add faster than multiply
<ul>
<li>Well soon see that compiler generates this code automatically
17</li>
</ul>
</li>
</ul>
<h2 id="summary">Summary</h2>
<ul>
<li>Demo of size and sign conversion in C: code and results posted!</li>
<li>Addition:
<ul>
<li>Unsigned/signed:
<ul>
<li>Behave the same way at the bit level</li>
<li>Interpretation of resulting bit vector (sum) may differ</li>
</ul>
</li>
<li>Unsigned addition -&gt; may overflow, i.e., (w+1)th bit is set
<ul>
<li>If so, then actual sum obtained =&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>x</mi><mo>+</mo><mi>y</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="0.6666666666666666em"/><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup></mrow><annotation encoding="application/x-tex">(x + y) \mod 2^{w}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:0.6666666666666666em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">mod</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span></span></span></span></li>
</ul>
</li>
<li>Signed addition -&gt; may overflow, i.e., (w+1)th bit is set
<ul>
<li>If so, then true sum may be too +ve -&gt; positive overflow OR too -ve -&gt; negative overflow</li>
<li>Then actual sum obtained =&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>U2T</mtext><mi>w</mi></msub><mo stretchy="false">[</mo><mo stretchy="false">(</mo><mi>x</mi><mo>+</mo><mi>y</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="0.6666666666666666em"/><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">\text{U2T}_{w}[(x + y) \mod 2^{w}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord text"><span class="mord">U2T</span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.151392em;"><span style="top:-2.5500000000000003em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mopen">[(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:0.6666666666666666em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">mod</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span><span class="mclose">]</span></span></span></span></li>
</ul>
</li>
</ul>
</li>
<li>Subtraction
<ul>
<li>Becomes an addition where negative operands are transformed into their additive inverse (in twos complement)</li>
</ul>
</li>
<li>Multiplication:
<ul>
<li>Unsigned: actual product obtained -&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>x</mi><mo>×</mo><mi>y</mi><mo stretchy="false">)</mo><mspace></mspace><mspace width="0.6666666666666666em"/><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext></mtext><mtext></mtext><msup><mn>2</mn><mi>w</mi></msup></mrow><annotation encoding="application/x-tex">(x \times y) \mod 2^{w}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mclose">)</span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:0.6666666666666666em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">mod</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.02691em;">w</span></span></span></span></span></span></span></span></span></span></span></span></li>
<li>Signed: actual product obtained -&gt; $$\text{U2T}_{w}[(x \times y) \mod 2^{w}]</li>
<li>Can be replaced by additions and shifts</li>
</ul>
</li>
</ul>
<h2 id="next-lecture">Next lecture</h2>
<ul>
<li>Representing data in memory Most of this is review
<ul>
<li>“Under the Hood” - Von Neumann architecture</li>
<li>Bits and bytes in memory
<ul>
<li>How to diagram memory -&gt; Used in this course and other references</li>
<li>How to represent series of bits -&gt; In binary, in hexadecimal (conversion)</li>
<li>What kind of information (data) do series of bits represent -&gt; Encoding scheme</li>
<li>Order of bytes in memory -&gt; Endian</li>
</ul>
</li>
<li>Bit manipulation bitwise operations
<ul>
<li>Boolean algebra + Shifting</li>
</ul>
</li>
</ul>
</li>
<li>Representing integral numbers in memory
<ul>
<li>Unsigned and signed</li>
<li>Converting, expanding and truncating</li>
<li>Arithmetic operations</li>
</ul>
</li>
<li>Representing real numbers in memory
<ul>
<li>IEEE floating point representation</li>
<li>Floating point in C casting, rounding, addition, …</li>
</ul>
</li>
</ul>
<p>Well illustrate what we covered today by having a demo!</p>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

@ -0,0 +1,97 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, size_t len) {
int i;
for ( i = len-1; i >= 0; i--)
printf(" %.2x", start[i]);
return;
}
int main(int argc, char *argv[]) {
if ( argc < 2 ) {
printf("Forgot the argment! Try again!\n");
return 1;
}
short aShort = atoi(argv[1]);
printf("\nAs a short data type, the variable aShort has the value %hd (hex: 0x", aShort);
show_bytes((byte_pointer) &aShort, sizeof(short));
printf(")\n\n");
/* Convert - size - implicitly */
printf("Converting SIZE implicitly: short -> integer *** Sign extension ***\n");
printf("This is done by issuing the statement: int anInt = aShort;\n");
int anInt = aShort;
printf("As a int data type, the variable anInt has the value %d (hex: 0x", anInt);
show_bytes((byte_pointer) &anInt, sizeof(int));
printf(")\n\n");
/* Convert - size - implicitly */
printf("Converting SIZE implicitly: short -> char *** Truncation ***\n");
printf("This is done by issuing the statement: signed char aChar = aShort;\n");
signed char aChar = aShort;
printf("As a char data type, the variable aChar has the value %hhi (hex: 0x", aChar);
show_bytes((byte_pointer) &aChar, sizeof(signed char));
printf(")\n\n");
/* Convert - sign - implicitly*/
printf("Converting SIGN implicitly: short -> unsigned short\n");
printf("This is done by issuing the statement: unsigned short aUShort = aShort;\n");
unsigned short aUShort = aShort;
printf("As an unsigned short data type, the variable aUShort has the value %hu (hex: 0x", aUShort);
show_bytes((byte_pointer) &aUShort, sizeof(unsigned short));
printf(")\n\n");
/* Convert - sign - implicitly*/
printf("Converting SIGN implicitly: unsigned short -> short\n");
printf("This is done by issuing the statement: short aShort1 = aUShort;\n");
short aShort1 = aUShort;
printf("As a signed short data type, the variable aShort1 has the value %hi (hex: 0x", (signed short) aShort1);
show_bytes((byte_pointer) &aShort1, sizeof(signed short));
printf(")\n\n");
/* Convert - both: 1) size, 2) sign */
printf("Converting both SIZE and SIGN: short -> unsigned int\n");
printf("This is done by issuing the statement: unsigned aUInt = aShort;\n");
unsigned aUInt = aShort;
printf("As an unsigned int data type, the variable aUInt has the value %u (hex: 0x", aUInt);
show_bytes((byte_pointer) &aUInt, sizeof(unsigned));
printf(")\n\n");
/* One step at a time */
printf("One step at a time - First conversion is SIZE: (int) aShort = %d\n", (int) aShort);
printf("One step at a time - Second conversion is SIGN: (unsigned) (int) aShort = %u\n\n", (unsigned) (int) aShort);
/* Reverse the process and see what happens ... */
printf("What if ... First conversion is SIGN: (unsigned short) aShort = %hu\n", (unsigned short) aShort);
printf("What if ... Second conversion is SIZE: (unsigned int) (unsigned short) aShort = %d\n\n", (unsigned int) (unsigned short) aShort);
/* Convert - both: 1) size, 2) sign */
printf("Converting both SIZE and SIGN: short -> unsigned char\n");
printf("This is done by issuing the statement: unsigned char anUChar = aShort;\n");
unsigned char anUChar = aShort;
printf("As an unsigned char data type, the variable anUChar has the value %hhu (hex: 0x", anUChar);
show_bytes((byte_pointer) &anUChar, sizeof(unsigned char));
printf(")\n\n");
/* One step at a time */
printf("One step at a time - First conversion is SIZE: (signed char) aShort = %hhi\n", (signed char) aShort);
printf("One step at a time - Second conversion is SIGN: (unsigned char) (signed char) aShort = %hhu\n\n", (unsigned char) (signed char) aShort);
/* Reverse the process and see what happens ... */
printf("What if ... First conversion is SIGN: (unsigned short) aShort = %hu\n", (unsigned short) aShort);
printf("What if ... Second conversion is SIZE: (unsigned char) (unsigned short) aShort = %hhu\n\n", (unsigned char) (unsigned short) aShort);
return 0;
}

@ -0,0 +1,81 @@
$ ./Demo 12345
As a short data type, the variable aShort has the value 12345 (hex: 0x 30 39)
Converting SIZE implicitly: short -> integer *** Sign extension ***
This is done by issuing the statement: int anInt = aShort;
As a int data type, the variable anInt has the value 12345 (hex: 0x 00 00 30 39)
Converting SIZE implicitly: short -> char *** Truncation ***
This is done by issuing the statement: signed char aChar = aShort;
As a char data type, the variable aChar has the value 57 (hex: 0x 39)
Converting SIGN implicitly: short -> unsigned short
This is done by issuing the statement: unsigned short aUShort = aShort;
As an unsigned short data type, the variable aUShort has the value 12345 (hex: 0x 30 39)
Converting SIGN implicitly: unsigned short -> short
This is done by issuing the statement: short aShort1 = aUShort;
As a signed short data type, the variable aShort1 has the value 12345 (hex: 0x 30 39)
Converting both SIZE and SIGN: short -> unsigned int
This is done by issuing the statement: unsigned aUInt = aShort;
As an unsigned int data type, the variable aUInt has the value 12345 (hex: 0x 00 00 30 39)
One step at a time - First conversion is SIZE: (int) aShort = 12345
One step at a time - Second conversion is SIGN: (unsigned) (int) aShort = 12345
What if ... First conversion is SIGN: (unsigned short) aShort = 12345
What if ... Second conversion is SIZE: (unsigned int) (unsigned short) aShort = 12345
Converting both SIZE and SIGN: short -> unsigned char
This is done by issuing the statement: unsigned char anUChar = aShort;
As an unsigned char data type, the variable anUChar has the value 57 (hex: 0x 39)
One step at a time - First conversion is SIZE: (signed char) aShort = 57
One step at a time - Second conversion is SIGN: (unsigned char) (signed char) aShort = 57
What if ... First conversion is SIGN: (unsigned short) aShort = 12345
What if ... Second conversion is SIZE: (unsigned char) (unsigned short) aShort = 57
----------------------------------------------------------------------------------------------
$ ./Demo -12345
As a short data type, the variable aShort has the value -12345 (hex: 0x cf c7)
Converting SIZE implicitly: short -> integer *** Sign extension ***
This is done by issuing the statement: int anInt = aShort;
As a int data type, the variable anInt has the value -12345 (hex: 0x ff ff cf c7)
Converting SIZE implicitly: short -> char *** Truncation ***
This is done by issuing the statement: signed char aChar = aShort;
As a char data type, the variable aChar has the value -57 (hex: 0x c7)
Converting SIGN implicitly: short -> unsigned short
This is done by issuing the statement: unsigned short aUShort = aShort;
As an unsigned short data type, the variable aUShort has the value 53191 (hex: 0x cf c7)
Converting SIGN implicitly: unsigned short -> short
This is done by issuing the statement: short aShort1 = aUShort;
As a signed short data type, the variable aShort1 has the value -12345 (hex: 0x cf c7)
Converting both SIZE and SIGN: short -> unsigned int
This is done by issuing the statement: unsigned aUInt = aShort;
As an unsigned int data type, the variable aUInt has the value 4294954951 (hex: 0x ff ff cf c7)
One step at a time - First conversion is SIZE: (int) aShort = -12345
One step at a time - Second conversion is SIGN: (unsigned) (int) aShort = 4294954951
What if ... First conversion is SIGN: (unsigned short) aShort = 53191
What if ... Second conversion is SIZE: (unsigned int) (unsigned short) aShort = 53191
Converting both SIZE and SIGN: short -> unsigned char
This is done by issuing the statement: unsigned char anUChar = aShort;
As an unsigned char data type, the variable anUChar has the value 199 (hex: 0x c7)
One step at a time - First conversion is SIZE: (signed char) aShort = -57
One step at a time - Second conversion is SIGN: (unsigned char) (signed char) aShort = 199
What if ... First conversion is SIGN: (unsigned short) aShort = 53191
What if ... Second conversion is SIZE: (unsigned char) (unsigned short) aShort = 199

@ -0,0 +1,604 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
<link rel="stylesheet" href="/assets/css/katex.css" id="math-css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="cmpt-295">CMPT 295</h1>
<ul>
<li>Unit - Data Representation
<ul>
<li>Lecture 6 Representing fractional numbers in memory</li>
<li>IEEE floating point representation contd</li>
</ul>
</li>
</ul>
<h2 id="have-you-heard-of-that-new-band-1023-megabytes">Have you heard of that new band “1023 Megabytes”?</h2>
<p>Theyre pretty good,
but they dont have a gig just yet.
😭</p>
<h2 id="last-lecture">Last Lecture</h2>
<ul>
<li>Representing integral numbers in memory
<ul>
<li>Can encode a small range of values exactly (in 1, 2, 4, 8 bytes)
<ul>
<li>For example: We can represent the values -128 to 127 exactly in 1 byte using a signed char in C</li>
</ul>
</li>
</ul>
</li>
<li>Representing fractional numbers in memory
<ol>
<li>Positional notation has some advantages, but also disadvantages -&gt; so not used!</li>
<li>IEEE floating point representation: can encode a much larger range of e.g., single precision: [10-38..1038] values approximately (in 4 or 8 bytes)</li>
</ol>
</li>
<li>Overview of IEEE floating point representation
<ul>
<li>Precision options (float 32-bit, double 64-bit)</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>V</mi><mo>=</mo><msup><mtext>(-1)</mtext><mi>s</mi></msup><mo>×</mo><mi>M</mi><mo>×</mo><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">V = \text{(-1)}^{s} \times M \times 2^{E}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.054292em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord text"><span class="mord">(-1)</span></span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.804292em;"><span style="top:-3.2029em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8913309999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8913309999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>s &gt; sign bit</li>
<li>exp encodes E (but != E)</li>
<li>frac encodes M (but != M)</li>
</ul>
</li>
</ul>
<p>We interpret the bit vector (expressed in IEEE floating point encoding) stored in memory using this equation.</p>
<h2 id="todays-menu">Todays Menu</h2>
<ul>
<li>Representing data in memory Most of this is review
<ul>
<li>“Under the Hood” - Von Neumann architecture</li>
<li>Bits and bytes in memory
<ul>
<li>How to diagram memory -&gt; Used in this course and other references</li>
<li>How to represent series of bits -&gt; In binary, in hexadecimal (conversion)</li>
<li>What kind of information (data) do series of bits represent -&gt; Encoding scheme</li>
<li>Order of bytes in memory -&gt; Endian</li>
</ul>
</li>
<li>Bit manipulation bitwise operations
<ul>
<li>Boolean algebra + Shifting</li>
</ul>
</li>
</ul>
</li>
<li>Representing integral numbers in memory
<ul>
<li>Unsigned and signed</li>
<li>Converting, expanding and truncating</li>
<li>Arithmetic operations</li>
</ul>
</li>
<li>Representing real numbers in memory
4
<ul>
<li>IEEE floating point representation</li>
<li>Floating point in C casting, rounding, addition, …</li>
</ul>
</li>
</ul>
<h2 id="ieee-floating-point-representation-three-kinds-of-values">IEEE Floating Point Representation Three “kinds” of values</h2>
<p>We interpret the bit vector
(expressed in IEEE floating point encoding) stored in memory using this equation:</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mo></mo><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">
V = (-1)^{s} M 2^{E}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathdefault" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.1413309999999999em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord"></span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathdefault" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8913309999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p>Bit breakdown<code class="language-plaintext highlighter-rouge">exp</code> and <code class="language-plaintext highlighter-rouge">frac</code> interpreted as unsigned:</p>
<ul>
<li>s = 1 bit</li>
<li>exp = k bits
<ol>
<li>If <code class="language-plaintext highlighter-rouge">exp</code> != 0 and <code class="language-plaintext highlighter-rouge">exp</code> != 11…11 (exp range: [0000001…11111110]). Equations:
<ul>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>E</mi><mo>=</mo><mtext>exp</mtext><mo></mo><mtext>bias</mtext></mrow><annotation encoding="application/x-tex">E = \text{exp} - \text{bias}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.05764em;">E</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.7777700000000001em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">exp</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span></span></span></span> and <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mo></mo><mn>1</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} - 1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.9324379999999999em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>M</mi><mo>=</mo><mn>1</mn><mo>+</mo><mtext>frac</mtext></mrow><annotation encoding="application/x-tex">M = 1 + \text{frac}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">frac</span></span></span></span></span></span>
</li>
</ul>
</li>
<li>If <code class="language-plaintext highlighter-rouge">exp</code> = 00…00 (all 0s) =&gt; denormalized. Equations:
<ul>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>E</mi><mo>=</mo><mn>1</mn><mo></mo><mtext>bias</mtext></mrow><annotation encoding="application/x-tex">E = 1 - \text{bias}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.05764em;">E</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span></span></span></span> and <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mo></mo><mn>1</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} - 1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.9324379999999999em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>M</mi><mo>=</mo><mtext>frac</mtext></mrow><annotation encoding="application/x-tex">M = \text{frac}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">frac</span></span></span></span></span></span>
</li>
</ul>
</li>
<li>If <code class="language-plaintext highlighter-rouge">exp</code> 11…11 (all 1s) =&gt; special cases.
<ul>
<li>Case 1: <code class="language-plaintext highlighter-rouge">frac</code> = 000…0</li>
<li>Case 2: <code class="language-plaintext highlighter-rouge">frac</code> != 000…0</li>
</ul>
</li>
</ol>
</li>
</ul>
<h2 id="ieee-floating-point-representation---normalized">IEEE floating point representation - normalized</h2>
<p>Numerical Form: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mtext></mtext><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">V = (1)^{s} M 2^{E}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.0913309999999998em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8413309999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></p>
<p>Bit breakdown:</p>
<ul>
<li>s = 1 bit</li>
<li><code class="language-plaintext highlighter-rouge">exp</code> = k bits
<ul>
<li>If <code class="language-plaintext highlighter-rouge">exp</code> != 0 and <code class="language-plaintext highlighter-rouge">exp</code> != 11…11 (<code class="language-plaintext highlighter-rouge">exp</code> range: [00000001…11111110]) =&gt; normalized. Equations:
<ul>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>E</mi><mo>=</mo><mtext>exp</mtext><mo></mo><mtext>bias</mtext></mrow><annotation encoding="application/x-tex">E = \text{exp} - \text{bias}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.05764em;">E</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.7777700000000001em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">exp</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span></span></span></span> and <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mo></mo><mn>1</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} - 1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.9324379999999999em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>M</mi><mo>=</mo><mn>1</mn><mo>+</mo><mtext>frac</mtext></mrow><annotation encoding="application/x-tex">M = 1 + \text{frac}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">frac</span></span></span></span></span></span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>Why is <code class="language-plaintext highlighter-rouge">E</code> biased?</p>
<p>Using single precision as an example (s = 1 bit, exp = 8 bits, frac = 23 bits):</p>
<ul>
<li>(exp range: [00000001 .. 11111110]) =&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><msub><mn>1</mn><mn>10</mn></msub><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mn>25</mn><msub><mn>4</mn><mn>10</mn></msub><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[1_{10}...254_{10}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord">...25</span><span class="mord"><span class="mord">4</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span></span></span></span></li>
<li>If <code class="language-plaintext highlighter-rouge">E</code> is not biased (i.e. E = exp), then <code class="language-plaintext highlighter-rouge">E</code> range <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><msub><mn>1</mn><mn>10</mn></msub><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mn>25</mn><msub><mn>4</mn><mn>10</mn></msub><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[1_{10} ... 254_{10}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord">...25</span><span class="mord"><span class="mord">4</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span></span></span></span></li>
<li><code class="language-plaintext highlighter-rouge">V</code> range [<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mn>1</mn></msup></mrow><annotation encoding="application/x-tex">2^{1}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span></span></span></span></span></span></span></span></span></span></span></span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mn>254</mn></msup></mrow><annotation encoding="application/x-tex">2^{254}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">254</span></span></span></span></span></span></span></span></span></span></span></span>] = [2…<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo></mo><mn>2.89</mn><mo>×</mo><mn>1</mn><msup><mn>0</mn><mn>76</mn></msup></mrow><annotation encoding="application/x-tex">\approx 2.89 \times 10^{76}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.48312em;vertical-align:0em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">2.89</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">76</span></span></span></span></span></span></span></span></span></span></span></span>] (so cannot express numbers &lt; 2)</li>
<li>By biasing <code class="language-plaintext highlighter-rouge">E</code> (i.e. E = exp - bias), then <code class="language-plaintext highlighter-rouge">E</code> range: [1-127…254-127] == [-126…127] (since k = 8, bias = <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mrow><mn>8</mn><mo></mo><mn>1</mn></mrow></msup><mo></mo><mn>1</mn></mrow><annotation encoding="application/x-tex">2^{8-1} - 1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.897438em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">8</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span> = 127)</li>
<li><code class="language-plaintext highlighter-rouge">V</code> range: [<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mrow><mo></mo><mn>126</mn></mrow></msup></mrow><annotation encoding="application/x-tex">2^{-126}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"></span><span class="mord mtight">126</span></span></span></span></span></span></span></span></span></span></span></span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mn>127</mn></msup></mrow><annotation encoding="application/x-tex">2^{127}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">127</span></span></span></span></span></span></span></span></span></span></span></span>] = [<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo></mo><mn>1.18</mn><mo>×</mo><mn>1</mn><msup><mn>0</mn><mrow><mo></mo><mn>38</mn></mrow></msup></mrow><annotation encoding="application/x-tex">\approx 1.18 \times 10^{-38}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.48312em;vertical-align:0em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1.18</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"></span><span class="mord mtight">38</span></span></span></span></span></span></span></span></span></span></span></span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo></mo><mn>1.7</mn><mo>×</mo><mn>1</mn><msup><mn>0</mn><mn>38</mn></msup></mrow><annotation encoding="application/x-tex">\approx 1.7 \times 10^{38}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.48312em;vertical-align:0em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1.7</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">38</span></span></span></span></span></span></span></span></span></span></span></span> (so can now express very small (and very large) numbers)</li>
<li>Why adding 1 to <code class="language-plaintext highlighter-rouge">frac</code>? Because the number (or value) V is first normalized before it is converted.</li>
</ul>
<h2 id="review-scientific-notation-and-normalization">Review: Scientific Notation and normalization</h2>
<ul>
<li>From Wikipedia:
<ul>
<li>Scientific notation is a way of expressing numbers that are too large or too small to be conveniently written in decimal form (as they are long strings of digits).</li>
<li>In scientific notation, nonzero numbers are written in the form +/- M × 10n</li>
<li>In normalized notation, the exponent n is chosen such that the absolute value of the significand M is at least 1 (M = 1.0) but less than the base
<ul>
<li>M range for base 10 =&gt; [1.0 .. 10.0 ε ]</li>
<li>M range for base 2 =&gt; [1.0 .. 2.0 ε ]</li>
</ul>
</li>
</ul>
</li>
<li>
<p>Examples:</p>
<ul>
<li>A protons mass is 0.0000000000000000000000000016726 kg -&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1.6726</mn><mo>×</mo><mn>1</mn><msup><mn>0</mn><mrow><mtext></mtext><mn>27</mn></mrow></msup></mrow><annotation encoding="application/x-tex">1.6726 \times 10^{27}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1.6726</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">27</span></span></span></span></span></span></span></span></span></span></span></span> kg</li>
<li>Speed of light is 299,792,458 m/s -&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>2.99792</mn><mo separator="true">,</mo><mn>458</mn><mo>×</mo><mn>1</mn><msup><mn>0</mn><mn>8</mn></msup></mrow><annotation encoding="application/x-tex">2.99792,458 \times 10^{8}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8388800000000001em;vertical-align:-0.19444em;"></span><span class="mord">2.99792</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">458</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord">1</span><span class="mord"><span class="mord">0</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">8</span></span></span></span></span></span></span></span></span></span></span></span> m/s</li>
</ul>
</li>
</ul>
<p>Syntax of normalized notation:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Notation</th>
</tr>
</thead>
<tbody>
<tr>
<td>Sign</td>
<td>+/-</td>
</tr>
<tr>
<td>Significant</td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mn>0</mn></msub><mo separator="true">,</mo><msub><mi>d</mi><mrow><mo></mo><mn>1</mn></mrow></msub><mo separator="true">,</mo><msub><mi>d</mi><mrow><mo></mo><mn>2</mn></mrow></msub><mo separator="true">,</mo><msub><mi>d</mi><mrow><mo></mo><mn>3</mn></mrow></msub></mrow><annotation encoding="application/x-tex">d_{0}, d_{-1}, d_{-2}, d_{-3}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.902771em;vertical-align:-0.208331em;"></span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">0</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.301108em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"></span><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.208331em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.301108em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"></span><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.208331em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.301108em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"></span><span class="mord mtight">3</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.208331em;"><span></span></span></span></span></span></span></span></span></span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mrow><mo></mo><mi>n</mi></mrow></msub></mrow><annotation encoding="application/x-tex">d_{-n}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.902771em;vertical-align:-0.208331em;"></span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.25833100000000003em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight"></span><span class="mord mathnormal mtight">n</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.208331em;"><span></span></span></span></span></span></span></span></span></span></td>
</tr>
<tr>
<td>Base</td>
<td><code class="language-plaintext highlighter-rouge">b</code></td>
</tr>
<tr>
<td>Exponent</td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mrow></mrow><mtext>exp</mtext></msup></mrow><annotation encoding="application/x-tex">^{\text{exp}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.664392em;vertical-align:0em;"></span><span class="mord"><span></span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">exp</span></span></span></span></span></span></span></span></span></span></span></span></span></td>
</tr>
</tbody>
</table>
<ul>
<li>Lets try: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>101011010.10</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">101011010.101_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">101011010.10</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span> -&gt; ___</li>
</ul>
<h2 id="lets-try-normalizing-these-fractional-binary-numbers">Lets try normalizing these fractional binary numbers!</h2>
<ol>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>101011010.10</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">101011010.101_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">101011010.10</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>0.00000000110</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">0.000000001101_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">0.00000000110</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>1100000011100</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">11000000111001_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1100000011100</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
</ol>
<h2 id="ieee-floating-point-representation">IEEE floating point representation</h2>
<ul>
<li>Once V is normalized, we apply the equations
<ul>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mtext></mtext><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup><mo>=</mo><mn>1.0101101010</mn><msub><mn>1</mn><mn>2</mn></msub><mo>×</mo><msup><mn>2</mn><mn>8</mn></msup></mrow><annotation encoding="application/x-tex">V = (1)^{s} M 2^{E} = 1.01011010101_{2} \times 2^{8}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.1413309999999999em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8913309999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1.0101101010</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8641079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8641079999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">8</span></span></span></span></span></span></span></span></span></span></span></span></span>
</li>
<li><code class="language-plaintext highlighter-rouge">s</code> = ???</li>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>E</mi><mo>=</mo><mtext>exp</mtext><mo></mo><mtext>bias</mtext></mrow><annotation encoding="application/x-tex">E = \text{exp} - \text{bias}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.05764em;">E</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.7777700000000001em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">exp</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span></span></span></span> where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mo></mo><mn>1</mn><mo>=</mo><msup><mn>2</mn><mn>7</mn></msup><mo></mo><mn>1</mn><mo>=</mo><mn>128</mn><mo></mo><mn>1</mn><mo>=</mo><mn>127</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} - 1 = 2^{7} - 1 = 128 - 1 = 127</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.9324379999999999em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.897438em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">7</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">128</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">127</span></span></span></span></li>
<li><code class="language-plaintext highlighter-rouge">exp</code> = <code class="language-plaintext highlighter-rouge">E</code> + <code class="language-plaintext highlighter-rouge">bias</code> = ___</li>
<li><code class="language-plaintext highlighter-rouge">M</code> = 1 + <code class="language-plaintext highlighter-rouge">frac</code> = ___</li>
<li><code class="language-plaintext highlighter-rouge">s</code> = 1 bit, <code class="language-plaintext highlighter-rouge">exp</code> = k bits =&gt; 8 bits, <code class="language-plaintext highlighter-rouge">frac</code> n bits =&gt; 23 bits</li>
<li>bit vector in memory:</li>
</ul>
</li>
</ul>
<h2 id="why-adding-1-to-frac-or-subtracting-1-from-m">Why adding 1 to frac (or subtracting 1 from M)?</h2>
<ul>
<li>Because the number (or value) V is first normalized before it is converted.
<ul>
<li>As part of this normalization process, we transform our binary number such that its significand M is within the range [1.0 .. 2.0 ε ]</li>
<li>Remember: M range for base 2 =&gt; [1.0 … 2.0 ε]</li>
<li>This implies that M is always at least 1.0, so its integral part always has the value 1</li>
<li>So since this bit is always part of M, IEEE 754 does not explicitly save it in its bit pattern (i.e., in memory)</li>
<li>Instead, this bit is implied!</li>
</ul>
</li>
</ul>
<h2 id="why-adding-1-to-frac-or-subtracting-1-from-m-1">Why adding 1 to frac (or subtracting 1 from M)?</h2>
<p>Implying this bit has the following effects:</p>
<p>We get the
leading bit
for free!</p>
<ol>
<li>We save 1 bit when we convert (represent) a fractional decimal number into a bit pattern using IEEE 754 floating point representation</li>
<li>We have to add this 1 bit back when we convert from a bit pattern (IEEE 754 floating point representation) back to a fractional decimal</li>
</ol>
<p>Example: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mtext></mtext><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup><mo>=</mo><mn>1.01011010101</mn><mo>×</mo><msup><mn>2</mn><mn>8</mn></msup></mrow><annotation encoding="application/x-tex">V = (1)^{s} M 2^{E} = 1.01011010101 \times 2^{8}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.0913309999999998em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8413309999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">1.01011010101</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">8</span></span></span></span></span></span></span></span></span></span></span></span></p>
<p>M = 1. 01011010101 =&gt; M = 1 + frac</p>
<p>This bit is implied hence not stored in the bit pattern produced
by the IEEE 754 floating point representation, and what we
store in the frac part of the IEEE 754 bit pattern is 01011010101</p>
<h2 id="ieee-floating-point-representation-single-precision">IEEE floating point representation (single precision)</h2>
<ul>
<li>What if the 4 bytes starting at M[0x0000] represented a fractional
decimal number (encoded as an IEEE floating point number) -&gt; value?
single precision</li>
</ul>
<p>Numerical Form: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mtext></mtext><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">V = (1)^{s} M 2^{E}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.0913309999999998em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8413309999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></p>
<table>
<thead>
<tr>
<th>Value</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td> </td>
</tr>
<tr>
<td>10000111</td>
<td>k=8 bits, interpreted as unsigned</td>
</tr>
<tr>
<td>01011010101000000000000</td>
<td>n=23 bits, interpreted as unsigned</td>
</tr>
</tbody>
</table>
<ul>
<li>exp ≠ 0 and exp ≠ 111111112 -&gt; normalized</li>
<li>s = ___</li>
<li>E = exp bias where bias = <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mtext></mtext><mn>1</mn><mo>=</mo><msup><mn>2</mn><mn>7</mn></msup><mtext></mtext><mn>1</mn><mo>=</mo><mn>128</mn><mtext></mtext><mn>1</mn><mo>=</mo><mn>127</mn></mrow><annotation encoding="application/x-tex">2^{k-1} 1 = 2^{7} 1 = 128 1 = 127</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8491079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">7</span></span></span></span></span></span></span></span></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1281</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">127</span></span></span></span>$</li>
<li>E = ____ - 127 =</li>
<li>M = 1 + <code class="language-plaintext highlighter-rouge">frac</code> = 1 + ___</li>
<li>V = ____</li>
</ul>
<p>Little endian memory layout:</p>
<table>
<thead>
<tr>
<th>Address</th>
<th>M[]</th>
</tr>
</thead>
<tbody>
<tr>
<td>size-1</td>
<td> </td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>0x0003</td>
<td>11000011</td>
</tr>
<tr>
<td>0x0002</td>
<td>10101101</td>
</tr>
<tr>
<td>0x0001</td>
<td>01010000</td>
</tr>
<tr>
<td>0x0000</td>
<td>00000000</td>
</tr>
</tbody>
</table>
<h2 id="lets-give-it-a-go">Lets give it a go!</h2>
<ul>
<li>What if the 4 bytes starting at M[0x0000] represented a fractional
decimal number (encoded as an IEEE floating point number) -&gt; value?</li>
</ul>
<p>Numerical form: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mo></mo><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">V = (-1)^{s} M 2^{E}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.0913309999999998em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord"></span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8413309999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></p>
<p>single precision</p>
<table>
<thead>
<tr>
<th>Value</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td> </td>
</tr>
<tr>
<td>10001100</td>
<td>k=8 bits, interpreted as unsigned</td>
</tr>
<tr>
<td>11011011011000000000000</td>
<td>n=23 bits, interpreted as unsigned</td>
</tr>
</tbody>
</table>
<ul>
<li>exp ≠ 0 and exp ≠ 111111112 -&gt; normalized</li>
<li>s = ____</li>
<li>E = exp - bias where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mn>7</mn></msup><mo></mo><mn>1</mn><mo>=</mo><mn>128</mn><mo></mo><mn>1</mn><mo>=</mo><mn>127</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{7} - 1 = 128 - 1 = 127</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.897438em;vertical-align:-0.08333em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">7</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord">128</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">127</span></span></span></span></li>
<li>E = ____ - 127 = ___</li>
<li>M = 1 + frac = 1 + ____</li>
<li>V = ____</li>
</ul>
<p>Little endian memory map:</p>
<table>
<thead>
<tr>
<th>Address</th>
<th>M[]</th>
</tr>
</thead>
<tbody>
<tr>
<td>size-1</td>
<td> </td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>0x0003</td>
<td>01000110</td>
</tr>
<tr>
<td>0x0002</td>
<td>01101101</td>
</tr>
<tr>
<td>0x0001</td>
<td>10110000</td>
</tr>
<tr>
<td>0x0000</td>
<td>00000000</td>
</tr>
</tbody>
</table>
<h2 id="ieee-floating-point-representation-single-precision-1">IEEE floating point representation (single precision)</h2>
<p>How would 47.21875 be encoded as IEEE floating point number?</p>
<ol>
<li>Convert 47.28 to binary (using the positional notation R2B(X)) =&gt;
<ul>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>47</mn><mo>=</mo><mn>10111</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">47 = 101111_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">47</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">10111</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi mathvariant="normal">.</mi><mn>21875</mn><mo>=</mo><mi mathvariant="normal">.</mi><mn>0011</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">.21875 = .00111_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">.21875</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">.0011</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span>
</li>
</ul>
</li>
<li>Normalize binary number:
101111.00111 =&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1.011110011</mn><msub><mn>1</mn><mn>2</mn></msub><mo>×</mo><msup><mn>2</mn><mn>5</mn></msup></mrow><annotation encoding="application/x-tex">1.0111100111_{2} \times 2^{5}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1.011110011</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">5</span></span></span></span></span></span></span></span></span></span></span></span>
<ul>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mtext></mtext><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">V = (1)^{s} M 2^{E}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.1413309999999999em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8913309999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></span>
</li>
</ul>
</li>
<li>Determine …
<ul>
<li>s = 0</li>
<li>E = exp bias where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mtext></mtext><mn>1</mn><mo>=</mo><msup><mn>2</mn><mn>7</mn></msup><mtext></mtext><mn>1</mn><mo>=</mo><mn>128</mn><mtext></mtext><mn>1</mn><mo>=</mo><mn>127</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} 1 = 2^{7} 1 = 128 1 = 127</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8491079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">7</span></span></span></span></span></span></span></span></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1281</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">127</span></span></span></span></li>
<li>exp = E + bias = 5 + 127 = 132 =&gt; U2B(132) =&gt; 10000100</li>
<li>M = 1 + frac -&gt; frac = M - 1 =&gt; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1.011110011</mn><msub><mn>1</mn><mn>2</mn></msub><mo></mo><mn>1</mn><mo>=</mo><mi mathvariant="normal">.</mi><mn>011110011</mn><msub><mn>1</mn><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">1.0111100111_{2} - 1 = .0111100111_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">1.011110011</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="mord">.011110011</span><span class="mord"><span class="mord">1</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></li>
</ul>
</li>
<li>32 bits organized in s|exp|frac: [0|10000100|01111001110000000000000}</li>
<li>0x423CE000</li>
</ol>
<h2 id="ieee-floating-point-representation-single-precision-2">IEEE floating point representation (single precision)</h2>
<p>How would 12345.75 be encoded as IEEE floating point number?</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mo></mo><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">
V = (-1)^{s} M 2^{E}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathdefault" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.1413309999999999em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord"></span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathdefault" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8913309999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<ol>
<li>Convert 12345.75 to binary
<ul>
<li>12345 =&gt; ____ .75 =&gt; ____</li>
</ul>
</li>
<li>Normalize binary number:</li>
<li>Determine …
<ul>
<li>s = ____</li>
<li>E = exp bias where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mtext></mtext><mn>1</mn><mo>=</mo><msup><mn>2</mn><mn>7</mn></msup><mtext></mtext><mn>1</mn><mo>=</mo><mn>128</mn><mtext></mtext><mn>1</mn><mo>=</mo><mn>127</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} 1 = 2^{7} 1 = 128 1 = 127</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8491079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">7</span></span></span></span></span></span></span></span></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1281</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">127</span></span></span></span></li>
<li>exp = E + bias = ____</li>
<li>M = 1 + frac -&gt; frac = M - 1</li>
</ul>
</li>
<li>[_|________|_______________________]</li>
<li>Express in hex:</li>
</ol>
<h2 id="summary">Summary</h2>
<ul>
<li>IEEE Floating Point Representation
<ol>
<li>Denormalized</li>
<li>Special cases</li>
<li>Normalized =&gt; exp ≠ 000…0 and exp ≠ 111…1
<ul>
<li>Single precision: bias = 127, exp: [1..254], E: [-126..127] =&gt; [10-38 … 1038]</li>
<li>Called “normalized” because binary numbers are normalized</li>
</ul>
<ul>
<li>Effect: “We get the leading bit for free”
<ul>
<li>Leading bit is always assumed (never part of bit pattern)</li>
</ul>
</li>
</ul>
</li>
</ol>
</li>
<li>IEEE floating point number as encoding scheme
<ul>
<li>Fractional decimal number  IEEE 754 (bit pattern)</li>
<li>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>V</mi><mo>=</mo><mo stretchy="false">(</mo><mtext></mtext><mn>1</mn><msup><mo stretchy="false">)</mo><mi>s</mi></msup><mi>M</mi><msup><mn>2</mn><mi>E</mi></msup></mrow><annotation encoding="application/x-tex">V = (1)^{s} M 2^{E}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.1413309999999999em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mclose"><span class="mclose">)</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7143919999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">s</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8913309999999999em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">E</span></span></span></span></span></span></span></span></span></span></span></span></span>
<ul>
<li>s is sign bit, M = 1 + frac, E = exp bias, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>bias</mtext><mo>=</mo><msup><mn>2</mn><mrow><mi>k</mi><mo></mo><mn>1</mn></mrow></msup><mtext></mtext><mn>1</mn></mrow><annotation encoding="application/x-tex">\text{bias} = 2^{k-1} 1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">bias</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.8491079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span><span class="mbin mtight"></span><span class="mord mtight">1</span></span></span></span></span></span></span></span></span><span class="mord">1</span></span></span></span> and k is width of exp</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="next-lecture">Next Lecture</h2>
<ul>
<li>Representing data in memory Most of this is review
<ul>
<li>“Under the Hood” - Von Neumann architecture</li>
<li>Bits and bytes in memory
<ul>
<li>How to diagram memory -&gt; Used in this course and other references</li>
<li>How to represent series of bits -&gt; In binary, in hexadecimal (conversion)</li>
<li>What kind of information (data) do series of bits represent -&gt; Encoding scheme</li>
<li>Order of bytes in memory -&gt; Endian</li>
</ul>
</li>
<li>Bit manipulation bitwise operations
<ul>
<li>Boolean algebra + Shifting</li>
</ul>
</li>
</ul>
</li>
<li>Representing integral numbers in memory
<ul>
<li>Unsigned and signed</li>
<li>Converting, expanding and truncating</li>
<li>Arithmetic operations</li>
</ul>
</li>
<li>Representing real numbers in memory
<ul>
<li>IEEE floating point representation</li>
<li>Floating point in C casting, rounding, addition, …</li>
</ul>
</li>
</ul>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

File diff suppressed because one or more lines are too long

@ -0,0 +1,280 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
<link rel="stylesheet" href="/assets/css/katex.css" id="math-css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="cmpt-295-unit---machine-level-programming">CMPT 295: Unit - Machine-Level Programming</h1>
<ul>
<li>Lecture 8 Introduction</li>
<li>Compilation process: C -&gt; assembly code -&gt; machine level code</li>
</ul>
<h1 id="last-lecture">Last Lecture</h1>
<ul>
<li>Most fractional decimal numbers cannot be exactly encoded using IEEE floating point representation -&gt; rounding</li>
<li>Denormalized values
<ul>
<li>Condition: exp = 0000…0</li>
<li>0 &lt;= denormalized values &lt; 1, equidistant because all have same 2E</li>
</ul>
</li>
<li>Special values
<ul>
<li>Condition: exp = 1111…1
<ul>
<li>Case 1: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>frac</mtext><mo>=</mo><mtext>000...0</mtext><mo>=</mo><mi mathvariant="normal"></mi></mrow><annotation encoding="application/x-tex">\text{frac} = \text{000...0} = \infty</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">frac</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">000...0</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord"></span></span></span></span></li>
<li>Case 2: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>frac</mtext><mo mathvariant="normal"></mo><mtext>000...0</mtext><mo>=</mo><mtext>NaN</mtext></mrow><annotation encoding="application/x-tex">\text{frac} \neq \text{000...0} = \text{NaN}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord text"><span class="mord">frac</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"><span class="mrel"><span class="mord vbox"><span class="thinbox"><span class="rlap"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="inner"><span class="mord"><span class="mrel"></span></span></span><span class="fix"></span></span></span></span></span><span class="mrel">=</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">000...0</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">NaN</span></span></span></span></span></li>
</ul>
</li>
</ul>
</li>
<li>Impact on C
<ul>
<li>Conversion/casting, rounding</li>
<li>Arithmetic operators:
<ul>
<li>Behaviour not the same as for real arithmetic =&gt; violates associativity</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="todays-menu">Todays Menu</h2>
<ul>
<li>Introduction
<ul>
<li>C program -&gt; assembly code -&gt; machine level code</li>
</ul>
</li>
<li>Assembly language basics: data, move operation</li>
<li>Operation leaq and Arithmetic &amp; logical operations</li>
<li>Conditional Statement Condition Code + cmovX</li>
<li>Loops</li>
<li>Function call Stack</li>
<li>Array</li>
<li>Buffer Overflow</li>
<li>Floating-point data &amp; operations</li>
</ul>
<h2 id="what-could-these-32-bits-represent">What could these 32 bits represent?</h2>
<p>What kind of information could they encode?</p>
<pre>
00011000111011001000001101001000
</pre>
<p>Answer:</p>
<ul>
<li>Aside from characters, integers or floating point numbers, etc…</li>
<li>Review: We saw that all modern computers, designed based on the von Neumann architecture, store their programs in memory
<ul>
<li>Data and instructions of our C program are in main memory together (but in different locations)</li>
</ul>
</li>
<li>So, these bits could represent code, for example:
<ul>
<li>Assembly code: <code class="language-plaintext highlighter-rouge">sub $0x18, %rsp</code></li>
<li>Machine code: <code class="language-plaintext highlighter-rouge">48 83 ec 18</code></li>
</ul>
</li>
</ul>
<h2 id="c-program-in-memory">C program in memory?</h2>
<p>We have just spent a few lectures looking at how our data
can be represented as a series of 0s and Is, now …</p>
<ol>
<li>Question: How does our C program end up being represented as a series of 0s and 1s (i.e., as machine code)?</li>
<li>Question: Then, how does our C program (once it is represented as a series of 0s and 1s) end up being stored in
memory?</li>
<li>Question: Then, how does our C program (once it is represented as a series of 0s and 1s and it is stored in memory) end up being executed by the microprocessor (CPU)?</li>
</ol>
<h2 id="demo--c-program-sum_storec">Demo C program: sum_store.c</h2>
<ol>
<li>Question: How does our C program end up being represented as a series of 0s and 1s (i.e., as machine code)?</li>
</ol>
<p>Lets answer these questions with a demo</p>
<h2 id="turning-c-into-machine-code---gcc">Turning C into machine code - gcc</h2>
<p>The Big Picture (transcribers note: each step depends on the last; this is implied in the diagram):</p>
<ul>
<li>C program (.c) -&gt; sum_store.c
<ul>
<li>C Preprocessor: <code class="language-plaintext highlighter-rouge">gcc -E sum_store.c &gt; sum_store.i</code></li>
</ul>
</li>
<li>Pre-processed Source (.i) -&gt; sum_store.i
<ul>
<li>C Compiler
<ul>
<li><code class="language-plaintext highlighter-rouge">gcc -Og -S sum_store.i</code>, or</li>
<li><code class="language-plaintext highlighter-rouge">gcc -Og -S sum_store.c</code></li>
</ul>
</li>
</ul>
</li>
<li>Assembly Program (.s) -&gt; sum_store.s
<ul>
<li>Assumbler: <code class="language-plaintext highlighter-rouge">gcc -g -c main.s sum_sotre.s</code></li>
</ul>
</li>
<li>Object (.o) -&gt; sum_store.o
<ul>
<li>Linker: <code class="language-plaintext highlighter-rouge">gcc -o ss main.o sum_store.o</code></li>
</ul>
</li>
<li>Executable -&gt; <code class="language-plaintext highlighter-rouge">ss</code>
<ul>
<li>Loader: <code class="language-plaintext highlighter-rouge">./ss 5 6</code>.</li>
<li>Disassemlber: <code class="language-plaintext highlighter-rouge">objdumb -d ss</code> (gdb/ddd debugger). This will write a disassembled object file which is the only one which is a mix between plain text and data.</li>
</ul>
</li>
<li>ISA - Instruction Set Architecture</li>
<li>Computer Executes It
<ul>
<li>CPU</li>
<li>Memory</li>
</ul>
</li>
</ul>
<h2 id="snapshot-of-compiled-code">Snapshot of compiled code</h2>
<ul>
<li>C code: <code class="language-plaintext highlighter-rouge">*dest = sum;</code>
<ul>
<li>Store sum in memory designated by pointer dest</li>
</ul>
</li>
<li>Assembly code: <code class="language-plaintext highlighter-rouge">movq %rax, (%rbx)</code>
<ul>
<li>Move an 8-byte value to memory
<ul>
<li>Quad words in x86-64 parlance</li>
</ul>
</li>
<li>Operands:
<ul>
<li><code class="language-plaintext highlighter-rouge">sum</code>: register <code class="language-plaintext highlighter-rouge">%rax</code></li>
<li><code class="language-plaintext highlighter-rouge">dest</code>: register <code class="language-plaintext highlighter-rouge">%rbx</code></li>
<li><code class="language-plaintext highlighter-rouge">*dest</code>: memory <code class="language-plaintext highlighter-rouge">M[%rbx]</code></li>
</ul>
</li>
</ul>
</li>
<li>Machine code (1s and 0s): <code class="language-plaintext highlighter-rouge">0x40059e: 48 89 03</code>
<ul>
<li>3-byte instruction</li>
<li><code class="language-plaintext highlighter-rouge">48</code> stored at address <code class="language-plaintext highlighter-rouge">0x40059e</code></li>
<li><code class="language-plaintext highlighter-rouge">89</code> stored at address <code class="language-plaintext highlighter-rouge">0x40059f</code></li>
<li><code class="language-plaintext highlighter-rouge">03</code> stored at address <code class="language-plaintext highlighter-rouge">0x4005a0</code></li>
</ul>
</li>
</ul>
<h2 id="fetch-execute-cycle">Fetch-Execute Cycle</h2>
<p>Terms:</p>
<dl>
<dt>PC - program counter</dt>
<dd>Defn: register containing address of instruction of ss that is currently executing</dd>
<dt>IR - instruction register</dt>
<dd>Defn: register containing copy of instruction of ss that is currently executing</dd>
</dl>
<p>Question: How does our C program (once it is represented as a series of 0s and 1s and it is stored in memory) end up being executed by the microprocessor (CPU)?</p>
<p>Answer: The microprocessor executes the machine code version of our C program by executing the following simple loop:</p>
<p>DO FOREVER:</p>
<ul>
<li><strong>fetch</strong> next instruction from memory into CPU</li>
<li><strong>update</strong> the program counter</li>
<li><strong>decode</strong> the instruction</li>
<li><strong>execute</strong> the instruction</li>
</ul>
<h2 id="summary">Summary</h2>
<ul>
<li>Review: von Neumann architecture
<ul>
<li>Data and code are both stored in memory during program execution</li>
</ul>
</li>
</ul>
<ol>
<li>Question: How does our C program end up being represented as a series of 0s and 1s (i.e., as machine code)?
<ul>
<li>Compiler: C program -&gt; assembly code -&gt; machine level code</li>
<li>gcc: 1) C preprocessor, 2) C compiler, 3) assembler, 4) linker</li>
</ul>
</li>
<li>Question: How does our C program (once it is represented as a series of 0s and 1s) end up being stored in memory?
<ul>
<li>When C program is executed (e.g. from our demo: ./ss 5 6 )</li>
</ul>
</li>
<li>Question: How does our C program (once it is represented as a series of 0s and 1s and it is stored in memory) end up being executed by the microprocessor (CPU)?
<ul>
<li>CPU executes C program by looping through the fetch-execute cycle</li>
</ul>
</li>
</ol>
<h2 id="next-lecture">Next Lecture</h2>
<ul>
<li>Introduction
<ul>
<li>C program -&gt; assembly code -&gt; machine level code</li>
</ul>
</li>
<li>Assembly language basics: data, move operation
<ul>
<li>Memory addressing modes</li>
</ul>
</li>
<li>Operation leaq and Arithmetic &amp; logical operations</li>
<li>Conditional Statement Condition Code + <code class="language-plaintext highlighter-rouge">cmov*</code></li>
<li>Loops</li>
<li>Function call Stack</li>
<li>Array</li>
<li>Buffer Overflow</li>
<li>Floating-point operations</li>
</ul>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

File diff suppressed because one or more lines are too long

@ -0,0 +1,661 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
<link rel="stylesheet" href="/assets/css/katex.css" id="math-css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="cmpt-295-unit---machine-level-programming">CMPT 295 Unit - Machine-Level Programming</h1>
<p>Lecture 10 Assembly language basics: leaq instruction,
memory addressing modes and
arithmetic &amp; logical operations</p>
<h2 id="last-lecture">Last Lecture</h2>
<ul>
<li>As x86-64 assembly s/w dev., we now get to see more of the microprocessor
(CPU) state: PC, registers, condition codes</li>
<li>x86-64 assembly language Data
<ul>
<li>16 integer registers of 1, 2, 4 or 8 bytes + memory address of 8 bytes</li>
<li>Floating point registers of 4 or 8 bytes</li>
<li>No aggregate types such as arrays or structures</li>
</ul>
</li>
<li>x86-64 assembly language Instructions
<ul>
<li>mov* instruction family
<ul>
<li>From register to register</li>
<li>From memory to register</li>
<li>From register to memory</li>
</ul>
</li>
<li>Memory addressing modes</li>
<li>Cannot do memory-memory transfer with a single mov* instruction</li>
</ul>
</li>
</ul>
<h2 id="why-cannot-do-memory-memory-transfer-with-a-single-mov-instruction">Why cannot do memory-memory transfer with a single mov* instruction?</h2>
<ul>
<li>No x86-64 assembly instructions that take 2 memory addresses as
operands</li>
<li>Such instruction would
<ul>
<li>Makes for very long machine instructions</li>
<li>Require more complex decoder unit (on microprocessor)
in other words, require more complex microprocessor datapath</li>
<li>Memory only has one data bus and one address bus</li>
<li>No appetite for instruction set architects to create such instructions</li>
<li>Registers very fast and can easily be used for such transfer</li>
</ul>
</li>
<li>More info here: https://stackoverflow.com/questions/33794169/why-isnt-movl-frommemory-to-memory-allowed</li>
</ul>
<h2 id="last-lecture-1">Last Lecture</h2>
<p>Requirement: When reading/writing assembly code…</p>
<ul>
<li>add a comment at the top of your function in your assembly code describing the parameter-toregister mapping</li>
<li>Comment each of your assembly language instruction by explaining what it does using corresponding C statement or pseudocode</li>
</ul>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>swap:
# xp -&gt; %rdi, yp -&gt; %rsi
movq (%rdi), %rax # L1 = *xp
movq (%rsi), %rdx # L2 = *yp
movq %rdx, (%rdi) # *xp = L2
movq %rax, (%rsi) # *yp = L1
ret
</code></pre></div></div>
<h2 id="todays-menu">Todays Menu</h2>
<ul>
<li>Introduction
<ul>
<li>C program -&gt; assembly code -&gt; machine level code</li>
</ul>
</li>
<li>Assembly language basics: data, move operation
<ul>
<li>(highlighted) Memory addressing modes</li>
</ul>
</li>
<li>(highlighted) Operation leaq and Arithmetic &amp; logical operations</li>
<li>Conditional Statement Condition Code + cmov*</li>
<li>Loops</li>
<li>Function call Stack</li>
<li>Array</li>
<li>Buffer Overflow</li>
<li>Floating-point operations</li>
</ul>
<h2 id="various-types-of-operands-to-x86-64-instructions">Various types of operands to x86-64 instructions</h2>
<ol>
<li>Integer value as operand directly in an instruction
<ul>
<li>This operand is called immediate</li>
<li>Operand syntax: Imm</li>
<li>Examples: <code class="language-plaintext highlighter-rouge">movq $0x4,%rax</code> and <code class="language-plaintext highlighter-rouge">movb $-17,%al</code> (NOTE: These instructions copy immediate value to register)</li>
</ul>
</li>
<li>Registers as operands in an instruction
(Note: So far, this is the type of operands what we have seen!)
<ul>
<li>Operand value: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>R</mi><mo stretchy="false">[</mo><msub><mi>r</mi><mi>a</mi></msub><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">R[r_{a}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mopen">[</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.151392em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">a</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span></span></span></span></li>
<li>Operand syntax: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="normal">%</mi><msub><mi>r</mi><mi>a</mi></msub></mrow><annotation encoding="application/x-tex">\%r_{a}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9em;vertical-align:-0.15em;"></span><span class="mord">%</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.151392em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">a</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span> &lt;- name of particular register</li>
<li>Example: <code class="language-plaintext highlighter-rouge">movq %rax,%rdx</code> (NOTE: This instruction copies the value of one register into another register)</li>
</ul>
</li>
<li>Memory address using various memory addressing
modes as operands in an instruction</li>
</ol>
<h2 id="memory-addressing-modes">Memory addressing modes</h2>
<p>We access memory in an x86-64 instruction by expressing a memory address through various memory addressing modes</p>
<ol>
<li>Absolute memory addressing mode
<ul>
<li>Use memory address as operand directly in instruction</li>
</ul>
<ul>
<li>The operand is also called immediate
* Operand syntax: Imm
* Effect: M[Imm]
* Example: call plus (NOTE: plus refers to the memory address of the first byte of the first instruction of the function called plus (see Demo))</li>
</ul>
</li>
<li>Indirect memory addressing mode</li>
</ol>
<h2 id="2-indirect-memory-addressing-mode">2. Indirect memory addressing mode</h2>
<ul>
<li>When a register contains an address
<ul>
<li>Similar to a pointer in C</li>
</ul>
</li>
<li>To access the data at the address contained in the
register, we use parentheses (…)</li>
<li>General Syntax: (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>b</mi></msub></mrow><annotation encoding="application/x-tex">r_b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>)</li>
<li>Effect: M[R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>b</mi></msub></mrow><annotation encoding="application/x-tex">r_b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>]]</li>
</ul>
<h2 id="2-indirect-memory-addressing-mode-1">2. Indirect memory addressing mode</h2>
<h3 id="example-register-to-register">Example: register to register</h3>
<p>Assembly code: <code class="language-plaintext highlighter-rouge">movq %rdx, %rax</code></p>
<p>Meaning, or affect: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>rax</mtext><mo></mo><mtext>rdx</mtext></mrow><annotation encoding="application/x-tex">\text{rax} \leftarrow \text{rdx}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord text"><span class="mord">rax</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.69444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">rdx</span></span></span></span></span>, or <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>R[rax]</mtext><mo></mo><mtext>R[rdx]</mtext></mrow><annotation encoding="application/x-tex">\text{R[rax]} \leftarrow \text{R[rdx]}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">R[rax]</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">R[rdx]</span></span></span></span></span></p>
<table>
<thead>
<tr>
<th>Register</th>
<th>Before</th>
<th>After</th>
</tr>
</thead>
<tbody>
<tr>
<td>%rax</td>
<td>15</td>
<td>6</td>
</tr>
<tr>
<td>%rdx</td>
<td>6</td>
<td>6</td>
</tr>
<tr>
<td>M[6]</td>
<td>11</td>
<td>11</td>
</tr>
</tbody>
</table>
<h3 id="vs-memory-to-register">vs. memory to register</h3>
<p>Assembly code: <code class="language-plaintext highlighter-rouge">movq (%rdx),%rax</code></p>
<p>Meaning or affect: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>rax</mtext><mo></mo><mtext>M[rdx]</mtext></mrow><annotation encoding="application/x-tex">\text{rax} \leftarrow \text{M[rdx]}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord text"><span class="mord">rax</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">M[rdx]</span></span></span></span></span> or <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>R[rax]</mtext><mo></mo><mtext>M[R[rdx]]</mtext></mrow><annotation encoding="application/x-tex">\text{R[rax]} \leftarrow \text{M[R[rdx]]}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">R[rax]</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">M[R[rdx]]</span></span></span></span></span></p>
<table>
<thead>
<tr>
<th>Register</th>
<th>Before</th>
<th>After</th>
</tr>
</thead>
<tbody>
<tr>
<td>%rax</td>
<td>15</td>
<td>11</td>
</tr>
<tr>
<td>%rdx</td>
<td>6</td>
<td>6</td>
</tr>
<tr>
<td>M[6]</td>
<td>11</td>
<td>11</td>
</tr>
</tbody>
</table>
<p>Other examples:</p>
<ul>
<li>register to memory: <code class="language-plaintext highlighter-rouge">movq %rax,(%rdx)</code></li>
<li>immediate to memory: <code class="language-plaintext highlighter-rouge">movq $-147,(%rax)</code></li>
</ul>
<h2 id="leaq---load-effective-address-instruction">leaq - Load effective address instruction</h2>
<p>leaq has the form of an instruction that reads from memory to a register (because of the parentheses), however it <strong><em>does not</em></strong> reference memory at all!</p>
<ul>
<li>Often used for address computations and general arithmetic computations</li>
<li>Syntax: <code class="language-plaintext highlighter-rouge">leaq Source, Destination</code></li>
<li>Example:
<ol>
<li>Computing addresses
<ul>
<li>if %rax &lt;- 0x0000000000000008 and %rcx &lt;- 16</li>
<li><code class="language-plaintext highlighter-rouge">leaq (%rax, %rcx), %rdx</code></li>
<li>Once executed, rdx will contain 0x18</li>
</ul>
</li>
<li>Computing arithmetic expressions of the form <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo>+</mo><mi>k</mi><mo>×</mo><mi>y</mi></mrow><annotation encoding="application/x-tex">x + k \times y</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.77777em;vertical-align:-0.08333em;"></span><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span> where <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi><mo></mo><mo stretchy="false">[</mo><mn>1</mn><mo separator="true">,</mo><mn>2</mn><mo separator="true">,</mo><mn>4</mn><mo separator="true">,</mo><mn>8</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">k \in [1,2,4,8]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.73354em;vertical-align:-0.0391em;"></span><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord">1</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">2</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">4</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">8</span><span class="mclose">]</span></span></span></span>
<ul>
<li>if %rdi &lt;- variable a</li>
<li><code class="language-plaintext highlighter-rouge">leaq (%rdi, %rdi, 2), %rax</code> (%rdi is x, 2nd %rdi is y and 2 is k)</li>
<li>Once executed, rax will contain 3a</li>
<li>C code: <code class="language-plaintext highlighter-rouge">return a*3;</code></li>
</ul>
</li>
</ol>
</li>
<li>Operand Destination is a register</li>
<li>Operand Source is a memory addressing mode expression</li>
</ul>
<h2 id="3-base--displacement-memory-addressing-mode">3. “Base + displacement” memory addressing mode</h2>
<ul>
<li>General Syntax: Imm(<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>b</mi></msub></mrow><annotation encoding="application/x-tex">r_b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>)</li>
<li>Effect: M[Imm + R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>b</mi></msub></mrow><annotation encoding="application/x-tex">r_b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>]]</li>
<li>Examples:
<ul>
<li><code class="language-plaintext highlighter-rouge">movq %rax, -8(%rsp)</code></li>
<li><code class="language-plaintext highlighter-rouge">leaq 7(%rdi), %rax</code></li>
</ul>
</li>
<li>Careful here!
<ul>
<li>When dealing with <code class="language-plaintext highlighter-rouge">leaq</code>, the effect is Imm + R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>b</mi></msub></mrow><annotation encoding="application/x-tex">r_b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>] <strong><em>not</em></strong> M[Imm + R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>b</mi></msub></mrow><annotation encoding="application/x-tex">r_b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>]]</li>
</ul>
</li>
</ul>
<h2 id="4-indexed-memory-addressing-mode">4. Indexed memory addressing mode</h2>
<ol>
<li>General Syntax: (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>b</mi></msub></mrow><annotation encoding="application/x-tex">r_b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>,<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">r_i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>)
<ul>
<li>Effect: M[R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>b</mi></msub></mrow><annotation encoding="application/x-tex">r_b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>] + R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">r_i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>]]</li>
<li>Example: <code class="language-plaintext highlighter-rouge">movb (%rdi, %rcx), %al</code></li>
</ul>
</li>
<li>General Syntax: Imm(rb,ri)
<ul>
<li>Effect: M[Imm + R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>b</mi></msub></mrow><annotation encoding="application/x-tex">r_b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>] + R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">r_i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>]]</li>
<li>Example: <code class="language-plaintext highlighter-rouge">movw 0xA(%rdi, %rcx), %r11w</code></li>
<li>Careful here! When dealing with <code class="language-plaintext highlighter-rouge">leaq</code>, the effect is</li>
</ul>
</li>
<li>R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>b</mi></msub></mrow><annotation encoding="application/x-tex">r_b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>] + R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">r_i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>] <strong><em>not</em></strong> M[R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>b</mi></msub></mrow><annotation encoding="application/x-tex">r_b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>] + R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">r_i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>]]</li>
<li>Imm + R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>b</mi></msub></mrow><annotation encoding="application/x-tex">r_b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>] + R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">r_i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>] <strong><em>not</em></strong> M[Imm + R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>b</mi></msub></mrow><annotation encoding="application/x-tex">r_b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>] + R[<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">r_i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>]]</li>
</ol>
<h2 id="5-scaled-indexed-memory-addressing-mode">5. Scaled indexed memory addressing mode</h2>
<ol>
<li>General Syntax: (,<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">r_i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>,s)
<ul>
<li>Effect: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>M</mi><mo stretchy="false">[</mo><mi>R</mi><mo stretchy="false">[</mo><msub><mi>r</mi><mi>i</mi></msub><mo stretchy="false">]</mo><mo>×</mo><mi>s</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">M[R[r_i] \times s]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mopen">[</span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mopen">[</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mclose">]</span></span></span></span></li>
<li>Example: <code class="language-plaintext highlighter-rouge">(, %rdi, 2)</code></li>
</ul>
</li>
<li>General Syntax: Imm(,<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>r</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">r_i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.58056em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>,s)
<ul>
<li>Effect: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>M</mi><mo stretchy="false">[</mo><mtext>Imm</mtext><mo>+</mo><mi>R</mi><mo stretchy="false">[</mo><msub><mi>r</mi><mi>i</mi></msub><mo stretchy="false">]</mo><mo>×</mo><mi>s</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">M[\text{Imm} + R[r_i] \times s]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mopen">[</span><span class="mord text"><span class="mord">Imm</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mopen">[</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mclose">]</span></span></span></span></li>
<li>Example: <code class="language-plaintext highlighter-rouge">3(, %rcx, 8)</code></li>
</ul>
</li>
<li>General Syntax: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><msub><mi>r</mi><mi>b</mi></msub><mo separator="true">,</mo><msub><mi>r</mi><mi>i</mi></msub><mo separator="true">,</mo><mi>s</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(r_{b},r_{i},s)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">b</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">i</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathnormal">s</span><span class="mclose">)</span></span></span></span>
<ul>
<li>Effect: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>M</mi><mo stretchy="false">[</mo><mi>R</mi><mo stretchy="false">[</mo><msub><mi>r</mi><mi>b</mi></msub><mo stretchy="false">]</mo><mo>+</mo><mi>R</mi><mo stretchy="false">[</mo><msub><mi>r</mi><mi>i</mi></msub><mo stretchy="false">]</mo><mo>×</mo><mi>s</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">M[R[r_b] + R[r_i] \times s]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mopen">[</span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mopen">[</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mopen">[</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mclose">]</span></span></span></span></li>
<li>Example: <code class="language-plaintext highlighter-rouge">(%rdi, %rsi, 4)</code></li>
</ul>
</li>
<li>General Syntax: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Imm</mtext><mo stretchy="false">(</mo><msub><mi>r</mi><mi>b</mi></msub><mo separator="true">,</mo><msub><mi>r</mi><mi>i</mi></msub><mo separator="true">,</mo><mi>s</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{Imm}(r_{b},r_{i},s)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">Imm</span></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">b</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">i</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathnormal">s</span><span class="mclose">)</span></span></span></span>
<ul>
<li>Effect: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>M</mi><mo stretchy="false">[</mo><mtext>Imm</mtext><mo>+</mo><mi>R</mi><mo stretchy="false">[</mo><msub><mi>r</mi><mi>b</mi></msub><mo stretchy="false">]</mo><mo>+</mo><mi>R</mi><mo stretchy="false">[</mo><msub><mi>r</mi><mi>i</mi></msub><mo stretchy="false">]</mo><mo>×</mo><mi>s</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">M[\text{Imm} + R[r_b] + R[r_i] \times s]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mopen">[</span><span class="mord text"><span class="mord">Imm</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mopen">[</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mopen">[</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mclose">]</span></span></span></span></li>
<li>Example: <code class="language-plaintext highlighter-rouge">8(%rdi, %rsi, 4)</code>
13</li>
</ul>
</li>
</ol>
<p>Again, careful here! When dealing with <code class="language-plaintext highlighter-rouge">leaq</code>, the effect is <strong><em>not</em></strong> to reference memory at all!</p>
<h2 id="summary---memory-addressing-modes">Summary - Memory addressing modes</h2>
<p>We access memory in an x86-64 instruction by expressing a
memory address through various memory addressing modes</p>
<ol>
<li>Absolute</li>
<li>Indirect</li>
<li>“Base + displacement”</li>
<li>2 indexed</li>
<li>4 scaled indexed</li>
</ol>
<p>See <a href="https://cas.sfu.ca/cas/login?service=https%3a%2f%2fwww.cs.sfu.ca%2fCourseCentral%2f295%2falavergn%2fResources%2fTable%20of%20x86-64%20Addressing%20Modes.html">Table of x86-64 Addressing Modes</a> on Resources web page of our course web site</p>
<p>Box on left of slide:</p>
<ul>
<li>General Syntax: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Imm</mtext><mo stretchy="false">(</mo><msub><mi>r</mi><mi>b</mi></msub><mo separator="true">,</mo><msub><mi>r</mi><mi>i</mi></msub><mo separator="true">,</mo><mi>s</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{Imm}(r_{b}, r_{i}, s)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">Imm</span></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">b</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">i</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathnormal">s</span><span class="mclose">)</span></span></span></span></li>
<li>Effect: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>M</mi><mo stretchy="false">[</mo><mtext>Imm</mtext><mo>+</mo><mi>R</mi><mo stretchy="false">[</mo><msub><mi>r</mi><mi>b</mi></msub><mo stretchy="false">]</mo><mo>+</mo><mi>R</mi><mo stretchy="false">[</mo><msub><mi>r</mi><mi>i</mi></msub><mo stretchy="false">]</mo><mo>×</mo><mi>s</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">M[\text{Imm} + R[r_b]+ R[r_i] \times s]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">M</span><span class="mopen">[</span><span class="mord text"><span class="mord">Imm</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mopen">[</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mopen">[</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mclose">]</span></span></span></span></li>
</ul>
<h2 id="lets-try-it">Lets try it!</h2>
<table>
<thead>
<tr>
<th>Register</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>%rdx</td>
<td>0xf000</td>
</tr>
<tr>
<td>%rcx</td>
<td>0x0100</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Expression</td>
<td>Address Computation</td>
<td>Address</td>
</tr>
<tr>
<td>8(%rdx)</td>
<td><code class="language-plaintext highlighter-rouge">0xF000</code>+<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><msub><mn>8</mn><mn>10</mn></msub><mo>=</mo><mtext>0x8</mtext><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(8_{10} = \text{0x8})</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord"><span class="mord">8</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.30110799999999993em;"><span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">0x8</span></span><span class="mclose">)</span></span></span></span></td>
<td><code class="language-plaintext highlighter-rouge">0xF008</code></td>
</tr>
<tr>
<td>(%rdx,%rcx)</td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>0xF000</mtext><mo>=</mo><mtext>0x0100</mtext></mrow><annotation encoding="application/x-tex">\text{0xF000} = \text{0x0100}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">0xF000</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord text"><span class="mord">0x0100</span></span></span></span></span></td>
<td><code class="language-plaintext highlighter-rouge">0xF100</code></td>
</tr>
<tr>
<td>(%rdx,%rcx,4)</td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>0xF000</mtext><mo>+</mo><mtext>0x0100</mtext><mo>×</mo><mn>4</mn></mrow><annotation encoding="application/x-tex">\text{0xF000} + \text{0x0100} \times 4</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="mord text"><span class="mord">0xF000</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord text"><span class="mord">0x0100</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">4</span></span></span></span></td>
<td><code class="language-plaintext highlighter-rouge">0xF400</code></td>
</tr>
<tr>
<td>0x80(,%rdx,2)</td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>0x80</mtext><mo>+</mo><mtext>0xF000</mtext><mo>×</mo><mn>2</mn></mrow><annotation encoding="application/x-tex">\text{0x80} + \text{0xF000} \times 2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="mord text"><span class="mord">0x80</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="mord text"><span class="mord">0xF000</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">2</span></span></span></span></td>
<td><code class="language-plaintext highlighter-rouge">0xE080</code></td>
</tr>
<tr>
<td>0x80(%rdx, 2)</td>
<td>invalid -&gt; missing a “,” (comma) before <code class="language-plaintext highlighter-rouge">%rdx</code></td>
<td> </td>
</tr>
<tr>
<td>0x80(,%rdx, 3)</td>
<td>invalid scalar value</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="two-operand-arithmetic-instructions">Two-Operand Arithmetic Instructions</h2>
<p>* -&gt; Size designator</p>
<ul>
<li>q -&gt; long/64</li>
<li>l -&gt; int/32</li>
<li>w -&gt; short/16</li>
<li>b -&gt; char/8</li>
</ul>
<table>
<tbody>
<tr>
<td>Syntax</td>
<td>Meaning</td>
<td>Examples</td>
<td>in C</td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">add* Src, Dest</code></td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Dest</mtext><mo>=</mo><mtext>Dest</mtext><mo>+</mo><mtext>Src</mtext></mrow><annotation encoding="application/x-tex">\text{Dest} = \text{Dest} + \text{Src}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Src</span></span></span></span></span></td>
<td><code class="language-plaintext highlighter-rouge">addq %rax,%rcx</code></td>
<td><code class="language-plaintext highlighter-rouge">x+=y</code></td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">sub* Src, Dest</code></td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Dest</mtext><mo>=</mo><mtext>Dest</mtext><mo></mo><mtext>Src</mtext></mrow><annotation encoding="application/x-tex">\text{Dest} = \text{Dest} - \text{Src}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Src</span></span></span></span></span></td>
<td><code class="language-plaintext highlighter-rouge">subq %rax,%rcx</code></td>
<td>x-=y</td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">imul* Src, Dest</code></td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Dest</mtext><mo>=</mo><mtext>Dest</mtext><mo>×</mo><mtext>Src</mtext></mrow><annotation encoding="application/x-tex">\text{Dest} = \text{Dest} \times \text{Src}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Src</span></span></span></span></span></td>
<td><code class="language-plaintext highlighter-rouge">imulq $16,(%rax,%rdx,8)</code></td>
<td>x*=y</td>
</tr>
</tbody>
</table>
<ul>
<li>“destination” and “first operand” are the same
<ul>
<li>“2 operand” assembly language (machine)</li>
</ul>
</li>
<li><code class="language-plaintext highlighter-rouge">mem = mem OP mem</code> usually not supported</li>
<li>2 assembly code formats: ATT and Intel format (see Aside in Section 3.2 P. 177)
<ul>
<li>We are using the ATT format</li>
<li>Both order the operands of their instructions differently - Watch out!</li>
</ul>
</li>
</ul>
<h2 id="two-operand-logical-instructions">Two-Operand Logical Instructions</h2>
<ul>
<li>* -&gt; Size designator</li>
<li>q -&gt; long/64</li>
<li>l -&gt; int/32</li>
<li>w -&gt; short/16</li>
<li>b -&gt; char/8</li>
</ul>
<p>Transcribers note: the operators used for the logical operation in the following table are the proper mathematical symbols, not what is written in the document.
The symbols used in the document are: &amp;, | and ^ respectively.</p>
<table>
<thead>
<tr>
<th>Syntax</th>
<th>Meaning</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr>
<td><code class="language-plaintext highlighter-rouge">and* Src, Dest</code></td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Dest</mtext><mo>=</mo><mtext>Dest</mtext><mo></mo><mtext>Src</mtext></mrow><annotation encoding="application/x-tex">\text{Dest} = \text{Dest} \land \text{Src}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Src</span></span></span></span></span></td>
<td><code class="language-plaintext highlighter-rouge">andl $252645135, %edi</code></td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">or* Src, Dest</code></td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Dest</mtext><mo>=</mo><mtext>Dest</mtext><mo></mo><mtext>Src</mtext></mrow><annotation encoding="application/x-tex">\text{Dest} = \text{Dest} \lor \text{Src}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Src</span></span></span></span></span></td>
<td><code class="language-plaintext highlighter-rouge">org %rsi,%rdi</code></td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">xor* Src, Dest</code></td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Dest</mtext><mo>=</mo><mtext>Dest</mtext><mo></mo><mtext>Src</mtext></mrow><annotation encoding="application/x-tex">\text{Dest} = \text{Dest} \oplus \text{Src}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Src</span></span></span></span></span></td>
<td><code class="language-plaintext highlighter-rouge">xorq %rsi,%rdi</code></td>
</tr>
</tbody>
</table>
<ul>
<li>xorq special purpose:
<ul>
<li><code class="language-plaintext highlighter-rouge">xorq %rax, %rax</code>: zeroes register %rax</li>
<li><code class="language-plaintext highlighter-rouge">movq $0, %rax</code>: also zeroes register %rax</li>
</ul>
</li>
<li>x86-64 convention:
<ul>
<li>Any instruction updating the lower 4 bytes will cause the higher-order bytes to be set to 0</li>
<li><code class="language-plaintext highlighter-rouge">xorl %eax, %eax</code> and <code class="language-plaintext highlighter-rouge">movl $0, %eax</code> &lt;- also zeroes register <code class="language-plaintext highlighter-rouge">%rax</code></li>
</ul>
</li>
</ul>
<h2 id="two-operand-shift-instructions">Two-Operand Shift Instructions</h2>
<ul>
<li>* -&gt; Size designator</li>
<li>q -&gt; long/64</li>
<li>l -&gt; int/32</li>
<li>w -&gt; short/16</li>
<li>b -&gt; char/8</li>
</ul>
<table>
<tbody>
<tr>
<td>Syntax</td>
<td>Meaning</td>
<td>Example</td>
<td>Note</td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">sal* Src, Dest</code></td>
<td>Dest &lt;- Dest « Src</td>
<td><code class="language-plaintext highlighter-rouge">salq $4,%rax</code></td>
<td>Left shiftalso called <code class="language-plaintext highlighter-rouge">shlq</code>: filling Dest with 0, from the right</td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">sar* Src, Dest</code></td>
<td>Dest &lt;- » Src</td>
<td><code class="language-plaintext highlighter-rouge">sarl %cl,%rax</code></td>
<td>Right arithmatic Shift: filling Dest with sign bit, form the left.</td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">shr* Src, Dest</code></td>
<td>Dest &lt;- Dest » Src</td>
<td><code class="language-plaintext highlighter-rouge">shrq $2,%r8</code></td>
<td>Right logical Shift: filling Rest with 0, from the left</td>
</tr>
</tbody>
</table>
<h2 id="one-operand-arithmetic-instructions">One-Operand Arithmetic Instructions</h2>
<ul>
<li>* -&gt; Size designator</li>
<li>q -&gt; long/64</li>
<li>l -&gt; int/32</li>
<li>w -&gt; short/16</li>
<li>b -&gt; char/8</li>
</ul>
<table>
<tbody>
<tr>
<td>Syntax</td>
<td>Meaning</td>
<td>Examples</td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">inc* Dest</code></td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Dest</mtext><mo>=</mo><mtext>Dest</mtext><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">\text{Dest} = \text{Dest}+1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></td>
<td><code class="language-plaintext highlighter-rouge">incq (%rsp)</code></td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">dec* Dest</code></td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Dest</mtext><mo>=</mo><mtext>Dest</mtext><mo></mo><mn>1</mn></mrow><annotation encoding="application/x-tex">\text{Dest} = \text{Dest}-1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></td>
<td><code class="language-plaintext highlighter-rouge">decq %rsi</code></td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">neg* Dest</code></td>
<td><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Dest</mtext><mo>=</mo><mo></mo><mtext>Dest</mtext></mrow><annotation encoding="application/x-tex">\text{Dest} = -\text{Dest}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Dest</span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="mord"></span><span class="mord text"><span class="mord">Dest</span></span></span></span></span></td>
<td><code class="language-plaintext highlighter-rouge">negl %eax</code></td>
</tr>
<tr>
<td><code class="language-plaintext highlighter-rouge">not* Dest</code></td>
<td>Dest = ~Dest</td>
<td><code class="language-plaintext highlighter-rouge">notq %rdi</code></td>
</tr>
</tbody>
</table>
<h2 id="summary">Summary</h2>
<ul>
<li><code class="language-plaintext highlighter-rouge">leaq</code> - load effective address instruction</li>
<li>Various types of operands to x86-64 instructions
<ul>
<li>Immediate (constant integral value)</li>
<li>Register (16 registers)</li>
<li>Memory address (various memory addressing modes)
<ul>
<li>General Syntax: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Imm</mtext><mo stretchy="false">(</mo><msub><mi>r</mi><mi>b</mi></msub><mo separator="true">,</mo><msub><mi>r</mi><mi>i</mi></msub><mo separator="true">,</mo><mi>s</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{Imm}(r_{b}, r_{i}, s)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">Imm</span></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">b</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">i</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathnormal">s</span><span class="mclose">)</span></span></span></span></li>
</ul>
</li>
</ul>
</li>
<li>Arithmetic &amp; logical operations
<ul>
<li>Arithmetic instructions: <code class="language-plaintext highlighter-rouge">add*</code>, <code class="language-plaintext highlighter-rouge">sub*</code>, <code class="language-plaintext highlighter-rouge">imul*</code>, <code class="language-plaintext highlighter-rouge">inc*</code>, <code class="language-plaintext highlighter-rouge">dec*</code>, <code class="language-plaintext highlighter-rouge">neg*</code>, <code class="language-plaintext highlighter-rouge">not*</code></li>
<li>Logical instructions: <code class="language-plaintext highlighter-rouge">and*</code>, <code class="language-plaintext highlighter-rouge">or*</code>, <code class="language-plaintext highlighter-rouge">xor*</code></li>
<li>Shift instructions: <code class="language-plaintext highlighter-rouge">sal*</code>, <code class="language-plaintext highlighter-rouge">sar*</code>, <code class="language-plaintext highlighter-rouge">shr*</code></li>
</ul>
</li>
</ul>
<h2 id="next-lecture">Next lecture</h2>
<ul>
<li>Introduction
<ul>
<li>C program -&gt; assembly code -&gt; machine level code</li>
</ul>
</li>
<li>Assembly language basics: data, move operation
<ul>
<li>Memory addressing modes</li>
</ul>
</li>
<li>Operation <code class="language-plaintext highlighter-rouge">leaq</code> and Arithmetic &amp; logical operations</li>
<li>Conditional Statement Condition Code + cmov*</li>
<li>Loops</li>
<li>Function call Stack</li>
<li>Array</li>
<li>Buffer Overflow</li>
<li>Floating-point operations</li>
</ul>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

@ -0,0 +1,130 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
<link rel="stylesheet" href="/assets/css/katex.css" id="math-css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="cmpt-295">CMPT 295</h1>
<ul>
<li>Unit - Machine-Level Programming</li>
<li>Lecture 11 Assembly language basics: Practice and DEMO
-&gt; leaq and arithmetic &amp; logical instructions and
memory addressing modes</li>
</ul>
<h2 id="why-did-the-programmer-quit-their-job">Why did the programmer quit their job?</h2>
<p>A never got arrays!</p>
<h2 id="summary">Summary</h2>
<ul>
<li><code class="language-plaintext highlighter-rouge">leaq</code> - load effective address instruction</li>
<li>Using data as operand to an instruction:
<ul>
<li>Immediate (constant integral value)</li>
<li>Register (16 registers)</li>
<li>Memory (various memory addressing modes)
<ul>
<li>General Syntax: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Imm</mtext><mo stretchy="false">(</mo><msub><mi>r</mi><mi>b</mi></msub><mo separator="true">,</mo><msub><mi>r</mi><mi>i</mi></msub><mo separator="true">,</mo><mi>s</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{Imm}(r_{b}, r_{i}, s)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">Imm</span></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.33610799999999996em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">b</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.31166399999999994em;"><span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">i</span></span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathnormal">s</span><span class="mclose">)</span></span></span></span></li>
</ul>
</li>
</ul>
</li>
<li>Arithmetic &amp; logical operations
<ul>
<li>Arithmetic instructions: add*, sub*, imul* inc*, dec*, neg*, not*</li>
<li>Logical instructions: and*, or*, xor*</li>
<li>Shift instructions: sal*, sar*, shr*</li>
</ul>
</li>
</ul>
<h2 id="todays-menu">Todays Menu</h2>
<ul>
<li>Introduction
<ul>
<li>C program -&gt; assembly code -&gt; machine level code</li>
</ul>
</li>
<li>Assembly language basics: data, move operation
<ul>
<li>(selected) Memory addressing modes</li>
</ul>
</li>
<li>(selected) Operation leaq and Arithmetic &amp; logical operations (Annotation: practice and DEMO!)</li>
<li>Conditional Statement Condition Code + cmov*</li>
<li>Loops</li>
<li>Function call Stack</li>
<li>Array</li>
<li>Buffer Overflow</li>
<li>Floating-point operations</li>
</ul>
<h2 id="demo">Demo</h2>
<ol>
<li><code class="language-plaintext highlighter-rouge">gcc</code> uses <code class="language-plaintext highlighter-rouge">leaq</code> for addition -&gt; <code class="language-plaintext highlighter-rouge">sum_store.c</code></li>
<li>Writing our own assembly code (<code class="language-plaintext highlighter-rouge">arith.s</code>) using arithmetic
instructions of x86-64 assembly language</li>
<li><code class="language-plaintext highlighter-rouge">makefile</code>
<ul>
<li>when we compile our own *.s files with *.c files</li>
<li>when we compile only *.c files</li>
</ul>
</li>
<li>How would <code class="language-plaintext highlighter-rouge">gcc</code> compile our <code class="language-plaintext highlighter-rouge">arith.c</code> into <code class="language-plaintext highlighter-rouge">arith.s</code>?</li>
</ol>
<h2 id="summary-1">Summary</h2>
<ul>
<li>Demo
<ul>
<li>Observation: C compiler will figure out different instruction combinations to carry out the computations in our C code</li>
</ul>
</li>
</ul>
<h2 id="next-lecture">Next lecture</h2>
<ul>
<li>Introduction
<ul>
<li>C program -&gt; assembly code -&gt; machine level code</li>
</ul>
</li>
<li>Assembly language basics: data, move operation
<ul>
<li>Memory addressing modes</li>
</ul>
</li>
<li>Operation leaq and Arithmetic &amp; logical operations</li>
<li>Conditional Statement Condition Code + cmov*</li>
<li>Loops</li>
<li>Function call Stack</li>
<li>Array</li>
<li>Buffer Overflow</li>
<li>Floating-point operations</li>
</ul>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

@ -0,0 +1,332 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
<link rel="stylesheet" href="/assets/css/katex.css" id="math-css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="cmpt-295">CMPT 295</h1>
<ul>
<li>Unit - Machine-Level Programming</li>
<li>Lecture 12 Assembly language Program Control Conditional Statements</li>
</ul>
<h2 id="last-lecture">Last Lecture</h2>
<ul>
<li>Demo
<ul>
<li>Observation: C compiler will figure out different instruction combinations to carry out the computations in our C code</li>
</ul>
</li>
</ul>
<h2 id="todays-menu">Todays Menu</h2>
<ul>
<li>Introduction
<ul>
<li>C program -&gt; assembly code -&gt; machine level code</li>
</ul>
</li>
<li>Assembly language basics: data, move operation
<ul>
<li>Memory addressing modes</li>
</ul>
</li>
<li>Operation leaq and Arithmetic &amp; logical operations</li>
<li>Conditional Statement Condition Code + cmovX (selected)</li>
<li>Loops</li>
<li>Function call Stack</li>
<li>Array</li>
<li>Buffer Overflow</li>
<li>Floating-point operations</li>
</ul>
<h2 id="completing-our-demo">Completing our Demo</h2>
<ol>
<li>(checked) <code class="language-plaintext highlighter-rouge">gcc</code> uses <code class="language-plaintext highlighter-rouge">leaq</code> for addition -&gt; <code class="language-plaintext highlighter-rouge">sum_store.c</code> our own assembly code (arith.s) using arithmetic</li>
<li>(checked) Writing instructions of x86-64 assembly language</li>
<li>makefile
<ul>
<li>when we compile our own *.s files with *.c files</li>
<li>when we compile only *.c files</li>
</ul>
</li>
<li>How would <code class="language-plaintext highlighter-rouge">gcc</code> compile our <code class="language-plaintext highlighter-rouge">arith.c</code> into <code class="language-plaintext highlighter-rouge">arith.s</code>?</li>
</ol>
<h2 id="program-control-overview">Program Control Overview</h2>
<ul>
<li>We can change the execution flow of a program
<ol>
<li>Based on a condition</li>
<li>Unconditionally</li>
</ol>
</li>
<li>Control statements (in C)
<ul>
<li>if/else</li>
<li>switch</li>
<li>cmp* instruction (compare)</li>
<li>for loop</li>
</ul>
</li>
<li>Control statements (in x86-64 assemvly)
<ul>
<li>cmp* instruction (compare)</li>
<li>jX instructions (jump)</li>
</ul>
</li>
</ul>
<p>Function calls -&gt; <code class="language-plaintext highlighter-rouge">call</code> and <code class="language-plaintext highlighter-rouge">ret</code></p>
<h2 id="conditional-statement-ifelse">Conditional statement: <code class="language-plaintext highlighter-rouge">if/else</code></h2>
<p>in C:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>void func(long x,long y){
if ( x &lt; y ) {
// stmts true
} else {
// stmts false
}
return;
}
</code></pre></div></div>
<p>A label is a memory address.</p>
<p>in assembly:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># func is a label
func:
cmpq %rsi,%rdi # x y
jge else #
... # stmts true
jmp endif #
# else is a label
else: ... # stmts false
# endif is a label
endif: ret #
</code></pre></div></div>
<p>We branch (jump) when the condition is false -&gt; This technique is called “coding the false condition first”</p>
<h2 id="comparison-instructions">comparison instructions</h2>
<ul>
<li>* -&gt; Size designator</li>
<li>q -&gt; long/64-bits</li>
<li>l -&gt; int/32-bits</li>
<li>w -&gt; short/16-bits</li>
<li>b -&gt; char/8-bits</li>
</ul>
<p>Remember in Lecture 9, we saw… (See header “Programming in x86-64 assembly” from lecture 9)</p>
<table>
<thead>
<tr>
<th>Syntax</th>
<th>Meaning/Effect</th>
<th>Example</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>cmp* Src2, Src1</td>
<td>Src1 Src2 -&gt;<br />&gt; 0? -&gt; Src1 &gt; Src2<br />= 0? -&gt; Src1 == Src2<br />&lt; 0? -&gt; Src1 &lt; Src2</td>
<td><code class="language-plaintext highlighter-rouge">cmpq %rsi,%rdi</code></td>
<td>without saving the result in the destination operand (no Dest). Sets <strong>condition codes</strong> based on value of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Src1</mtext><mo></mo><mtext>Src2</mtext></mrow><annotation encoding="application/x-tex">\text{Src1} - \text{Src2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="mord text"><span class="mord">Src1</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Src2</span></span></span></span></span></td>
</tr>
<tr>
<td>test* Src2, Src1</td>
<td>Src1 &amp; Src2 -&gt;</td>
<td><code class="language-plaintext highlighter-rouge">testq %rax,%rax</code></td>
<td>without saving the result in the destination operand (no <strong>Dest</strong>); sets <strong>condition code</strong> based on value of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Src1</mtext><mo></mo><mtext>Src2</mtext></mrow><annotation encoding="application/x-tex">\text{Src1} \land \text{Src2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Src1</span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord text"><span class="mord">Src2</span></span></span></span></span>; useful when one of the operands is a bit mask</td>
</tr>
</tbody>
</table>
<h2 id="jx-jump-family-instructions-branching"><code class="language-plaintext highlighter-rouge">jX</code> jump family instructions (branching)</h2>
<ul>
<li>Jump to different part of the program depending on result of previous instructions (i.e., condition codes)</li>
</ul>
<table>
<thead>
<tr>
<th><code class="language-plaintext highlighter-rouge">jX</code></th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>jmp</td>
<td>unconditional</td>
</tr>
<tr>
<td>je</td>
<td>Equal / Zero</td>
</tr>
<tr>
<td>jne</td>
<td>Not Equal / Not Zero</td>
</tr>
<tr>
<td>js</td>
<td>Negative</td>
</tr>
<tr>
<td>jns</td>
<td>Nonnegative</td>
</tr>
<tr>
<td>jg</td>
<td>Greater (Signed)</td>
</tr>
<tr>
<td>jge</td>
<td>Greater or Equal (Signed)</td>
</tr>
<tr>
<td>jl</td>
<td>Less (Signed)</td>
</tr>
<tr>
<td>jle</td>
<td>Less or Equal (Signed</td>
</tr>
<tr>
<td>ja</td>
<td>Above (unsigned)</td>
</tr>
<tr>
<td>jb</td>
<td>Below (unsigned)</td>
</tr>
</tbody>
</table>
<h2 id="example--int-absint-x">Example <code class="language-plaintext highlighter-rouge">int abs(int x)</code></h2>
<p>in C:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>int abs(int x){
if(x&lt;0)
x=-x;
return x;
}
</code></pre></div></div>
<p>in assembly:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># x in edi, result in eax
abs:
movl %edi,%eax # eax &lt;- x
____ #
____ # ret if x &gt;= 0
____ # x = -x
endif:
ret
</code></pre></div></div>
<h2 id="int-maxint-x-int-y---homework"><code class="language-plaintext highlighter-rouge">int max(int x, int y</code> )- Homework</h2>
<p>In C:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>int max(int x, int y){
int result=x;
if(y&gt;x)
result=y;
return result;
}
</code></pre></div></div>
<p>In assembly:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># x in edi, y in esi, result in eax
max:
movl %edi,%eax # result = x
____
____
____
____
____
endif:
ret
</code></pre></div></div>
<h2 id="summary">Summary</h2>
<ul>
<li>In C, we can change the execution flow of a program
<ol>
<li>Conditionaly
<ul>
<li>Conditional statements: if/else, switch</li>
<li>Iterative statements: loops</li>
</ul>
</li>
<li>Unconditionally
<ul>
<li>Functions calls</li>
</ul>
</li>
</ol>
</li>
<li>In x86-64 assembly, we can also change the execution flow of a program
<ul>
<li><code class="language-plaintext highlighter-rouge">cmp*</code> instruction (compare)</li>
<li><code class="language-plaintext highlighter-rouge">jX</code> instructions (jump)</li>
<li><code class="language-plaintext highlighter-rouge">call</code> and <code class="language-plaintext highlighter-rouge">ret</code> instructions</li>
</ul>
</li>
</ul>
<h2 id="next-lecture">Next Lecture</h2>
<ul>
<li>Introduction
<ul>
<li>C program -&gt; assembly code -&gt; machine level code</li>
</ul>
</li>
<li>Assembly language basics: data, move operation
<ul>
<li>Memory addressing modes</li>
</ul>
</li>
<li>Operation leaq and Arithmetic &amp; logical operations</li>
<li>Conditional Statement Condition Code + cmovX</li>
<li>(selected) Loops</li>
<li>Function call Stack</li>
<li>Array</li>
<li>Buffer Overflow</li>
<li>Floating-point operations</li>
</ul>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

@ -0,0 +1,452 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> | tait.tech</title>
<link rel="stylesheet" href="/assets/css/style.css" id="main-css">
<link rel="stylesheet" href="/assets/css/transcription.css" id="trans-css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/assets/js/"></script>
<link rel="stylesheet" href="/assets/css/katex.css" id="math-css">
</head>
<body>
<main>
<div id="wrapper">
<h1 id="cmpt-295">CMPT 295</h1>
<ul>
<li>Unit - Machine-Level Programming</li>
<li>Lecture 13 Assembly language Program Control cmovX</li>
<li>Iterative Statements Loops</li>
</ul>
<h2 id="last-lecture">Last Lecture</h2>
<ul>
<li>In C, we can change the execution flow of a program
<ol>
<li>Conditionaly
<ul>
<li>Conditional statements: if/else, switch</li>
<li>Iterative statements: loops</li>
</ul>
</li>
<li>Unconditionally
<ul>
<li>Functions calls</li>
</ul>
</li>
</ol>
</li>
<li>In x86-64 assembly, we can also change the execution flow of a program
<ul>
<li>cmp* instruction (compare)</li>
<li>jX insturction (jump)</li>
<li>call and ret instructions</li>
</ul>
</li>
</ul>
<h2 id="todays-menu">Todays Menu</h2>
<ul>
<li>Introduction
<ul>
<li>C program -&gt; assembly code -&gt; machine level code</li>
</ul>
</li>
<li>Assembly language basics: data, move operation
<ul>
<li>Memory addressing modes</li>
</ul>
</li>
<li>Operation leaq and Arithmetic &amp; logical operations</li>
<li>Conditional Statement Condition Code + cmovX</li>
<li>(highlighted) Loops</li>
<li>Function call Stack</li>
<li>Array</li>
<li>Buffer Overflow</li>
<li>Floating-point operations</li>
</ul>
<h2 id="homework-int-maxint-x-int-y">Homework: <code class="language-plaintext highlighter-rouge">int max(int x, int y)</code></h2>
<p>In C:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>int max(int x,int y){
int result = x;
if(y&gt;x)
result=y;
return result;
}
</code></pre></div></div>
<p>version 1 with jX instruction</p>
<p>In Assembly: # x in %edi, y in %esi, result in %eax</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>max:
movl %edi,%eax #result=x
cmpl %edi,%esi #if y&lt;=x then
jle endif #return
movl %esi,%eax #result=y
endif:
ret
</code></pre></div></div>
<p>We branch (jump) when the condition (y &gt; x) is false, i.e., when (y &lt;= x)
-&gt; This technique is called “coding the false condition first”
or ”taking care of …”</p>
<h2 id="conditional-move-instruction-cmovx">Conditional move instruction cmovX</h2>
<p>What C code looks like when using conditional operator:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>result=test?val2:val1;
return result;
</code></pre></div></div>
<p>What logic of assembly
code looks like when using
cmovX (expressed in C):</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>result=val1;
if (test) result = val2;
return result;
</code></pre></div></div>
<p>Example: <code class="language-plaintext highlighter-rouge">cmovle Src,Dest</code></p>
<p>Alternative: <code class="language-plaintext highlighter-rouge">int abs(int x)</code></p>
<p>In C:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>int abs(int x){
if(x&lt;0)
x=-x;
return x;
}
</code></pre></div></div>
<p>in assembly: # <code class="language-plaintext highlighter-rouge">x</code> in <code class="language-plaintext highlighter-rouge">%edi</code>, <code class="language-plaintext highlighter-rouge">result</code> in <code class="language-plaintext highlighter-rouge">%eax</code></p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>abs:
movl %edi,%eax # result = x
negl %edi # x = -x
cmpl $0,%eax # if x &lt; 0 then
cmovl %edi,%eax # result = -x
ret
</code></pre></div></div>
<h2 id="advantage-of-conditional-move-cmovx">Advantage of conditional move cmovX</h2>
<p>Note about branching:</p>
<ul>
<li>Branches are very disruptive to instruction flow through
microprocessor CPU pipelines</li>
<li>However, since conditional moves (cmovX) do not
require control transfer (no branching/jumping required),
they are less disruptive</li>
<li>So, <code class="language-plaintext highlighter-rouge">gcc</code> tries to use them, but only when safe</li>
</ul>
<h2 id="what-do-we-mean-by-safe">What do we mean by “safe”?</h2>
<ul>
<li>In <code class="language-plaintext highlighter-rouge">result = test ? aVal : anotherVal;</code> both values
(<code class="language-plaintext highlighter-rouge">aVal</code> and <code class="language-plaintext highlighter-rouge">anotherVal</code>) are computed so their
computation must be “safe”</li>
<li>Example of unsafe computations:
<ol>
<li>Expensive computations <code class="language-plaintext highlighter-rouge">val = Test(x) ? Hard1(x) : Hard2(x);</code>
<ul>
<li>Only makes sense when computations are very simple</li>
</ul>
</li>
<li>Risky computations <code class="language-plaintext highlighter-rouge">val = p ? *p : 0;</code>
<ul>
<li>Only makes sense when computations do not crash the application</li>
</ul>
</li>
<li>Computations with side effects <code class="language-plaintext highlighter-rouge">val = x &gt; 0 ? x*=7 : x+=3;</code>
<ul>
<li>Only makes sense when computations do not have side effects</li>
</ul>
</li>
</ol>
</li>
</ul>
<h2 id="homework-example-alternate-int-maxint-x-int-y">Homework: Example: alternate <code class="language-plaintext highlighter-rouge">int max(int x, int y)</code></h2>
<p>version 2 with cmovX instruction</p>
<p>In C:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>int max(int x, int y){
int result = x;
if(y&gt;x)
result = y;
return result;
}
</code></pre></div></div>
<p>In Assembly: # x in %edi, y in %esi, result in %eax</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>max:
movl %edi,%eax #result=x
cmpl %edi,%esi #if y&gt;x then
cmovg %esi,%eax #result=y
ret
</code></pre></div></div>
<h2 id="while-loop--coding-the-false-condition-first-updated">While loop “coding the false condition first” (updated)</h2>
<p><code class="language-plaintext highlighter-rouge">int x</code> and <code class="language-plaintext highlighter-rouge">int y</code> are arguments to function</p>
<p>in C:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>while(x&lt;y){
//stmts
}
return;
</code></pre></div></div>
<p>Steps:</p>
<ol>
<li>Analyze the condition: <code class="language-plaintext highlighter-rouge">x&lt;y</code></li>
<li>write <code class="language-plaintext highlighter-rouge">cmpl %esi,%edi</code></li>
<li>List all outcomes:
<ul>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo></mo><mi>y</mi><mo>&gt;</mo><mn>0</mn><mtext></mtext><mo></mo><mtext></mtext><mi>x</mi><mo>&gt;</mo><mi>y</mi></mrow><annotation encoding="application/x-tex">x-y &gt; 0 \implies x&gt;y</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.7335400000000001em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.66844em;vertical-align:-0.024em;"></span><span class="mord">0</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.5782em;vertical-align:-0.0391em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span>: exits loop</li>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo></mo><mi>y</mi><mo>&lt;</mo><mn>0</mn><mtext></mtext><mo></mo><mtext></mtext><mi>x</mi><mo>&lt;</mo><mi>y</mi></mrow><annotation encoding="application/x-tex">x-y &lt; 0 \implies x&lt; y</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.7335400000000001em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">&lt;</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.66844em;vertical-align:-0.024em;"></span><span class="mord">0</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.5782em;vertical-align:-0.0391em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">&lt;</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span>: loops</li>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo></mo><mi>y</mi><mo>=</mo><mn>0</mn><mtext></mtext><mo></mo><mtext></mtext><mi>x</mi><mo>=</mo><mi>y</mi></mrow><annotation encoding="application/x-tex">x-y = 0 \implies x=y</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.66844em;vertical-align:-0.024em;"></span><span class="mord">0</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span>: exits loop</li>
</ul>
</li>
<li>Which outcomes “exit loop”?</li>
<li>Write for false condition i.e. g &amp; e (<code class="language-plaintext highlighter-rouge">jge</code>)</li>
</ol>
<p>in assembly:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>loop: # x in %edi, y in %esi
cmpl %esi,%edi # cmpl y,x
jge endloop
#stmts in loop
jmp loop
endloop:
ret
</code></pre></div></div>
<p>Loop Pattern 1</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>loop:
if cond false
goto done:
stmts
goto loop:
done:
</code></pre></div></div>
<h2 id="while-loop--jump-to-middle-updated">While loop “jump-to-middle” (updated)</h2>
<p><code class="language-plaintext highlighter-rouge">int x</code> and <code class="language-plaintext highlighter-rouge">int y</code> are arguments to function</p>
<p>in C:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>while(x&lt;y){ //step 1
//stmts
}
return;
</code></pre></div></div>
<p>Steps:</p>
<ol>
<li>(shown in C code)</li>
<li>(shwon in assembly)</li>
<li>All possible values
<ul>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo></mo><mi>y</mi><mo>&gt;</mo><mn>0</mn><mtext></mtext><mo></mo><mtext></mtext><mi>x</mi><mo>&gt;</mo><mi>y</mi></mrow><annotation encoding="application/x-tex">x-y &gt;0 \implies x&gt;y</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.7335400000000001em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.66844em;vertical-align:-0.024em;"></span><span class="mord">0</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.5782em;vertical-align:-0.0391em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span>: exits loop, g</li>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo></mo><mi>y</mi><mo>&lt;</mo><mn>0</mn><mtext></mtext><mo></mo><mtext></mtext><mi>x</mi><mo>&lt;</mo><mi>y</mi></mrow><annotation encoding="application/x-tex">x-y &lt; 0 \implies x&lt; y</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.7335400000000001em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">&lt;</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.66844em;vertical-align:-0.024em;"></span><span class="mord">0</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.5782em;vertical-align:-0.0391em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">&lt;</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span>: loops, l</li>
<li><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo></mo><mi>y</mi><mo>=</mo><mn>0</mn><mtext></mtext><mo></mo><mtext></mtext><mi>x</mi><mo>=</mo><mi>y</mi></mrow><annotation encoding="application/x-tex">x-y = 0 \implies x=y</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.66666em;vertical-align:-0.08333em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin"></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.66844em;vertical-align:-0.024em;"></span><span class="mord">0</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span>: exits loops, e</li>
</ul>
</li>
<li>Consider outcome ???(cant read) “loops”</li>
</ol>
<p>in assembly:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> jmp test
loop:
# stmts
test:
cmpl %esi,%edi #cmpl y,x (step 2)
jl loop (step 5)
ret
</code></pre></div></div>
<p>Loop Pattern 2</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> goto test:
loop:
# stmts
test:
if cond true
goto loop:
done:
</code></pre></div></div>
<h2 id="do-while-loop--jump-to-middle-updated">Do While loop “jump-to-middle” (updated)</h2>
<p><code class="language-plaintext highlighter-rouge">int x</code> and <code class="language-plaintext highlighter-rouge">int y</code> are arguments to function</p>
<p>in C:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>do{
stmts
} while(x&lt;y);
return;
</code></pre></div></div>
<p>in assembly:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>loop:
#stmts
test:
jmpl %esi,%edi
jl loop
ret
</code></pre></div></div>
<p>Loop Pattern 2:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(deleted) goto test:
loop:
stmts
test:
if cond true
goto loop:
done:
</code></pre></div></div>
<h2 id="for-loop-updated">For loop (updated)</h2>
<p>In C:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>//format: for(initialization; condition testing; increment)
for(int i=0;i&lt;n;++i){
//stmts
}
return;
</code></pre></div></div>
<p>Becomes:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>i=0; // initialization
while(i&lt;n){// condition testing
//stmts
i++; //increment
}
return;
</code></pre></div></div>
<p>Questions:</p>
<ol>
<li>Here are we coding the false condition first?</li>
<li>Which loop patterns are we using?</li>
</ol>
<p>Which becomes, in Assembly:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> xorl %ecx, %ecx # initialization
loop: # %ecx (i) &lt;- 0
cmpl %edi, %ecx # while i &lt; n true (testing)
jge endloop # jump when i&gt;=n (false condition); In this situation "je" would also work. Do you see why?
#stmts
incl %ecx # i++ increment
jmp loop # loop again
endloop:
ret
</code></pre></div></div>
<h2 id="summary">Summary</h2>
<p>Compiler can produce different instruction combinations when assembling the same C code.</p>
<p><code class="language-plaintext highlighter-rouge">cmp*</code> and <code class="language-plaintext highlighter-rouge">test*</code> instructions set condition codes</p>
<ul>
<li>In x86-64 assembly, there are no conditional statements, however,
we can alter the execution flow of a program by using …
<ul>
<li><code class="language-plaintext highlighter-rouge">cmp*</code> instruction (compare)</li>
<li><code class="language-plaintext highlighter-rouge">jX</code> instructions (jump)</li>
<li><code class="language-plaintext highlighter-rouge">call</code> and ret instructions</li>
<li><code class="language-plaintext highlighter-rouge">cmovX</code> instructions -&gt; conditional move</li>
</ul>
</li>
<li>In x86-64 assembly, there are no iterative statements, however, we
can alter the execution flow of a program by using …
<ul>
<li><code class="language-plaintext highlighter-rouge">cmp*</code> instruction</li>
<li><code class="language-plaintext highlighter-rouge">jX</code> instructions (jump)</li>
</ul>
</li>
<li>CPU uses these condition codes to decide whether a …
<ul>
<li><code class="language-plaintext highlighter-rouge">jX</code> instruction (conditional jump) is to be exectued or a</li>
<li><code class="language-plaintext highlighter-rouge">cmovX</code> instruction (conditional move) is to be exectued</li>
</ul>
</li>
<li>2 loop patterns:
<ul>
<li>“coding the false condition first” -&gt; <code class="language-plaintext highlighter-rouge">while</code> loops (hence <code class="language-plaintext highlighter-rouge">for</code> loops)</li>
<li>“jump-in-middle” -&gt; <code class="language-plaintext highlighter-rouge">while</code>, <code class="language-plaintext highlighter-rouge">do-while</code> (hence for loops)</li>
</ul>
</li>
</ul>
<h2 id="next-lecture">Next Lecture</h2>
<ul>
<li>Introduction
<ul>
<li>C program -&gt; assembly code -&gt; machine level code</li>
</ul>
</li>
<li>Assembly language basics: data, move operation
<ul>
<li>Memory addressing modes</li>
</ul>
</li>
<li>Operation leaq and Arithmetic &amp; logical operations</li>
<li>Conditional Statement Condition Code + cmovX</li>
<li>Loops</li>
<li>(highlighted) Function call Stack
<ul>
<li>(highlighted) Overview of Function Call</li>
<li>(highlighted) Memory Layout and Stack - x86-64 instructions and registers</li>
<li>(highlighted) Passing control</li>
<li>Passing data Calling Conventions</li>
<li>Managing local data</li>
</ul>
</li>
<li>Array</li>
<li>Buffer Overflow</li>
<li>Floating-point operations</li>
</ul>
</div>
</main>
<hr>
<footer>
</footer>
</body>
</html>

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save