diff --git a/_site/2020/01/22/padding-and-margin/index.html b/_site/2020/01/22/padding-and-margin/index.html new file mode 100644 index 0000000..828e456 --- /dev/null +++ b/_site/2020/01/22/padding-and-margin/index.html @@ -0,0 +1,96 @@ + + + + + Padding And Margin | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

Padding And Margin

+ +
+
+ +

Many people have expressed confusion over how padding and margins work in HTML/CSS. I have been one of those people. In this short article I will explain what the differences are between the two, and how it may affect the functionality of your site.

+ +

Here is an image from the World Wide Web Consortium (W3C) who sets the standards for the web.

+ +

The W3C standard for padding, margin, borders and width. Width encompases the inner element + padding; the border then encompases it. The margin is the space beyond the border and in between other elements.

+ +

Now although this image shows all the different types of spacing as equal, the majority of the time these will mostly be padding (inner) and margin (outer). Padding is the inner space between the element and its border; margin is the outer space between two different elements.

+ +

Within the margin the user is unable to press any links or execute any javascript code. It is empty space. If each <link> on your navigation bar has 10 pixels of margin, then there would be 20 pixels in between each <link> that would not be clickable by the user.

+ +

If you have <link>s on your navigation bar with padding set to 20 pixels, however, then there will be 20 pixels on each side of the <link> text where the user is able to click.

+ +

If that part is confusing, try thinking about it in terms of whether background-color would apply.

+ + + + + + + + + + + + + + + + + + + + + +
AttributePaddingMargin
Spacingwithin elementbetween elements
background-color appliesYesNo
+ +

In summary:

+
    +
  • Padding: the space within a tag which is still part of the same tag. background-color applies.
  • +
  • Margin: the space in between two seperate tags. background-color does not apply; it is empty space.
  • +
  • Border: the space in between the two; it surrounds the padding, but is not the margin. It looks nice somtimes, but it has no non-visual function. background-color does not apply.
  • +
+ +

I hope this covers the basics of margin and padding! Happy coding!

+ + +
+ +
+
+ +
+ + diff --git a/_site/2020/01/26/rsa1/index.html b/_site/2020/01/26/rsa1/index.html new file mode 100644 index 0000000..ac87d5f --- /dev/null +++ b/_site/2020/01/26/rsa1/index.html @@ -0,0 +1,102 @@ + + + + + Is Encryption Worth It? | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

Is Encryption Worth It?

+ +
+
+ +

What is the most embarassing thing you have typed into Google search? What is the most personal secret you told a friend in confidence? What is your bank password? What is your business’s secret to stay ahead of the competition?

+ +

Now at first these questions may seem not completely related. There is a point though: You likely sent all of this information over the internet.

+ +

When you send that messege to your friend or business partner, why is it that any person can’t just listen to the signals coming from your phone or laptop and know what you sent to your friend or colleague? The answer: encryption.

+ +

First, some background about internet privacy. You can’t have a conversation about internet encryption and privacy without discussing the man himself:

+ +

Snowden

+ +

Edward Joseph Snowden is an ex-NSA, ex-CIA employee who felt the United State’s 4th Ammendment was being violated by their programs of msas survailence. +Snowden was raised a staunch establishmentarian conservative; his girlfriend Lisndey however, slowly started changing his mind. Snowden became very influenced by the ideology of populism. +His populist thinking is shown very clearly when he explains his reasoning for his disclosure of humongous troves of NSA documents.

+ +
+

“My sole motive is to inform the public as to that which is done in their name and that which is done against them.” +—Edward Snowden

+
+ +

Snowden’s first set of leaks went public in The Gaurdian, The New York Times, and ProPublica in late 2013; +people started to realize that their governments and internet service providers (ISPs) are listening. People understood there might be more sinister motives than “national security” at play.

+ +

Personally, I have seen a lot of non-tech-savy individuals using security-conscious software when I am helping them fix a problem. +In fact, there was one time I saw a collage student from rural Alberta who had a VPN running on her phone. This impressed me!

+ +

Encryption on The Web

+ +

The type of encryption used on the web is called: HyperText Transfer Protocol–Secure (HTTPS). +This kind of encryption stops two things from happening: A) it stops the information you are sending and recieving online from being seen by easvesdroppers and criminals, and B) stops those same third-parties from tampering with the data.

+ +

Without HTTPS it is possible for sombody to listen in and change the data being sent between you and a server.

+ +

Only in recent years has HTTPS become near-universal across the web. It is used even on the simplest sites these days: this one included. After 2013, people became weary of government, criminal, and ISP interference with their web traffic. +This can be backed up by statistics: +The level of encrypted web traffic around the time of the Snowden leaks was around 30 percent. It was mostly used by banks, email providers, government, and journalists. +At the turn of the 2020s however, this has risen to nearly 90 percent among U.S. users of Firefox. +Japan lags slightly behind with 80 percent encrypted traffic.

+ +
+ Use of encrypted web traffic incresing over time. +
+ More at: Let's Encrypt +
+
+ +

This is just the data we know of. You can disable the telemetry settings in Firefox, and it is very likely that hardcore privacy advocates would disable this data collection, so perhaps the amount of encrypted web traffic is slightly higher.

+ +

What about RSA?

+ +

RSA is an encryption method named after the initials of the inventors’ sir names: Ron Rivest, Adi Shamir, and Leonard Adleman. It uses the mathematical “factoring problem” to secure communication. The details of this specific type of encryption will be discussed in an article soon to come.

+ + +
+ +
+
+ +
+ + diff --git a/_site/2020/02/19/rsa2/index.html b/_site/2020/02/19/rsa2/index.html new file mode 100644 index 0000000..9e9b948 --- /dev/null +++ b/_site/2020/02/19/rsa2/index.html @@ -0,0 +1,194 @@ + + + + + How Does Encryption Work, in Theory? | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

How Does Encryption Work, in Theory?

+ +
+
+ +

There are many kinds of encryption used in our everyday communication. Online and offline, over the internet and in person. In this article, I will explain the basics of how encryption should work in theory. I explain in this article why encryption is important, and why you should care about it.

+ +

We will start by looking at in-person, offline encryption.

+ +

Cryptography We Do Everyday

+

We encrypt things all the time without even thinking about it. +If you spend a significant amount of time with the same group of friends, you will tend to develop common codes that may not make sense to others outside the group. +For example: for years, my family called sombody falling from a sitting position “doing a Don”. There is a story of course—We knew a guy named Don who fell from his plastic beach chair in a rather hilarious way; “doing a Don” was born.

+ +

These types of minor dialects in speech are cryptographic in their own way. The truth is though, that we use cryptography much more than that!

+ +
+

“Is cryptography any different than talking? +We say something other than what we mean, and then expect everyone is able to decipher the true meaning behind the words. +Only, I never do…” — Adapted from a scene in The Imitation Game (p. 39-40)

+
+ +

How many times have you hinted, flirted, and innuendoed to try to say “I find you very physically attractive”? +Have you told your friend that always stinks to wear more deodorant? +Have you ever had someone say the words “I’m fine” when you know for certain that they are indeed not okay?

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Words SaidMeaning
What can you do?I don’t want to talk about this anymore.
I don’t want to overstay my welcome.I want to go home now.
I don’t like them and don’t know why.They threaten my ego.
CreepyUnattractive and friendly
+ +

All of these scenarios are perfect examples of lies encryption! If we have the key to these codes, we can start to understand what people really mean. +Hopefully I have convinced you that you use deceit cryptography on a regular basis in your life, so let us consider what a basic encryption method might be:

+ +

Grade-School Encryption

+

Back when I was in middle school I used to pass notes like these:

+ +
+ A message I would have sent in middle school. ROT5: Xfwfm hx hzy +
+ The kind of message I would have sent in middle school. A ROT5 Ceasar cipher. +
+
+

This is a message encrypted using the Caesar cipher. This encryption technique was used by Julius Caesar during the reign of the Roman Empire to “encrypt messages of military significance.”[1] +This is one of the oldest and simplest methods of encryption known to us today.

+ +
+ A diagram of a Ceasar Shift algorithm. A <-> N, B <-> O, et cetera. +
+ A diagram of a ROT13 Ceasar shift algorithm. A <-> N, B <-> O, et cetera. +
+
+ +

You can try this out yourself by moving some letters forward in the alphabet. +An ‘A’ turns into a ‘B’, ‘B’ into ‘C’, ‘C’ into ‘D’, et cetera. +In this case, “Hello!” would become “Ifmmp!” +That is just using a shift of one. You can use a shift of seven, for example, and then you would shift letters like so:

+ +
    +
  • A -> +7 -> H
  • +
  • Q -> +7 -> X
  • +
  • T -> +7 -> A
  • +
+ +

When you reach the end of the alphabet, wrap around to the beginning to find the encrypted letter.

+ +

Example of a Caesar Cipher

+

Let’s setup a little story to illustrate the problems of encryption. We will have three characters:

+ +
    +
  • Alice, young lady with feelings for Bob
  • +
  • Bob, a young lad with an addiction to pancakes
  • +
  • Eve, a wee jealous girl scout who sits between Bob and Alice
  • +
+ +

Alice really likes Bob and wants to tell Bob her feelings, so she writes “I love you, Bob! Please eat healthier!” on a sticky note. +She passes it to Eve, so Eve can pass it to Alice’s love interest. +However, in an unfortunate turn of events Eve reads the note herself, and decides not to give it to Bob.

+ +

Oh the horror! Alice is without young love! How could she remedy this so that Bob can read her message, but evil Eve can not? +Let’s use the Caesar cipher to fix this problem.

+ +

Let us assume that Alice and Bob already have a shared key, 7 for example. To encrypt this message, she should shift her letters seven letters forward in the alphabet—just like the example above.

+ +
+A longer Ceasar cipher encrypted message: ROT2: Wpeng Vgf ku dqqogt ogog] +
+ A longer Ceasar cipher encrypted message using ROT2. +
+
+ +

Now Alice’s message reads “P svcl fvb, Ivi! Wslhzl lha olhsaoply!”

+ +

Now, when Alice sends her Romeo a little note, all he has to do is decrypt the text by shifting the letters down by 7. +Here is a site which can do longer pieces of text for you instead of doing it manually.

+ +

Problems

+ +

Before the two love-birds start smooching on the branch of a big pine tree in the schoolyard, perhaps we should consider some problems with the Ceasar cipher.

+ +

It is Very Easy to Break

+ +

Even Eve with her measly grade 4 math skills could easily start going through this message with pen and paper and figure out any combination in a couple hours at maximum. +Imagine how easy this is for a computer? +This could be broken in a few microseconds even on an older processor like the Intel Core 2 Duo.

+ +

No Secure Way of Sharing Keys

+ +

We assumed in our previous example that Bob and Alice already have a shared key (seven) to encrypt and decrypt all of their messages. +If Bob and Alice did not have a previous friendship and time to share secrets of this sort, there is no way to share their key with eachother without Eve also knowing. +This would defeat the entire purpose of obscuring the message in the first place.

+ +

Universal Vulnerability of Messages

+ +

Every message sent between the two parties uses the same code to encrypt and decrypt. If someone finds out the code once, all previous communications are comprimised.

+ +

Better Encryption Methods

+ +

To combat the issues with easily breakable, shared-key cryptography, we can turn to the beautiful beast that is Asymetric Cryptography. +I will discuss this more in another article, but for the technically inclined:

+ +
    +
  1. RSA/EC provides very large cryptographic keys. It would be impossible for a human to encrypt or decrypt a message manually.
  2. +
  3. Asymetric cryptography provides four keys, instead of just one; stopping evesdroppers from listening in on your secret conversations—even if you do not have the chance to exchange keys in advance.
  4. +
+ + +
+ +
+
+ +
+ + diff --git a/_site/2020/04/02/rsa3/index.html b/_site/2020/04/02/rsa3/index.html new file mode 100644 index 0000000..c2a341e --- /dev/null +++ b/_site/2020/04/02/rsa3/index.html @@ -0,0 +1,193 @@ + + + + + How Asymetric Encryption Works | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

How Asymetric Encryption Works

+ +
+
+ +

In a few previous articles I have explained why encryption may be important to you and how the theory behind encryption works. I did not yet explain the system of asymetric cryptography, however. That is what this article is for.

+ +

Previously, we talked about how symetric encryption works. This is by having a shared key that both parties use to simultaniously encrypt, and decrypt the data. (See Ceasar Cipher for example).

+ +

Public-key, or Asymetric Encryption

+ +

Asymetric encryption is based on the idea of having multiple keys instead of only one shared key. +For example: instead of encrypting with one key, and decrypting with that same key (like our ROT ciphers we talked about previously), we can use one key to encrypt the information, and a different key to decrypt the information.

+ +
+ Alice sending her message to Bob using Bob's public key. Bob decrypts the message with his private key. +
+ Alice sending her message to Bob using Bob's public key. Bob decrypts the message with his private key. +
+
+ +

In the picture above, see how Alice uses Bob’s public key to encrypt some data, +then sends it to Bob for him to decrypt with his private key? +That is the essense of public-key encryption.

+ +

The great thing about public-key encryption is that your public key is public! There is no need to be afraid of sending this everywhere! +You can attach it at the end of all your emails, the end of your forum posts, a link to it on your low-power webserver (wink). +There are even things called keyservers that will save your public key on them for retrival in case somebody wants to verify your public key.

+ +

Anything encrypted with your public key can only be decrypted with your private key. +Provided you never, NEVER share your private key with anyone ever, we can assume that all messages sent to you encrypted with your public key will never be read by anyone else.

+ +

Asymetric encryption, however, often contains four keys instead of two. Why is this?

+ +

Verification of Author

+ +

One interesting thing about keys pairs is that not only can the private key decrypt anything the public key encrypts, +but the public key can decrypt anything the private key encrypts.

+ +

Now why would one want to encrypt a message that can be decrypted by anyone?

+ +
+ Alice sending a message to bob which is 'signed' with her private key. This allows Bob to know only Alice could have sent it! +
+ Alice sending a message to bob which is 'signed' with her private key. This allows Bob to know only Alice could have sent it! +
+
+ Note: Although the picture shows otherwise, the text is not sent in the plain. It is encrypted with Alice's private key. +
+
+ +

This is how you can verify that the person who says they wrote the message really did indeed write the message! +If their private key was never shared with anyone else, then the message must have come from them!

+ +

For maximum security, these methods are often layered. +First, signing with the sender’s private key, +ensuring only they could have sent it— +then encrypted with the recipient’s pulbic key, +making sure only the reciever can read it.

+ +

Note that both sides must first have eachother’s public keys to do this. +This is easy if they communicate often, but when first contacting somebody, +people will generally send their encrypted message along with the their own pulbic key attached in a seperate file.

+ +

What This Means

+ +

Notice neither Alice nor Bob had to share any comprimsing information over the network? +This is why public-key encryption is so powerful!

+ +

Alice and Bob can both safely send their public keys in the open. +They can even send them over the insecure HTTP, or FTP protocols.

+ +

Whilst not sending any encryption-breaking messages, +Alice and Bob now have a way to communicate securely. +If you trust nothing and no one, this is your perfered method of security.

+ +

Check out this Computerphile video if you want the simplified explaination.

+ +

The Algorithms

+ +

The two biggest “implementations” of public-key cryptography vary only in the mathamatical equations used to generate the numbers, +and how the numbers are “trapdoored” to decrypt if you have the correct key.

+ +

I will discuss the differences in approach here. +If you want to skip to the next article where I show you how to encrypt your own documents using RSA, see this link.

+ +

RSA

+ +

The mathamatic center of the RSA system was developed over the course of a year or so. +Three men were involved. Ron Rivest, Adi Shamir, and Leonard Aldeman. +They worked as a kind of “team”: Each idea by Rivest and Shamir were critisized by the mathamatician on their team: Mr. Aldeman.

+ +

One night, after consuming +“liberal quantities of Manischewitz wine” +Rivest had trouble sleeping. +After taking long gazes into the abyss of his math textbook, he came up with an idea which would change cryptography forever. +By the next morning, an academic mathamatical paper was nearly finished. +He named it after himself and the two others that had been helping him along this whole time. Rivest, Shamir, Aldeman.

+ +

Key sizes of RSA range from 1024-bit to 4096-bit. +1024-bit keys are considered somewhat insecure. +However, +it should be noted that every bit doubles the complexity of the key, +so 2048 is 2^1024 times more complex than 1024.

+ +

Eliptic-Curve (EC)

+ +

Eliptic-Curve (EC) is a family of algorithms that use the Eliptic curve mathamatical structure to generate the numbers for the keys. +EC can effectivly provide the security of an RSA key one order of magnitude larger than an RSA key.

+ +
+ A picture of an eliptic curve. +
+ An eliptic curve structure. +
+
+ +

It’s fast; it’s secure! Perfect right?

+ +

Of course not!

+ +

One problem is that due to the smaller key size, +it can more easily be broken by brute-force. +This is why EC is mostly used for temporary communication (like HTTPS), not permenant communication (like having an encrypted email conversation with a journalist).

+ +

The other problem is that a certain EC algrorithm called P-256 is suspected to be introduced with malice to National Institute of Standards and Technology (NIST) +by the NSA. +Supposedly, the NSA is able to crack anything encrypted with this algorithm. +I will let the experts argure about that.

+ +

Other well-known EC algorithms that are more-or-less trusted as secure do exist though. +The premeire one being Curve25519. +The reference implementation of this algrorithm is also public-domain, +so it is easy for devlopers to work into their own applications without worrying about copywrite.

+ +

Conslusion

+ +

In this article we went over some basic points:

+ +
    +
  1. Public-key encryption enables secure communication over insecure networks.
  2. +
  3. RSA is considered the standard for extra-seure communication.
  4. +
  5. EC is a newer, faster, more transient encryption method.
  6. +
+ +

To learn how to use RSA keys to encrypt your own communications, check out this other aritcle I wrote.

+ + +
+ +
+
+ +
+ + diff --git a/_site/2020/04/06/rsa4/index.html b/_site/2020/04/06/rsa4/index.html new file mode 100644 index 0000000..3cd3113 --- /dev/null +++ b/_site/2020/04/06/rsa4/index.html @@ -0,0 +1,392 @@ + + + + + How To Encrypt Your Own Documents Using gpg | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

How To Encrypt Your Own Documents Using gpg

+ +
+
+ +

If you have ever wanted to garuntee the utmost security of your emails and documents, then this is the guide for you! +It should be noted that in some circles the tools used are more common than in others. +These are the everyday tools of many privacy advocates and computer nerds.

+ +

If you have never used Linux however, then the method of doing this will be rather unfamiliar. +This tutorial will be done on an Arch Linux machine, +but it should be the same on Ubuntu, Fedora, CentOS, Debian, +OpenBSD, FreeBSD, MacOSX, etc. +The only operating system that does not include these tools by default (or easily accessible) is Windows.

+ +

This tutorial makes heavy use of the terminal. +You have been warned.

+ +

Let us…begin!

+ +

Glossary

+ +
    +
  • ASCII armour — A way to encode OpenPGP documents so they are readable by humans. These files end in .asc
  • +
  • (Open)PGP — An open standard for encoding pulbic keys and encrypted documents.
  • +
  • GPG — GNUPrivacyGaurd is an implementation of OpenPGP. It is installed by default on most Linux distrobutions.
  • +
+ +

Step 0: Setup

+ +

We will be using the utility gpg for this tutorial.

+ +

The other thing to note: The character ‘$’ (dollar sign) is usually not typed when shown in a command. +It simply indicates that you do not need administrative privilages to run these commands.

+ +

Test to see if you get this output in your terminal.

+ +
+$ gpg --version
+
+gpg (GnuPG) 2.2.20
+libgcrypt 1.8.5
+Copyright (C) 2020 Free Software Foundation, Inc.
+License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
+
+...
+
+ +

If this is not successful look into how to install these tools on your system.

+ +

Step 1: Get/Create A Public Key!

+ +

Get Somebody Else’s

+

Step one is having somebody to send your encrypted message to. Maybe this is a friend, a journalist, or a whistleblower.

+ +

To encrypt a document with somebody’s public key, you need to first obtain it. +My public key is available at this link, and you can use it to send me encrypted stuff.

+ +

If you are on a linux terminal, you can use the curl or wget command to download it.

+ +

wget:

+
+$ wget https://tait.tech/public-key.asc
+
+ +

Curl:

+
+$ curl https://tait.tech/public-key.asc -o public-key.asc
+
+ +

Make Your Own (optional)

+ +

The following section is quite long, +so if you don’t want to create your own keypair, +then feel free to skip to Step #2.

+ +

If you want to encrypt your own documents, +or you want others to be able to send you encrypted messages, +then you can create your own public/private key pair. +You can use these to encrypt your documents, +and you can send our public key to others so that they can securely communicate with yourself.

+ +

Run the following command in your terminal, and follow the steps I outline to get you started.

+ +
+$ gpg --full-gen-key
+
+ +

This will produce the following dialog:

+ +
+gpg (GnuPG) 2.2.20; Copyright (C) 2020 Free Software Foundation, Inc.
+This is free software: you are free to change and redistribute it.
+There is NO WARRANTY, to the extent permitted by law.
+
+Please select what kind of key you want:
+  (1) RSA and RSA (default)
+  (2) DSA and Elgamal
+  (3) DSA (sign only)
+  (4) RSA (sign only)
+  (14) Existing key from card
+Your selection? 
+
+ +

Select the option 1. You want two keys, both RSA.

+ +

Next we will select the key size:

+
+RSA keys may be between 1024 and 4096 bits long.
+What keysize do you want? (2048) 
+
+ +

Type the number 2048.

+ +

Next it will ask you how long you want the key to be valid.

+ +
+Requested keysize is 2048 bits
+Please specify how long the key should be valid.
+          0 = key does not expire
+       <n>  = key expires in n days
+       <n>w = key expires in n weeks
+       <n>m = key expires in n months
+       <n>y = key expires in n years
+Key is valid for? (0)
+
+ +

Type the number 1. This will enable you time to test it, +but it will make the key expire within 24 hours so that if you accidentally +share your private key, or delete your VM and no longer have access to it, you will be fine.

+ +

It will ask your if you are sure about the expiry date.

+ +
+Key expires at Tue Apr  7 02:24:23 2020 UTC
+Is this correct? (y/N) 
+
+ +

Type y to confirm your choice.

+ +

Now gpg is going to ask you to create a user id to indetify this key. +Use some test data for now. +User input is in bold, feel free to follow along or to put your own test data in.

+ +

Once you are more comfortable with the tools, +then you can create a public/private keypair that you will keep for some time.

+ +
+GnuPG needs to construct a user ID to identify your key.
+
+Real name: Mr. Tester
+Email address: test@test.org
+Comment: for testing only
+You selected this USER-ID:
+    "Mr. Tester (for testing only) <test@test.org>"
+
+Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
+
+ +

It will then ask you for a password. +If you are simply using this for test purposes, +then you can feel free to set it to something like “test”. +When create a long-term use pulbic key make sure to make the password very secure.

+ +

During the process of creating your key, gpg may warn you with this message:

+ +
+We need to generate a lot of random bytes. It is a good idea to perform
+some other action (type on the keyboard, move the mouse, utilize the
+disks) during the prime generation; this gives the random number
+generator a better chance to gain enough entropy.
+
+ +

If this happens, feel free to smash your keyboard (lightly), +watch a YouTube video on the machine, +browse the web with w3m, +etc. until the key is generated.

+ +

You will know it is done when you see this message (or something similar):

+ +
+gpg: key EACCC490291EA7CE marked as ultimately trusted
+gpg: revocation certificate stored as '/home/tait/.config/gnupg/openpgp-revocs.d/FFA7D7525C6546983F1152D8EACCC490291EA7CE.rev'
+public and secret key created and signed.
+
+pub   rsa2048 2020-04-06 [SC] [expires: 2020-04-07]
+      FFA7D7525C6546983F1152D8EACCC490291EA7CE
+      uid                      Mr. Tester (for testing only) <test@test.org>
+      sub   rsa2048 2020-04-06 [E] [expires: 2020-04-07]
+
+ +

Tada! You have your own public/private keypair!

+ +

Sharing a keypair that will expire soon is not a good idea, +however, if you are ready, then you can use this command to generate a public key file to share with others.

+ +

Feel free to substitute “Mr. Tester” for any other identifying part of your key. +Remember that to use the email, you must enclose it in < and >.

+ +
+$ gpg --export --armour "Mr. Tester" > public-key.asc
+
+ +

To use the email as the identifier:

+
+$ gpg --export --armour "<test@test.org>" > public-key.asc
+
+ +

Step 2: Import Public Key

+ +

This list of keys that gpg keeps on tap so to speak, is called our “keyring”. +Your will need to import a new public key to encrypt files with gpg.

+ +

If you already created your own public key, then this step is not necessary unless you want to also encrypt something for me :)

+ +
+ A keyring holding eight allen keys. +
+ A keyring holding eight allen keys. +
+
+ +

To import a public key to use for encrypting files, use the --import option of gpg. Like so:

+ +
+$ gpg --import public-key.asc
+gpg: key 64FB4E386953BEAD: public key "Tait Hoyem <tait.hoyem@protonmail.com>" imported
+gpg: Total number processed: 1
+gpg:               imported: 1
+
+ +

Now that we have imported a public key, we can make a message to send!

+ +

Step 3: Have A Message To Encrypt

+ +

You can make a new file which holds some important, secret data. +Feel free to use a graphical editor if you have one, if not, nano works alright too.

+ +
+  Rules Of A Good Life:
+
+  1. Wash your hands!
+  2. Work hard!
+  3. Be firm.
+  5. Have good friends!
+
+ +

Save this file as something like test-pgp.txt, and we’ll use that name later.

+ +

Step 4: Encrypt A Message

+ +

Now that we have a message to send and person to send to, +all we have to do is encrypt this message and it’ll be on its merry way! +To do so, we must specify two new options to gpg.

+ +

The first is --recipient. +This tells gpg to encrypt using a certin public key that we have in our keyring. +You can use the person’s name, email address, or the key’s uid.

+ +

The second is --encrypt.

+ +

You will also specify the --armour option to use ASCII armoured files. Put this option after --encrypt, and put the file name after --armour. See below.

+ +

You can either use your own public key name to encrypt a document (allowng only you to decrypt it), +or you can use my public key that we imported earlier (allowing only me to decrypt it). +Either way works fine.

+ +

This is the big one!

+ +
+$ gpg --recipient "Tait Hoyem" --encrypt --armour test-gpg.txt
+
+ +

“But there is no output!” you might say! +Yes, that is because our new (encrypted) file has already been saved. +Let’s look at it with cat.

+ +
+$ cat test-gpg.txt.asc
+-----BEGIN PGP MESSAGE-----
+
+hQIMA2mJuYb8vkIlAQ/9FDmXJgW2vI7p9sznKvHhQk7uTZvoWC3hCeqHoO3BSElP
+XR1BNAkJ+bykB30M+9u+XDyRtTwazjvNPmYfQnIh0Q+BQZigDWbEd1R47jbzm7Tu
+0eZKKapgEidfecULtaECX1sR3qPt1m9oZjyUR1rzNd8tezZlCu2pjdNZrkta2Bdm
+Hh1xDS43Bw7PMQqraJsHwqr0M1GLDbMzPes2ZU5y4jEmXZ0PZdJ7kgjR8dvhLBfi
+MU+4kYnnemQEztXBOjKidhyOntKiLjenvD00tVHrOuQoWuWCHGiqR24qSwVjeb9G
+079gqH1VWi3fk2cwFA9f3TLvJqUwatyE0Hcba0U1d2Voz/C9JEQjT6FHuaCqQL6b
+p7B7m2DwpywFGJpAn6ksrEYqHaLVWiEGmdMmHYuHxMw8+cqoSwbYymCZTwMBAuJe
+Pr1VO9uNo+Vj5r8IX7ACcSsrjf0XkVzfX6ySsPbyOlGXnwzWSOM3Dk2Z9MqDORbj
+0/7vJTnDctPuc91Rlp3YnJlZKWMcNfPMKMtvpljd2XuVwub+C4vGWXa9XLbRXmJo
+cnEFT6SB11AKjytE2Urt62CCrYjJPBneabxbCztnBs+vQSx7Fj0LK6v4Euik/Xm/
+9aKmZZW8306c9Zwgpp9glWjLMCDNxJRGdKRjZsnkt9hOEYsP1irTegystK6u4eHS
+mwHX931ENOJsnPfQZCZ9b41Q9doZQ/N/WHstQO8MtA3HIN1sW3wYkGzOLKj4gJfm
+bqR/TzQmXyLT1xZa+/yTscaV0P4OlI4vcii/k4DgeSeQVWp9o9DbZFxSCsdYVvPu
+jaDMzZnIKoax1GFz/coUAHFQub2rLzaQ5DDbvrkX++UrAjuUtRcSFH0TKhahZmCF
+nv117moLfK22Mst/
+=bw8T
+-----END PGP MESSAGE-----
+
+ +

Step 5: Decryption (optional)

+ +

If you created your own public/private keypair in step 1, +and you encryped using --recipient "Your Test Name", +then you can decrypt your document as well!

+ +

You will need to specify --decrypt, and that’s all folks!

+ +
+$ gpg --decrypt test-gpg.txt.asc
+
+ +

A password dialog will then come up asking for your previously created password. +As long as you remember your password from before and enter it correctly: voila!

+ +
+gpg: encrypted with 4096-bit RSA key, ID 6989B986FCBE4225, created 2020-01-02
+      "Tait Hoyem <tait.hoyem@protonmail.com>"
+Rules Of A Good Life:
+
+1. Wash your hands!
+2. Work hard!
+3. Be firm.
+5. Have good friends!
+
+ +

Step 6: Finale!

+ +

Ladies and gentleman, you have done it! +You have encrypted our very own document. +(And maybe even decrypted it yourself too :)

+ +

If you encrypted using my public key, +feel free to send it to my email. +I am happy to verify if it worked.

+ +

For more information on this subject, check out gnugp.org’s guide on using GPG. +They are the ones that make these tools available, +and the GNU Project has been instrumental in creating the open-source world as it exists today. +Give ‘em some love, eh!

+ +

Thank you so much for sticking through this whole thing! +Let me know if there is anything that doesn’t make sense. +I am happy to improve this guide as time goes on if that is necessary.

+ +

Happy hacking :)

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/04/12/nas1/index.html b/_site/2020/04/12/nas1/index.html new file mode 100644 index 0000000..086804c --- /dev/null +++ b/_site/2020/04/12/nas1/index.html @@ -0,0 +1,102 @@ + + + + + NAS Part 1: Theorize | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

NAS Part 1: Theorize

+ +
+
+ +

New Project, phase one: +Theorize.

+ +

I want to build a NAS server to store a bunch of data on. Current problem is lack of a computer to accept multiple SATA connections.

+ +

Problem 1: SATA connectors

+ +

This can be solved by an HBE card. Although they tend to be quite expensive (250+). +One decent model that isn’t that much is the LSI 9211-8I. +This is ideal for future expansion.

+ +

A cheaper option is a PCIe multi-SATA connector like this.

+ +

Either work, but one is cheaper and the other is more expandable. +The 9211-8I uses two SAS ports, which can be expanded indefinetely. SAS supports splitting. +SATA can be connected in a 4:1 ratio to SAS connectors with some cheap cables.

+ +

Problem 2: Drives

+

I do not have enough drives to make this work right now. +For the setup I want it would require 5 or 6 drives. +I will get 4-5 drives worth of space as one drive worth of space is dedicated to “parity”, making you able to:

+ +
    +
  1. Verify data integrity. If anything goes wrong with a write, it will be fixed automatically.
  2. +
  3. If one drive dies, the system can stay online with no problem. Two drives and I’m eff-you-see-kay-ed-dee.
  4. +
+ +

My other option is to use two drives worth of space for partiy. +This would only have me 3-4 drives of space, but +this system can withstand the failure of two drives.

+ +

Problem 3: Computer System

+ +

I currently have 5 computers.

+ +
    +
  1. Celery Stick. An old grey HP laptop with a Braille stickered keyboard. Does not work right now; bad thermal paste job.
  2. +
  3. A Dell laptop lent to me by my school during my studies.
  4. +
  5. Houston. A 21-inch 2011 iMac for which the screen does not work under Linux (excep with the nomodeset kernel option enabled).
  6. +
  7. An Old Toshiba laptop (circa 2010) that I got for $50 to test with OpenBSD (works….sometimes).
  8. +
  9. Main Rig. My main laptop is an ASUS-705 TUF gaming laptop.
  10. +
+ +

None of these have PCIe expansion slots with a case that can handle the new drives.

+ +

I think it’s reasonable to say that for hard-drives and low-end tower PCs, +I will likely have luck on a place like Kijiji (Canadian Craigslist).

+ +

The search continues :)

+ +

I’m in for a fun ride…. and a few monnies.

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/04/21/rfi/index.html b/_site/2020/04/21/rfi/index.html new file mode 100644 index 0000000..27cff95 --- /dev/null +++ b/_site/2020/04/21/rfi/index.html @@ -0,0 +1,68 @@ + + + + + rfi: A Simple Linux utility to get a random file from a directory | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

rfi: A Simple Linux utility to get a random file from a directory

+ +
+
+ +

I made a little video about this script I wrote:

+ +
+$ rfi
+
+ +

This program gets a random file from your current directory +if you do not specify one; +it gets a random file from the specified directory if you give it one like so:

+ +
+# rfi /etc/wireguard
+
+ +

Which is very useful if you want to start a random VPN configuration :)

+ +

The code, comments, etc. are on the Github.

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/04/25/xss/index.html b/_site/2020/04/25/xss/index.html new file mode 100644 index 0000000..c756408 --- /dev/null +++ b/_site/2020/04/25/xss/index.html @@ -0,0 +1,147 @@ + + + + + What is XSS? | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

What is XSS?

+ +
+
+ +

I found a cross-site scripting (XSS) attack +in a well-known quiz hosting website. +I disclosed the vulnerability to them years ago, so I thought +now might be a good time to write about it.

+ +

In this first article I will explain what XSS is.

+ +

In the next article I will explain how I found this attack.

+ +

What is cross-site scripting (XSS)

+ +

Cross-site scripting, XSS for short, +is a technique to execute arbitrary Javascript code on a user visiting a website +by linking to Javascript code stored on another server.

+ +

So for example:

+ +

I have a file on my website called hacked.js. +If I was able to run this javascript file on anybody visiting a certain website that is not mine, this would be called cross-site scripting.

+ +

Click the above hacked.js link to view the code I use to “hack” this website. +It’s safe, I promise ;)

+ +

Now, how can we get this code to execute when a user visits this site? +To explain, I will start with some of the underlying technologies.

+ +

Escape Characters!

+ +

No, this is not a Sherlock Holmes novel!

+ +

If we suppose that a website is built with sequences like these (called “tags”): +<body>, <p> (for paragraph), <link> and <b> for bold, +then why can you see the left and right angle bracket characters? +Don’t they mean something? Shouldn’t they be telling the browser: +“Hey! Make me bold!”? +Why doesn’t everything after me typing <b> turn bold?

+ +

The answer is:

+ +

There are special characters in HTML to type a visible left (<) +and visible right angle bracket (>) in a website. +If I use the left and right brackets on my keyboard however, +things will indeed show up bold.

+ +

This is the code for the sentence I wrote above:

+
+There are special characters in HTML to type a visible left (&lt;)
+and visible right angle bracket (&gt;) in a website.
+If I use the left and right brackets on my keyboard however,
+things will indeed <b>show up bold</b>.
+
+ +

Notice how all visible left angle brackets use an &lt; to show them?

+ +

These are called escape characters. +They tell a system, in this case your web browser: +“Hello! Please show me off! I don’t want to be hidden.”

+ +

Sanitization

+ +

Most of the time XSS attacks are done using poorly sanitized HTML <input> elements.

+ +

Sanitization is when a program (usually on the server side), +will remove characters like < and replace them with the aforementioned “escape characters”. +Internally this would be something like &lt;, +but they would show up to a user as <.

+ +

When inputs are not properly sanitized and the input is shown to the user in another part of the website, +then a malicous user can type in HTML that will run whenever anybody tries to look at what they typed. +For example: a name for a quiz website (input) and the leaderboard for said quiz (display).

+ +

HTML, by itself is not very dangerous. +The worst thing you could do is probably put a link on your name, +and then point it to a porn site. +Make your name bold, italic. Maybe make the background a funny color. +Although this may annoy your victim it is not dangerous security wise.

+ +

There is one tag however, that is scary…

+ +

<script>

+ +

The <script> tag allows you to write code that can:

+ +
    +
  1. Change the page contents.
  2. +
  3. Redirect the user to a new page automatically.
  4. +
  5. Get a user’s location.
  6. +
  7. Open a user’s microphone/webcam.
  8. +
  9. With the src attribute you can also load a script from another site. (This is XSS)
  10. +
+ +

Those last two will ask for permission from the user (if their browser isn’t insanely insecure).

+ +

In my next article I’ll talk about a website I found which is vulnerable to this attack. +And, show you how you can run your own XSS attack.

+ + +
+ +
+
+ +
+ + diff --git a/_site/2020/05/01/nginx-socket-io-projects/index.html b/_site/2020/05/01/nginx-socket-io-projects/index.html new file mode 100644 index 0000000..97c774a --- /dev/null +++ b/_site/2020/05/01/nginx-socket-io-projects/index.html @@ -0,0 +1,109 @@ + + + + + How to use NGINX as a reverse-proxy server for a Node.js application using socket.io | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

How to use NGINX as a reverse-proxy server for a Node.js application using socket.io

+ +
+
+ +

Despite the long name of the article, I have a feeling this may apply to more people than I might think. +If you have a Node.js application which needs socket.io connections that you want to pass throgh nginx’s reverse_proxy directive then this is the article for you!

+ +

You must seperate the socket.io sockets and the static resources.

+ +
    +
  • The socket connections can be routed through the default $host/socket.io if you want to ease modifications to the source code.
  • +
  • The connections to your main npm Node.js application can be routed through the relevant directory.
  • +
+ +

Here is the relevant part of my projects.tait.tech.conf file:

+ +
+location /socket.io {
+  proxy_pass http://localhost:8080/socket.io/;
+  proxy_set_header Upgrade $http_upgrade;
+  proxy_set_header Connection "upgrade";
+  proxy_http_version 1.1;
+  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+  proxy_set_header Host $host;
+}
+
+location /ttrpg {
+  proxy_pass http://localhost:8080/;
+  proxy_set_header Host $host;
+  proxy_set_header X-Real-IP $remote_addr;
+  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+}
+
+ +

Explaination:

+ +

For this application, +I needed the /ttrpg directory to connect to my main Node.js instance. This was going to be the root of a ttrpg project. +It was to have static files served form my Node.js application.

+ +

I also needed /socket.io to conenct to my running npm instance. +When I tried to route all the traffic through the /trrpg location directive +I had no luck whatsoever; +$host/ttrpg/socket.io/* calls always failed with a 404.

+ +

Having two seperate blocks forwarding in different ways seems to fix this. +I am not knowledgable enough to understand how.

+ +

For now, the project is alive!!!

+ +

Happy hacking!

+ +

P.S. I forgot to mention I also symbolically linked the socket.io.js file (that node is supposed to serve automatically) to the static client dir. +For some reson the node instance would not serve this file without that.

+ +
+$ pwd
+/home/user/ttrpg.co/client
+$ ln -s ../server/node_modules/socket.io-client/dist/socket.io.js .
+
+ +

Happy hacking 2.0!

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/05/19/clue-announcement/index.html b/_site/2020/05/19/clue-announcement/index.html new file mode 100644 index 0000000..2e69ef7 --- /dev/null +++ b/_site/2020/05/19/clue-announcement/index.html @@ -0,0 +1,66 @@ + + + + + New Game: Clue (coming soon) | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

New Game: Clue (coming soon)

+ +
+
+ +

Ooo! Exciting! +Today I want to announce a new project I’ll be working on which should be live within the month of May: +Clue.

+ +

The original board game, implemented in an accessible format via the web.

+ +

It uses a Node.js backend and standard Javascript/HTML frontend. +Nothing fancy.

+ +

All the code will be hosted here: https://github.com/TTWNO/clue

+ +

It will be licensed under the BSD-3 license, meaning it can be used for any reason—even commercially and without source-code disclosure—without prior authorization, but it must acknowledge that I helped build the end product.

+ +

Once the project is live, it will be located at: Lame Games (currently a dead link).

+ + +
+ +
+
+ +
+ + diff --git a/_site/2020/06/04/site-update/index.html b/_site/2020/06/04/site-update/index.html new file mode 100644 index 0000000..f78799a --- /dev/null +++ b/_site/2020/06/04/site-update/index.html @@ -0,0 +1,57 @@ + + + + + Site Update | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

Site Update

+ +
+
+ +

I updated the site with some easier to identify information about me and my projects :)

+ +

Also, Clue has been delayed due to my partner in crime on the project wokring too many hours.

+ +

I also posted a new project called Caesar Cipher in C. It will be an intermediate example of how to use build systems like make.

+ + +
+ +
+
+ +
+ + diff --git a/_site/2020/06/25/tmux-minecraft/index.html b/_site/2020/06/25/tmux-minecraft/index.html new file mode 100644 index 0000000..e4f15a6 --- /dev/null +++ b/_site/2020/06/25/tmux-minecraft/index.html @@ -0,0 +1,182 @@ + + + + + How to use tmux to send and receive things from your Minecraft server | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

How to use tmux to send and receive things from your Minecraft server

+ +
+
+ +

So recently I had problem. +I run a Minecraft server on a big Linux computer I have running in my room. +Now, as a system administrator it is very helpful to be able to run some simple commands without needing to login with my key, password, TFA, etc. +It is, frankly, a lot of work. +Especially when I really just want to be playing games but I just need to check something quickly.

+ +

So for simple things like finding out of the network, CPU, memory or disk usage is my bottleneck, I wrote this really nifty script to connect the world of Minecraft and the Linux shell.

+ +

My completed solution for what I needed can be found at https://github.com/TTWNO/termcraft.

+ +

If you want some of the implementation details, stick around.

+ +

Solution

+ +

So to solve this interesting problem, I decided to use tmux. +tmux is a tterminal multiplexer. +This allows you to run a terminal session, then detach fromc it while it still runs in the background.

+ +

This is very valuable when running command line applications that need to have an active console connection, like a Minecraft server.

+ +

So first I looked at the tmux command send-keys.

+ +

send-keys

+ +

send-keys allows you to send text, and key presses to a tmux session. +Now assuming this tmux session is attached to a Minecraft server, +there is no reason you could not run a command like this:

+ +
+$ tmux send-keys "tell @a This is a Test" Enter
+
+ +

This will send the text “tell @a This is a Test” to the Minecraft server. +Then, it will hit the newline character, this will execute the command.

+ +

So now we can send information to the server and have it tell the users something.

+ +

But how do we get information about who is typing what in the Minecraft chat?

+ +

tmux’s capture-pane is painful

+ +

So in the manual page for tmux I can see a section recorded below for options I can give to the capture-pane subcommand.

+ +
+  -S and -E specify the starting and ending line numbers,
+  zero is the first line of the visible pane and negative
+  numbers are lines in the history.  ‘-’ to -S is the start
+  of the history and to -E the end of the visible pane.  The
+  default is to capture only the visible contents of the pane.
+
+ +

What it seems to be saying is I can start at line -S n and end at line -E n. +Negative numbers start from the bottom, so in theory I can do the following: tmux capture-pane -S -1 should capture only the last line, because I’m starting from the last line. Right?

+ +

No. It just doesn’t work. Negative numbers do not work with the tmux capture-pane subcommand.

+ +

So I did some simple UNIX piping, like so, to get just the last thing in the chat.

+ +
+$ tmux capture-pane -p -t steve | tail -n1
+[SERVER] [ExtraDebuggingInfoHere]: <TaterTheTot> MY_MESSAGE
+
+

TaterTheTot is my Minecraft username :)

+ +

-p prints the result to the terminal/stdout.

+ +

steve is the name of the tmux session I’m trying to pull form.

+ +

So that’s done! Beauty!

+ +

Now that we have that, how can we extract the username and the message from the latest line?

+ +

grep

+ +

grep is a command to find patterns of text. +grep has an option to only show a matching pattern of text. +This option is -o.

+ +

Let’s see how we can use this in conjunction with our latest line of server output to get our results.

+ +
+$ echo "[DEBUG] [SERVER] blah blah: <TaterTheTot> MY_MESAGE" | grep -o "<.*>"
+<TaterTheTot>
+
+ +

Now, that’s my name with the < and > attached. Not bad! +We can use the sed command to clean it up a bit.

+ +

The syntax is like so: select/somepattern/replacewith/global

+ +

So the following command is: s/[<>]//g

+ +

Select any characters that are either < or >. +Replace with nothing. +Do so globally (as in, don’t stop after you replace only one character).

+ +

Take two!

+ +
+$ echo "[DEBUG] [SERVER] blah blah: <TaterTheTot> MY_MESAGE" | grep -o "<.*>" | sed 's/[<>]//g'
+TaterTheTot
+
+ +

Beautiful!

+ +

Now what about that pesky message?

+ +

more grep; more sed

+ +

Simple: capture everything after the >. Leaving the user’s message entirely in tact.

+ +
+$ echo "[DEBUG] [SERVER] blah blah: <TaterTheTot> MY_MESAGE" | grep -o ">.*$" | sed 's/> //'
+MY_MESSAGE
+
+ +

So now we have a way to get the username of someone typing in the Minecraft server chat. +We have a way to find out what they said. +And, we have a way to respond.

+ +

You can imagine how these might go together for your own use case.

+ +

Conclusion

+ +

This shows some pretty fun stuff you can do with a few simple Linux commands and a Minecraft server.

+ +

I hope you learned something and found my explanations not horrific haha!

+ +

Remember to checkout the git repository to see what I did with it: https://github.com/TTWNO/termcraft.

+ +

Happy hacking!

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/07/12/independence/index.html b/_site/2020/07/12/independence/index.html new file mode 100644 index 0000000..e29b881 --- /dev/null +++ b/_site/2020/07/12/independence/index.html @@ -0,0 +1,90 @@ + + + + + Independence | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

Independence

+ +
+
+ +
+

“When given a choice between independence and dependence, always choose independence; you will never regret that choice!”—Luke Smith

+
+ +

Whatever you may believe about the YouTube personality Luke Smith, +the quote above summarizes a core principle of mine. +Much like many people have religious principles, I have Independence.

+ +

My choice to use Linux as my primary operating system, +host my own website, +own my own domain name—all of these are directly related to this core principle of independence.

+ +

I never want a man, or a company to have too much power over my life. +Just like I would not trust just any person to be able to read my emails, +know where I live, where I am going, who are my friends, what do I believe; in the same way, I do not trust a company with that same information.

+ +
+

“If you want to find out what a man is to the bottom, give him power. Any man can stand adversity — only a great man can stand prosperity.”—Robert Ingersoll

+
+ +

Take control of your own digital life:

+ +
    +
  1. Own your own domain.
  2. +
  3. Hookup an email and a website to that.
  4. +
+ +

That’s it!

+ +

Without this, any of your internet privileges can be revoked at any time by Google, Facebook, YouTube, Twitter, or even an angry Twitter Mob. Maybe because they hate your skin colour, maybe they hate your religious/political views, or maybe you got caught on a technicality.

+ +

If you own your own domain, however:

+ +

Your email provider goes down/bans you: change your provider; keep the email.

+ +

Your website is pulled for controversial views: switch hosts.

+ +

Protect yourself; give yourself choices. +Why give others that power when you could have it for yourself?

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/07/19/multicraft-php-gentoo/index.html b/_site/2020/07/19/multicraft-php-gentoo/index.html new file mode 100644 index 0000000..9b26273 --- /dev/null +++ b/_site/2020/07/19/multicraft-php-gentoo/index.html @@ -0,0 +1,145 @@ + + + + + Installing MultiCraft on Gentoo Linux | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

Installing MultiCraft on Gentoo Linux

+ +
+
+ +

In a very odd combination of requirements, +I needed to install MultiCraft on a Gentoo Linux system. +The PHP USE flags are important so you don’t have to recompile it three times like I did.

+ +

Here are some useful tips I came across:

+ +

PHP USE flags

+ +

In /etc/portage/package.use/php I placed the following line:

+ +
+dev-lang/php cgi mysql mysqli fpm pdo gd truetype
+
+ +

This should give you enough for a mysql backended MultiCraft installation. +The cgi option may not be required as fpm stands for FastCGI Process Managment. +I don’t know for sure though.

+ +

Paper

+ +

This will grab the latest version of the Paper jar file using YivesMirror. +I’m not sure how reputable it is, +but my buddy who works with this stuff more often than me seemed to recognize it.

+ +
+## See the default craftbukkit.jar.conf for a detailed documentation of the
+## format of this file.
+[config]
+name = Paper 1.16.1 Latest
+source = https://yivesmirror.com/files/paper/Paper-1.16.1-latest.jar
+category = Mods
+
+[encoding]
+#encode = system
+#decode = system
+#fileEncoding = latin-1
+
+[start]
+command = "{JAVA}" -Xmx{MAX_MEMORY}M -Xms{START_MEMORY}M -XX:MaxPermSize=128M -Djline.terminal=jline.UnsupportedTerminal -jar "{JAR}" nogui
+
+ +

Other Tips

+ +

Do not use the option to setup a separate user for each server. +This completely stalled any work getting done with a ton of ‘permission denied’ errors.

+ +

Security

+ +

If the panel is in the root directory of your NGINX web server, +use the following in your server block to deny access to the /protected directory.

+ +
+location /protected {
+  deny all;
+  return 404;
+}
+
+ +
MySQL
+ +

It is always good practice to separate privileges. +The MultiCraft daemon should have one SQL login, +with one database allocated to it. +The MultiCraft panel should have a separate SQL login, +with a separate database allocated to it.

+ +

You can do this with the following commands in your MySQL prompt:

+ +
+sql> CREATE DATABASE multicraft_daemon_database;
+Query OK, 0 rows affected (0.01 sec)
+
+sql> CREATE DATABASE multicraft_panel_database;
+Query OK, 0 rows affected (0.01 sec)
+
+sql> CREATE USER 'muilticraft_daemon'@'localhost' IDENTIFIED BY 'strong password here';
+Query OK, 0 rows affected (0.01 sec)
+
+sql> CREATE USER 'multicraft_panel'@'localhost' IDENTIFIED BY 'different strong password here';
+Query OK, 0 rows affected (0.01 sec)
+
+sql> GRANT ALL PRIVILEGES ON multicraft_daemon_database . * TO 'multicraft_daemon'@'localhost';
+Query OK, 0 rows affected (0.01 sec)
+
+sql> GRANT ALL PRIVILEGES ON multicraft_panel_database . * TO 'mutlicraft_panel'@'localhost';
+Query OK, 0 rows affected (0.01 sec)
+
+
+ +

During setup, make sure the proper credentials are used for each step. +Database 1 is the panel database. +Database 2 is the daemon database.

+ +

Happy hacking :)

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/07/30/canadian-parliament/index.html b/_site/2020/07/30/canadian-parliament/index.html new file mode 100644 index 0000000..6d1540e --- /dev/null +++ b/_site/2020/07/30/canadian-parliament/index.html @@ -0,0 +1,96 @@ + + + + + Know How Your Representative Votes In Parliament | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

Know How Your Representative Votes In Parliament

+ +
+
+ +

As an advocate for openness, I had an idea to make a project out of the government of Canada’s Open Data +initiative to take a look at how my local MP voted on various pieces of legislation. +It turns out though that this was not necessary due to how easy it was to find this information on the government’s own website. +In this article, I will explain how you can do the same.

+ +

1. Find Your Representative

+ +

The first step in this process is to find who your representative is. +To do so, go to the government’s own website +ourcommons.ca’s search tool.

+ +

Simply type in your postal code in the search box to find out who your MP is.

+ +

2. Their Voting Record

+ +

Every MP’s voting record is public knowledge, +and it is available nice and simple in a table on that MP’s page. +For example, this is a link to +Pierre Poilievre’s voting record.

+ +

To find your MP’s voting record, do step one, then: +After the Overview, and Seat in The House sections, +there are three tabs, Roles, Work, and Contact. +Click on work. +At the bottom of that tab is a link which says Chamber Votes. +This will open a small window with some recent votes by this politician. +If you want to see all their votes, there is a button at the bottom named All Votes by This Member.

+ +

Tada! You can now keep your local MP accountable for anything you do or do not support.

+ +

3. Bill Details

+ +

If you want to get into the nitty gritty, +once you open a specific bill, you can actually find out the status of said bill, +or read the actual text by clicking the View this Bill on LEGISinfo button.

+ +

Both the status of the bill, and a link to a PDF document containing the bilingual text of the bill are visible in the main body of the page.

+ +

Conclusion

+ +

I thought this was pretty cool! +It was way simpler than I thought it would be.

+ +

Thanks, Canada!

+ + +
+ +
+
+ +
+ + diff --git a/_site/2020/08/15/openbsd1/index.html b/_site/2020/08/15/openbsd1/index.html new file mode 100644 index 0000000..e5db8e9 --- /dev/null +++ b/_site/2020/08/15/openbsd1/index.html @@ -0,0 +1,85 @@ + + + + + BSD Journey, Part 1 | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

BSD Journey, Part 1

+ +
+
+ +

As Linux becomes controlled by corporate sponsors and becomes more full of proprietary blobs, drivers, and even closed-source software like Steam, +One may wonder if there are other options out there. +For me, somebody that is intensely interested in security, there is one option: OpenBSD.

+ +

Now, my interest in OpenBSD has been going on for a long time. +I started poking around for Linux alternatives way back a few years ago when Linus Torvalds decided to leave after he got in trouble for some +unprofessional behaviour. +That said, Linus did come back to Linux development, +but I knew that his abrasive style is what brought good code to the Linux kernel. +I also knew that his ability to be critical would be hurt by the new +code of conduct. +It would become a tool for the SJW types to hammer on Linus for being a “white male, et al.”; +It would become a tool for the easily offended to use to get their dumb code into Linux; +It would become a tool for the corporatization, the HR-ification of Linux. +Frankly, this does not interest me.

+ +

Now I’m sure that OpenBSD has its own internal policies that I disagree with. +That said, Theo De Raadt is still at least known for calling Firefox an “amorphous peace of garbage” due to its lack of privilege separation. +And, in their project goals page, they specifically mention:

+ +
+

Be as politics-free as possible; solutions should be decided on the basis of technical merit.

+
+ +

Now that’s something I can get behind! +Bet you that’s not in the Linux COC?

+ +

He also went to university in my hometown, so that’s pretty cool! +I can support a local madman who thinks he can make a better operating system than all those corporations. +Maybe he was right, maybe not. What I know is I am excited to find out!

+ +

Wish my luck on my OpenBSD journey. I will post updates here along the way.

+ +

Happy hacking!

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/08/18/django-deployment/index.html b/_site/2020/08/18/django-deployment/index.html new file mode 100644 index 0000000..1346a6f --- /dev/null +++ b/_site/2020/08/18/django-deployment/index.html @@ -0,0 +1,172 @@ + + + + + How to Solve The Django Deployment Puzzle | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

How to Solve The Django Deployment Puzzle

+ +
+
+ +

A few days ago I had a Django project I wanted to put on a real server. +This project is still in its infancy, but I thought it would be nice to put it on my resume and show my friends. +Little did I know the headache coming my way. +Here are some tips to help you not make the same mistakes as me.

+ +

ASGI Servers

+ +

Because my project used the ASGI (Asynchronous webServer Gateway Interface), +I needed to find a good production ASGI server to handle all the incoming requests. +The best thing I found was uvicorn. +It focuses on speed, which is a priority, especially when using the ASGI protocol.

+ +

To run uvicorn on the command line for testing purposes, use something like the following:

+ +
+$ uvicorn --reload myapp.asgi:application
+
+ +

The --reload option says to reload the server if any of the files get updated. +This is not recommended in production. +Sadly, I thought this meant I would need to do a hard shutdown of the server process every time I wanted to update. +This turned out to not be the case.

+ +

Workload Managers

+ +

There is another equine-named program called gunicorn +which can hold a number of processes under its control. +An interesting feature of gunicorn is that it will gracefully switch from an old to a new deployment, +replacing the subprocesses one-by-one and eventually having only the new deployment active on all subprocesses. +The greatest part? Zero down time. +The server keeps any old processes open if there is communication with them, +then shift and new connections to the new deployment. +This was a very cool feature I wanted to take advantage of.

+ +

“Now hold on!” you might protest. +“gunicorn is a WSGI server!” … oh you got me there! +Yes, that’s right, gunicorn is paired with uvicorn to serve my files.

+ +

systemd

+ +

Love it or hate it, the majority of Linux distributions use the systemd init system. +I decided it would be very convenient to have a .service file for my Django application to run automatically at boot. +Systemd allows me to do this with a file like the following one I stored in /lib/systemd/system/lamegames.service.

+ +
+[Unit]
+Description=Gunicorn/Uvicorn (lamegames.io)
+
+[Service]
+WorkingDirectory=/home/lame/lamegames.io
+Type=simple
+RemainAfterExit=yes
+ExecStart=/home/lame/lamegames.io/env/bin/gunicorn lamegames.asgi:application -w 2 -k uvicorn.workers.UvicornWorker
+ExecStop=/bin/kill -HUP $MAINPID
+Restart=always
+
+[Install]
+WantedBy=multi-user.target
+
+ +

nginx

+ +

NGINX (pronounced engine-X) is a performance web server designed for speed and simplicity. +For the front facing side of the site, I do need a production web server like nginx. +Gunicorn simply doesn’t need all the features that nginx provides, but I do. +To configure my nginx installation, I used the following few directives to:

+ +
    +
  1. Redirect most traffic towards the gunicorn server.
  2. +
  3. Redirect statically served files (CSS, JS, images) to the directory specified in the STATIC_ROOT variable of my settings.py file.
  4. +
  5. Use TLS to enable https://
  6. +
+ +

Serving the static files from nginx as opposed to the gunicorn server is necessary. +Gunicorn and other production A/WSGI web server will not set the proper MIME type over TLS. +This will cause your browser to not load the Javascript/CSS.

+ +

This is the important part of my nginx config.

+ +
+server {
+    location / {
+        proxy_set_header Host $http_host;
+        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+        proxy_set_header X-Forwarded-Proto $scheme;
+        # these two lines ensure that WebSocket, and HTTP2 connection are forwarded correctly
+        proxy_set_header Upgrade $http_upgrade;
+        proxy_set_header Connection "Upgrade";
+        proxy_redirect off;
+        proxy_buffering off;
+        # this forwards all traffic to the local server on port 8000
+        proxy_pass http://localhost:8000;
+    }
+
+    # This forwards all static requests to Django's STATIC_ROOT set in settings.py; it is generated using the collectstatic command.
+    location /static {
+        autoindex on;
+        alias /home/lame/lamegames.io/static_generated;
+    }
+}
+
+ +

Setup

+ +

After all that, I was able to do the following:

+ +
+# systemctl enable lamegames
+
+ +

This enabled my gunicorn server to run once the server started. +NGINX is that way be default.

+ +

And tada! You now have a working Django project on a production server!

+ +

Notes

+ +
    +
  • If using ws:// websockets, change them to wss:// for secure web sockets.
  • +
  • Make sure to use channels.routing.get_default_application() instead of django.get_asgi_application() if your’re wanting to use channels/redis WebSockets.
  • +
+ +
+ +
+
+ +
+ + diff --git a/_site/2020/09/09/lamegames/index.html b/_site/2020/09/09/lamegames/index.html new file mode 100644 index 0000000..9c5ddae --- /dev/null +++ b/_site/2020/09/09/lamegames/index.html @@ -0,0 +1,60 @@ + + + + + lamegames.tait.tech | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

lamegames.tait.tech

+ +
+
+ +

This is an announcement for a new project of mine: +lamegames.tait.tech.

+ +

This is something I’m really excited to work on!

+ +

Right now, I’ve just got a rock-paper-scissors game. +A chat function, and a few simple card games to come.

+ +

Check out the repository on my Github.

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/09/12/minesweeper/index.html b/_site/2020/09/12/minesweeper/index.html new file mode 100644 index 0000000..eb077cc --- /dev/null +++ b/_site/2020/09/12/minesweeper/index.html @@ -0,0 +1,144 @@ + + + + + Minesweeper Bomb Generation And Tile Revealing | tait.tech + + + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

Minesweeper Bomb Generation And Tile Revealing

+ +
+
+ +

When I was creating a little Minesweeper game, I got confused at some points. +My bomb generation didn’t look quite right, and I for sure didn’t quite get the whole cascading tile reveal thing. +With a bit of internet research, I found what I was looking for. +I’ll explain it all in one place for my own research purposes.

+ +

Bomb Generation

+ +

When I started this project I attempted to use a random bomb generator. +By this I mean on each square, before it gets generated, give it a one in 15 change of being a bomb. +Personally, I’m not sure why this never looked right. +Something about the layout of the bombs did not mimic the classic Minesweeper game.

+ +

After looking at some open source Minesweeper examples, I started to get the idea. +I wrote some mathematical statements describing the generation of bombs and how to get their x,y position from an appropriate number. +For those non-mathy people, don’t leave just yet; +there will be code equivalents to the math.

+ +

W and H are the width and height of the board respectively.

+ +

0rW×H +\it 0 \leq r \leq \text W \times \text H + +x=rmodW +\it x = r \bmod \text W + +y=rH +\it y = \left\lfloor\frac{r}{\text H}\right\rfloor +

+ +

The code equivalent to this in Python is below:

+ +
import random
+# r <= 0 <= W*H
+r = random.randint(1, W*H)-1
+
+# x = r mod W
+x = r % W
+
+# y = floor(r/H); note the special syntax python has for this operation
+y = r // H
+
+ +

So that’s that, we can put this in a big ‘ol for loop and generate an arbitrary n number of bombs given a width and height of a Minesweeper board.

+ +

Cascading Tile Revealing

+ +

This one is hard to describe; +I am adapting this from leetcode.com. +Whenever a player clicks a tile, the following logic should be used:

+ +
    +
  1. If a mine is revealed, the game is over. (obviously)
  2. +
  3. If a tile with no adjacent mines is revealed, recursively reveal all eight adjacent tiles.
  4. +
  5. If a tile with one or more adjacent mines is revealed, display the number of mines next to it.
  6. +
+ +

Here is the code in Python for this algorithm.

+ +
def reveal_square(x, y, board, alread_revealed):
+  # if already checked
+  if (x, y) in already_revealed:
+    return
+  # if it's a bomb
+  if board[x][y] == 'B':
+    you_lose()
+    return
+  # if the bomb number is more than 0
+  already_revealed.append((nx, ny))
+
+  # from -1 to 1
+  for xd in range(-1, 2):
+    for yd in range(-1, 2):
+      # skip if it is this the center tile
+      if x+xd == x and y+yd == y:
+        continue
+      # recursively check the adjacent square
+      reveal(x+xd, y+yd, board, already_revealed)
+  return already_revealed
+
+ +

This has no checks for valid squares, but it’s the general idea. +This function returns an array of tile coordinates which should be revealed.

+ +

Conclusion

+ +

I wrote this because in the first place because I was writing my own Minesweeper game. +I hope that this helps you with getting the general idea of a Minesweeper game. +The completed version of this game is available on my lamegames site. +Let me know what you think!

+ +

Happy hacking!

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/10/26/curiosity/index.html b/_site/2020/10/26/curiosity/index.html new file mode 100644 index 0000000..c3bd42b --- /dev/null +++ b/_site/2020/10/26/curiosity/index.html @@ -0,0 +1,123 @@ + + + + + Curiosity | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

Curiosity

+ +
+
+ +

Curiosity is fundamental to a deep understanding of any subject. +Masters, Ph.Ds, and other fancy name suffixes will never help you +if you don’t have the spirit of curiosity burning inside of you.

+ +

I was speaking to someone from a journalism major at my school when the subject of hacking arose. +I expected her to know nothing about it, being a journalism student and all, but surprisingly she had something to say about it:

+ +
+

“The best hackers are the ones who are curious.”

+
+ +

That struck a cord with me. +It seems to me she has nailed down the difference between the students who care about grades, +and those who want to learn. +These are not necessarily mutually exclusive, but in my experience they often are due to the way education is structured.

+ +

My Anecdote

+ +

In my second semester at SAIT Polytechnic, I took a class entitled Emerging Trends In Technology. +This class was probably the best class I have ever taken. +We had to combine two things:

+ +
    +
  • Hard skills: learning a new hard skill like Angular, Django, or GPG encryption.
  • +
  • Soft skills: public speaking and presentation of our ideas.
  • +
+ +

Soft skills are not usually my area, but I can do public speaking. +I grew up quite religious, so public speaking was drilled into me young. +I liked to go off script and talk about interesting things I found along the way to the actual point. +My creativity was not usually encouraged. +That said, going off script is useful when teaching and presenting ideas; +it gives a natural air to your breath and an unquestionable confidence in your speech.

+ +

This is how we learn: in relationships. +Try explaining ancient Japanese history to a computer science major, or UNIX sockets to an English major and you’ll see what I mean. +If there is nothing for us to connect the knowledge to, it dissipates.

+ +

So why did I do so well in this class?

+ +

Our task for the semester was as follows:

+ +
    +
  1. Learn a new subject (any emerging trend in technology) which you find fascinating.
  2. +
  3. Give a one minute introduction by week three.
  4. +
  5. Give a 10 minute non-technical overview by week 8.
  6. +
  7. Give a 20 minute technical explaination and demo by week 13.
  8. +
+ +

This is the only course I have ever taken which lets students’ imagination run wild. +Their presentation, their rules. +They treated the students like adults who know what they are doing. +What happened? Everyone stopped coming because “Oh no! Presentations!”?

+ +

No, exactly the opposite. +There was never more than one student missing. +Every single presentation was at least moderately interesting, +and most students were excited to come to that class. +You could see it in their faces, the way they carried themselves. +Every student picked something unique to their tastes, leaving every student more educated than before.

+ +

This class, unlike many others, encouraged the curiosity of the students. +It rewarded those who had unique interests and an ability to sell others on their ideas.

+ +

The curiosity and the grades were one.

+ +

Conclusion

+ +

Although it’s nice to have a course where these goals align here and there, anyone who has been to collage or university can tell you that is far from the norm.

+ +

On the other hand, I never would have started this site if it wasn’t for that class alone. +So I thank you, Kitty Wong, for getting me started running my own “research blog” (?)

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/11/15/nas2/index.html b/_site/2020/11/15/nas2/index.html new file mode 100644 index 0000000..36e760b --- /dev/null +++ b/_site/2020/11/15/nas2/index.html @@ -0,0 +1,145 @@ + + + + + ZFS NAS Box, Part 2 | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

ZFS NAS Box, Part 2

+ +
+
+ +

Back in part one of my NAS project I discussed how I wanted to set up my hardware. +Today, I set up the NAS (almost).

+ +

There were some hiccup along the way, like learning that M.2 slots can disable some of your SATA ports or waiting a month for a host bus adapter to come in from China.

+ +

Why Did It Take So Long

+ +

So it turns out I was going to spend a lot more on this project than I originally anticipated. +I ended up getting a server machine instead of a sleek NAS box. +Here are some of the quick specs:

+ +
    +
  • Standard ATX case by Thermaltake.
  • +
  • LSI 9211-8i.
  • +
  • The cheapest HDMI graphics card I could find on Kijiji.
  • +
  • 6x 3TB Segate HDDs.
  • +
  • 1x 250G Kingston SSD.
  • +
  • AMD Ryzen 5 3600.
  • +
  • MSI B450 Gaming Plus Max.
  • +
  • 2x 8GB FlareX 3200Mhz RAM.
  • +
  • 1x 16GB Kingston 3200Mhz RAM.
  • +
+ +

ZFS

+ +

This is how I decided to configure my storage pools. +In hindsight, this was not the best choice for upgrading. +I may change it in the future to a 0+1 setup, but it works for now.

+ +

I have 5x 3TB in a RAIDZ2 with one drive not attached for redundancy’s sake. +How does one setup a ZFS pool. Check this out:

+ +
+# zpool create poolname raidz2 \
+/dev/by-id/blahblahblah1 \
+/dev/by-id/blahblahblah2 \
+/dev/by-id/blahblahblah3 \
+/dev/by-id/blahblahblah4 \
+/dev/by-id/blahblahblah5
+
+ +

And zippidy-doo! We’ve got a ZFS pool! +We can check its status with zpool status.

+ +
+$ zfs status
+  pool: raid
+ state: ONLINE
+  scan: scrub in progress since Wed Nov 18 18:41:41 2020
+    1.84T scanned at 8.51G/s, 121G issued at 562M/s, 1.84T total
+    0B repaired, 6.45% done, 0 days 00:53:25 to go
+config:
+
+    NAME                                         STATE     READ WRITE CKSUM
+    raid                                         ONLINE       0     0     0
+      raidz2-0                                   ONLINE       0     0     0
+        ata-HGST_HUS724030ALA640_PN2234P8JTNMYY  ONLINE       0     0     0
+        ata-HGST_HUS724030ALA640_PN2234P8JVSXTY  ONLINE       0     0     0
+        ata-HGST_HUS724030ALA640_PN2234P8JXAS8Y  ONLINE       0     0     0
+        ata-HGST_HUS724030ALA640_PN2234P8JXBARY  ONLINE       0     0     0
+        ata-HGST_HUS724030ALA640_PN2234P8JXP77Y  ONLINE       0     0     0
+
+errors: No known data errors
+
+ +

I had run a scrub right before this, so there’s some extra detail in that. +This is really fun! I will be doing more home storage projects soon. +Perhaps Raspberry Pi NAS using all 4 USB ports to load SATA drives on it. +Now that would be fun!

+ +

So I Kinda Have A NAS Now…?

+ +

So right now I can only copy files with rsync, scp and moving data via a physical drive. +The one major disadvantage this has is speed.

+ +

Due to this machine being connected directly outside my network and pulling DHCP like a normal router would, I need to send my data through the WAN connection to get my files to it. +This is rather unfortunate as my upload speed is capped at 20 megabits per second, despite my upload being in the 300+ range.

+ +

Part 3 will involve a LAN card so I can connect both to the DHCP server of my ISP and my local router. +This way my transfer speeds should be in the range of 1 gigabit per second. +This will make my life much easier, at least on the local network.

+ +

Fun Fact!

+ +

Do not try to use the M.2 slot on a consumer motherboard where you are also using all the SATA ports. +On my consumer gaming motherboard, the SATA ports next to the M.2 slot became disabled when I attached the M.2 SSD. +I found this out form my motherboard documentation, which I read only after a week of thinking my motherboard itself was defective, and sending it in for repairs that did absolutely nothing.

+ +

Thoughts

+ +

I like having all this space. I plan on using it up pretty fast, so I’m already looking at how to expand. +Hopefully that gives a decent overview of how I set up my drives.

+ +

Happy hacking!

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/12/01/pacaur-rpi/index.html b/_site/2020/12/01/pacaur-rpi/index.html new file mode 100644 index 0000000..c9dae4c --- /dev/null +++ b/_site/2020/12/01/pacaur-rpi/index.html @@ -0,0 +1,127 @@ + + + + + Getting Pacaur Working on a Raspberry Pi 4 with Manjaro ARM or Arch Linux | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

Getting Pacaur Working on a Raspberry Pi 4 with Manjaro ARM or Arch Linux

+ +
+
+ +

I recently installed Manjaro ARM (based on Arch Linux ARM) on a Raspberry Pi 4. +I used some standard commands to start to add the pacaur package so I can easily retrieve AUR packages without needing to do it manually. +Unfortunately, there is a small problem with compiling this on ARM.

+ +

always_inline

+ +

To setup the install for pacaur, I first needed to download auracle-git AUR package manually. +I ran into an error when compiling this package.

+ +

But first, my setup:

+
+$ git clone https://aur.archlinux.org/auracle-git
+$ cd auracle-git
+$ makepkg -sri
+
+ +

Around half way through compiling this project, I got this cryptic message telling me there was a “target specific option mismatch”…Whatever that means. +The full error is below, hopefully that helps my chances on the search engines.

+ +
+In file included from ../subprojects/abseil-cpp-20200225.2/absl/random/internal/randen_hwaes.cc:225:
+/usr/lib/gcc/aarch64-unknown-linux-gnu/9.3.0/include/arm_neon.h: In function 'Vector128 {anonymous}::AesRound(const Vector128&, const Vector128&)':
+/usr/lib/gcc/aarch64-unknown-linux-gnu/9.3.0/include/arm_neon.h:12452:1: error: inlining failed in call to always_inline 'uint8x16_t vaesmcq_u8(uint8x16_t)': target specific option mismatch
+12452 | vaesmcq_u8 (uint8x16_t data)
+
+ +

Luckily, there is a very easy fix for this. +The user redfish helpfully pointed out +on the auracle-git package page that you need to add a special make option to your /etc/make.conf file to make this work.

+ +

His solution, as commented is like so:

+ +
+

If you get this error when building for ARM aarch64:

+ +

(insert error message from before)

+ +

Then check that in /etc/makepkg.conf CFLAGS and CXXFLAGS have the +crypto suffix in -march flag, like -march=armv8-a+crypto (the base identifier may very depending on your hardware)

+
+ +

Basically, there is a file on Linux: /etc/makepkg.conf which tells your computer how to compile all programs on the system. +By default the Manjaro ARM (RPi4) edition has the following relevant lines in makepkg.conf.

+ +
+CFLAGS="-march=armv8-a -O2 -pipe -fstack-protector-strong -fno-plt"
+CXXFLAGS="-march=armv8-a -O2 -pipe -fstack-protector-strong -fno-plt"
+
+ +

What Mr. redfish is telling us is that we must add ‘+crypto’ to the end of the -march compiler flag so that our compiler will know how to inline that pesky vaesmcq_u8 function.

+ +

So in the end, your makepkg.conf’s relevant lines will look like so:

+
+CFLAGS="-march=armv8-a+crypto -O2 -pipe -fstack-protector-strong -fno-plt"
+CXXFLAGS="-march=armv8-a+crypto -O2 -pipe -fstack-protector-strong -fno-plt"
+
+ +

Why?

+ +

Redfish continues:

+ +
+

Build of abseil-cpp package works because it uses CMake which adds the correct -march flag regardless of makepkg.conf, whereas when abseil-cpp is build as a subproject within this package, it uses meson, which does not add the flag and thus fails with the above error.

+
+ +

In other words, one of the dependencies pulled in with auracle is not compiling without this special compiler flag enabled.

+ +

Conclusion

+ +

Thanks to redfish for posting this solution to the forums! +Would’ve been quite the rabbit hole for me to figure out how to do that. +In fact, it is very likely I would have never figured that one out.

+ +

After this issue is resolved, the installation of pacaur goes as expected. Nice and easy! +Pacuar will compile on any architecture so it’s smooth sailing from here.

+ +

Happy hacking!

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/12/14/orca-raspberry-pi-manjaro/index.html b/_site/2020/12/14/orca-raspberry-pi-manjaro/index.html new file mode 100644 index 0000000..6f6d9a8 --- /dev/null +++ b/_site/2020/12/14/orca-raspberry-pi-manjaro/index.html @@ -0,0 +1,234 @@ + + + + + Orca, Emacspeak and Chromium Accessibility on A Raspberry Pi Running Manjaro ARM | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

Orca, Emacspeak and Chromium Accessibility on A Raspberry Pi Running Manjaro ARM

+ +
+
+ +

I wanted to get a gift for my blind friend who has been interested in learning Linux for a while now. +Just when I was about to start looking for something I decided to take a look at Brian Lunduke’s newest video featuring the Raspberry Pi 400. +The Raspberry Pi 400 has come full circle in terms of computing. +It is a keyboard. All the computing is done from within the keyboard. +Much like the Comodore64, this computer comes without a screen but is still technically fully functional without one. +I had my ‘Aha!’ moment, and decided that the Raspberry Pi 400 would be a very cool present.

+ +

No Headphone Jack

+ +

My first problem was that the Raspberry Pi 400 does not come with a headphone jack, +even though every other Raspberry Pi A/B/B+ board with the exception of the compute modules and Zero series have included one. +That said, the Raspberry Pi audio jacks are also known to crackle and pop due to either bad drivers or cheap manufacturing.

+ +

To compensate, I got an external USB audio card. +I ended up going with the DuKabel USB to 3.5mm Jack Adapter (Amazon).

+ +

There are cheaper options for sure, but when I was looking around Raspberry Pi forums and Amazon reviews, this specific model seemed to come out on top in terms of performance. +It costed me around 40 Canadian dollars. +I figure this isn’t too bad considering my friend doesn’t need a screen, haha!

+ +

Distribution

+ +

I wanted to choose something which will age well, and run the latest and greatest. +For visually impaired users, the most important piece of software is the screen reader. If the screen reader doesn’t work, the rest of the work is toast! +Some Raspberry Pi Orca installation guides like this one, +have actually asked the user to compile Orca from source to get the latest version due to how out-of-date Debian’s package repositories are. +A distribution which has none of these fusses was top priority, +especially because Orca receives frequent updates.

+ +

I decided to go with Manjaro ARM Xfce:

+ +
    +
  • Xfce is lightweight.
  • +
  • Runs a 64-bit kernel.
  • +
  • Is based on Arch Linux, so receives very frequent package updates.
  • +
  • Has the AUR (with some effort, as we’ll see).
  • +
+ +

The only downside of Manjaro ARM, and likewise other Aarch64 (ARM 64-bit) architecture kernels is that it did not have HEVC nor H264 hardware decoding available. +Apparently, support for the Raspberry Pi’s VC4 graphics is getting mainlined in Linux kernel 5.10. +Unfortunately, running the release candidate (RC) kernel did not make the Raspberry Pi 400 use hardware decoding. +Perhaps other applications like ffmpeg and Chromium need to add support as well before this works.

+ +

Orca

+ +

As stated before, minimum requirement for a visually impaired desktop Linux user is a screen reader. +The most used screen reader for Linux is Orca, headed by the GNOME Project. +This is relatively easy to install with a standard pacman command.

+ +
+# pacman -S orca
+
+ +

If logged in via SSH, you can start Orca with the orca command. This will start reading the screen to you so you can do the next parts.

+ +

To activate Orca on login with LightDM, enable it in the ‘LightDM GTK+ Greeter Settings’ application. The ‘Misc.’ tab will have a drop-down for asking which screen reader you want to use. +This will start Orca when the LightDM login system starts.

+ +

LightDM’s Orca will not help us once we are logged in however. +To activate Orca on login, open the ‘Session And Startup application’ program, then add Orca to the “startup” list of apps.

+ +

And now Orca will be activated on boot and login.

+ +

AUR

+ +

The AUR, or the Arch User Repository has community maintained and distributed packages. +Basically, you download a file which will tell your computer how to download, build and install a package for you.

+ +

To get the AUR working in a more automatic way, we need to install the pacaur helper. +This merited its own article, so check that out here: +How to Install Pacaur on Manjaro ARM

+ +

The TL;DR is that we need to open the /etc/makepkg.conf file and replace any mention of -march=armv8-a with -march=armv8-a+crypto.

+ +

This can be done as a oneliner, thanks to the sed command.

+ +
+# sed -i 's/-march=armv8-a/-march=armv8-a+crypto/' /etc/makepkg.conf
+
+ +

Emacspeak

+ +

Emacs is a scriptable document editor run by the GNU Project. +Infamous for its high learning curve though it may be, +there is a very extensive speech extension for it called Emacspeak. +Emacspeak is built entirely by T. V. Ramen and has been freely available since its inception in 1999. +Due to its age (and thus maturity), Emacspeak is an important tool in any “eyes-free” software developer toolkit.

+ +

Lucky for me, Emacspeak appears to not only to be available in the AUR, +but also compilable with the Aarch64 architecture—the architecture of the Raspberry Pi 400. +So this is as easy to install as:

+ +
+$ pacaur -S emacspeak
+
+ +

Web Browsers

+ +

I’ll cover a couple web browsers here based on how they worked.

+ +

Firefox

+ +

Firefox will run in accessibility mode with caret browsing enabled automatically when it detects Orca is running. +This makes Firefox by far the best browser for the job.

+ +

Chromium

+ +

Chromium… Ugh. So it works, sort of. +You need to enable two special flags, and add an environment variable.

+ +

To do this, I suggest editing the main profile in /etc/profile. +Add the following line to the end of that file:

+ +
+export ENABLE_ACCESSIBILITY=1
+
+ +

Next, add two flags to the $HOME/.config/chromium-flags.conf file.

+ +
+--force-renderer-accessibility
+--enable-caret-browsing
+
+ +

You will need to relog to set the ENABLE_ACCESSIBILITY environment variable. Now Chromium should work with Orca.

+ +

Badwolf

+ +

The Badwolf browser +is based on the WebkitGTK engine, as opposed to Firefox’s Gecko and Google’s Blink engines. +It almost works out of the box. It is really fast compared to the other two, +but it lacks some features like announcing a page is done loading and it doesn’t appear to support caret browsing, +which will cause some other problems. +Interesting though for such a young browser to have a minimal level of accessibility so early!

+ +

I would be interested where this goes in the future.

+ +

P.S.

+ +

Somehow I forgot that you would need to setup the dummy display driver to work with the graphics without a display attached. +To do this, install the xf86-video-dummy pacakge:

+
+# pacman -S xf86-video-dummy
+
+ +

Next, override any /etc/X11/xorg.conf you may have with this:

+ +
+Section "Monitor"
+	Identifier "Monitor0"
+	HorizSync 28.0-80.0
+	VertRefresh 48.0-75.0
+	# https://arachnoid.com/modelines/
+	# 1920x1080 @ 60.00 Hz (GTF) hsync: 67.08 kHz; pclk: 172.80 MHz
+	Modeline "1920x1080_60.00" 172.80 1920 2040 2248 2576 1080 1081 1084 1118 -HSync +Vsync
+EndSection
+
+Section "Device"
+	Identifier "Card0"
+	Driver "dummy"
+	VideoRam 256000
+EndSection
+
+Section "Screen"
+	DefaultDepth 24
+	Identifier "Screen0"
+	Device "Card0"
+	Monitor "Monitor0"
+	SubSection "Display"
+		Depth 24
+		Modes "1920x1080_60.00"
+	EndSubSection
+EndSection
+
+ +

Now, when you boot, you should head ‘Screen reader on’.

+ +

Conclusion

+ +

It took a bit of messing around to get this working, but I’m glad I did! +Now I can nerd out with another Linux friend and given how well they seem to find problems with accessibility, hopefully a few upstream patches can be made.

+ +

Happy hacking, for everyone!

+ +
+ +
+
+ +
+ + diff --git a/_site/2020/12/20/deploy-lichess/index.html b/_site/2020/12/20/deploy-lichess/index.html new file mode 100644 index 0000000..6e7decc --- /dev/null +++ b/_site/2020/12/20/deploy-lichess/index.html @@ -0,0 +1,148 @@ + + + + + How to Deploy Lichess's Lila With Nginx | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

How to Deploy Lichess's Lila With Nginx

+ +
+
+ +

I was getting ready to have a public test of some changes I made to lichess.org’s open source chess platform. +In preperation, I got my Let’s Encrypt certificates and nginx configurations setup… +and it wouldn’t work. +Here are some tips for myself and future Lichess developers.

+ +

Reasoning

+ +

My pull request involves accessibility. +It will extend Lichess’s NVUI (Non-Visual User Interface) to be more accessible to beginner level chess players. +At the time of writing this, Lichess’s NVUI only supports searching pieces by type, rank and file. +It does not support any kind of interactive board.

+ +

I wanted to play chess with a friend of mine who uses a screen reader. +Even though Lichess does indeed have a separate rendering of the page for visually impaired users, +I have heard from a few people that it is not the best.

+ +

I don’t use a screen reader myself, so I thought having a public latest changes deployed server would work better for testing. +It would certainly work better than getting some of my less computer literate friends to connect to me via VSCode/VPN and view my local repository.

+ +

So here is how to deploy it:

+ +

Setup a development environment

+ +

This is described in Lichess’s documentation itself. +I will not elaborate further as it is not necessary.

+ +

Setup nginx

+ +

This is the part that stumps most people. +Getting a local development server usually works alright, but once you want to reverse proxy it for security and professionalism purposes, it get more interesting.

+ +

Here is the relevant portion of my nginx configuration for lila:

+ +
+server_name chess.tait.tech;
+
+location / {
+  proxy_pass http://127.0.0.1:9663;
+  proxy_set_header Host $host;
+  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+  proxy_set_header X-NginX-Proxy true;
+  proxy_set_header X-Real-IP $remote_addr;
+}
+
+ +

This is the config for the lila-ws websocket subdomain:

+ +
+server_name ws.chess.tait.tech;
+
+location / {
+  proxy_pass http://127.0.0.1:9664;
+  proxy_http_version 1.1;
+  proxy_set_header Upgrade $http_upgrade;
+  proxy_set_header Connection "upgrade";
+}
+
+ +

You will need to deploy these on two virtual hosts.

+ +

Lila

+ +

Lila is the name for the main chess server, we need to change a few settings. Here is my git diff for the conf/base.conf file:

+ +
+-  domain = "localhost:9663"
+-  socket.domains = [ "localhost:9664" ]
++  domain = "chess.tait.tech"
++  socket.domains = [ "ws.chess.tait.tech" ]
+   asset.domain = ${net.domain}
+-  asset.base_url = "http://"${net.asset.domain}
++  asset.base_url = "https://"${net.asset.domain}
+   asset.minified = false
+-  base_url = "http://"${net.domain}
++  base_url = "https://"${net.domain}
+
+ +

Lila-ws

+ +

Lila-ws is the websocket component of Lila.

+ +

The most common complaint amongst aspiring Lichess developers is websockets not working. +They constantly get these ‘101’ responses from the websocket, +and it also seems that the websocket returns instead of staying in the ‘pending’ state as it should be.

+ +

Here is how to fix that (in diff format):

+ +
+-csrf.origin = "http://127.0.0.1:9000"
++csrf.origin = "https://chess.tait.tech"
+
+ +

You need to tell lila-ws where the websocket requests will be coming from. This is how to do that.

+ +

Conclusion

+ +

This is not a long article, but just some notes for future me and Lila developers.

+ +
+ +
+
+ +
+ + diff --git a/_site/2021/01/31/lichess/index.html b/_site/2021/01/31/lichess/index.html new file mode 100644 index 0000000..fe8df89 --- /dev/null +++ b/_site/2021/01/31/lichess/index.html @@ -0,0 +1,116 @@ + + + + + Lichess Accessibility | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

Lichess Accessibility

+ +
+
+ +

I wanted to play chess with somebody who used a screen reader, without requiring a screen reader myself; +some sites, like QuintenC’s Playroom have a rather poor visual interface for anyone who would like the play the game visually. +Lichess is an free and open-source website for chess players; +it bridges this gap by having two “modes” on the site: +standard mode and accessibility mode.

+ +

Accessibility Mode

+ +

Accessibility mode is far from perfect on lichess.org. +That said, the idea to separate the sites into different modes was a good call. +It stops the inevitable “this would work well for screen readers but cause visual issues” shenanigans, +or, vice-verse “this looks great but it might be weird with a screen reader”. +This way all the things which affect the visual interface are in one place, +and all things which affect the non-visual user interface (NVUI) are written in another.

+ +

In my quest to play chess with visual and non-visual players with both having optimal experiences, I tried Lichess with my friend from melly.tech. +She pointed out that the method to interface with the board previously was rather poor. +This is because it required an “enter” at the end of each command and the commands tended to read out a row or column of a chessboard not just an individual square.

+ +

For example, to list all pieces (or lack thereof) on the e file, I would type the command:

+ +
s e
+
+ +

Although this seems good in theory, and it’s great when you need an entire file, there was no way to get only one square. +In addition, imagine typing to navigate around the board:

+ +
s e1
+s f1
+s e2
+
+ +

For the inexperienced player, it seems to be more convenient to bind some keys and have the user bounce to various buttons, which they can push to say “I want to move this piece”. +This is what I was told anyway. +So I want to work making a system so you could use the following basic keys:

+ +
    +
  • left/right/up/down arrow: move on the board.
  • +
  • k/q/r/b/n/p: move to next piece represented by its character in chess notation.
  • +
  • shift + k/q/r/b/n/p: move back to the last piece represented by its character in chess notation.
  • +
  • click/enter/space: select piece to move.
  • +
  • click/enter/space again: move piece here.
  • +
  • m: show where I can move with this piece.
  • +
  • shift+m: show where I can capture with this piece.
  • +
  • 1-8: move to rank 1-8; stay on same file.
  • +
  • shift + 1-8: move to file a-h; stay on same rank.
  • +
+ +

This gives a pretty solid basis for playing the game. +One caveat is after you have moved a pawn all the way to the farthest rank, only the destination tile will accept your promotion choice. +Therefore, all the other keys still work on other square, but if you are on the destination square of a promotion q/r/b/n will promote your piece, not jump you to the next/previous one.

+ +

This pull request was merged earlier this month:

+ +

More To Come

+ +

Next thing I want to do is implement the analysis board. +Right now it is not accessible whatsoever.

+ +

Help Me

+ +

If you are a screen reader user or know about accessibility and want to help make Lichess an awesome chess site for sighted and unsighted players alike, +then send me an email at tait@tait.tech and I’ll BCC you once I start testing the analysis board.

+ +

Happy hacking, y’all!

+ +
+ +
+
+ +
+ + diff --git a/_site/2021/04/04/quiz-your-friends-xss/index.html b/_site/2021/04/04/quiz-your-friends-xss/index.html new file mode 100644 index 0000000..990c810 --- /dev/null +++ b/_site/2021/04/04/quiz-your-friends-xss/index.html @@ -0,0 +1,160 @@ + + + + + The "Quiz Your Friends" XSS Exploit | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

The "Quiz Your Friends" XSS Exploit

+ +
+
+ +

Note: I have alerted the administrators of this site multiple times about this vulnerability. +One email was sent many years ago, which is more than enough time for responsible disclosure.

+ +

Update: They have fixed the vulnerability as of the day of release for this article.

+ +

Background

+ +

In early 2014, when my “programming” skills consisted of editing web pages with inspect element, I was sent a link from an old friend in a town about 3 hours away. +This was a link to a quiz about them. +I had to answer as many questions right as I could about them and I got a score at the end based on my answers. +It seemed fun enough, so I went for it. +In the following weeks this quiz website became quite a trend amongst my friend group as we all started making quizes to see how well we all knew eachother.

+ +

A few weeks into this trend, I was staying at a friends’ place and told him about this site, +so he goes and creates his own quiz and sends it to all his friends, group chats, Google Plus groups, et cetera.

+ +

Hackerman

+ +

While filling in my friend’s survey I thought it would be +funny for them to know it is me without anyone else knowing. +We were young and had Inspect Elemented a few things together, +so it was a safe bet that an HTML joke would let them know.

+ +

I decided to write my name like so: <b>Steve</b>. +Steve is in reference to the main character in the video game Minecraft.

+ +
+ +
+

Me typing in my name as <b>Steve</b>.

+
+
+ +

Now in theory this should have shown in in the leaderboard as: “<b>Steve</b>” +However, to my horror and excitement, I saw this in the leaderboard:

+ +
+ +
+

<b>Steve</b> displaying in the leaderboard as bold text: Steve

+
+
+ +

The text “Steve” showed up in bold on the leaderboard. +This told me all I needed to know. +How did this happen? You might wonder.

+ +

Server-Side Validation

+ +

Here is a great demonstration why you should do most of your validation on the server side. +As a user, I can edit any of the HTML, CSS, or Javascript your server serves to me.

+ +

Quiz your friends uses the maxlength=20 HTML attribute on the name input field. +Imagine trying to fit in a script tag doing anything useful with 20 characters! Don’t forget that includes the <script> tag. +That would leave 13 characters for Javascript. +Although I’m sure a genius would be able to code golf that, I know I couldn’t.

+ +

Now obviously I can edit any HTML that a server has sent to me. +If I open up my inspect element window, I can go ahead and change that maxlength attribute to anything I want. +Let’s change it to 100!

+ +
+ An image of the Quiz Your Friends name input field with inspect element. THe code reads: <font class="style6"><input class="inputbutton" name="takername" type="text" id="takername" maxlength="20" width="425" placeholder="Your First Name" style="text-align: center; text-decoration:inherit; font-size:38px;" tabindex="-1"></font> +
+ Manually changing the maxlength attribute. +
+
+ +

In theory, there is a way that a site can stop people from just putting in their name of any length: server-side validation. +The server could check to see if the input is too long and reject it if it is. +The Quiz My Friends server has no such checks in place. +Therefore, I can send an almost arbitrary load to them. +Being able to send something potentially very large (more than a few megabytes) is a vulnerability of its own. +Imagine being able to send entire executable programs as your “name” in one of these quizzes?

+ +

Javascript

+ +

So I went on my merry way thinking about ways to use malicious javascript. +Then, I thought that might be mean, so I decided to warn users instead. +I filled in the name with a script tag and a call to alert() to warn the user about this site. +I edited the max-length attribute to allow me to type a long string like this:

+ +
<script>alert("Don't use this site. It is not secure!");</script>
+
+ +

Sure enough, I got a text from my friend saying: “Tait! I know this is you, why would you do that!” +A bit salty, but who wouldn’t be.

+ +

Cross-Site Scripting (XSS)

+ +

As my final act, I decided to use a cross-site script that I could edit and have it load with new changes at any time.

+ +

I set this as my name:

+ +
<script src="https://tait.tech/assets/js/hacked.js"></script>
+
+ +

This script pops up a warning, telling the user that the site is insecure and it is now redirecting to an article about the attack. +This script redirects to an older post I made about how XSS works.

+ +

Conclusion

+ +

Watch out for sketchy websites that may be vulnerable to malicious or insecure sites which are ripe for abuse. +Always check that you are using an encrypted connection, HTTPS. +And if you see any messages warning you that a site is not secure and redirecting you to some random site… +Take their info with a grain of salt.

+ +

Happy Hacking, literally :)

+ + +
+ +
+
+ +
+ + diff --git a/_site/2021/04/18/uefi-development-environment/index.html b/_site/2021/04/18/uefi-development-environment/index.html new file mode 100644 index 0000000..0844f82 --- /dev/null +++ b/_site/2021/04/18/uefi-development-environment/index.html @@ -0,0 +1,183 @@ + + + + + UEFI Development On x86 With EDK2 | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

UEFI Development On x86 With EDK2

+ +
+
+ +

I made this blog so I could remember how to do stuff that had instructions spread around the internet. +So here is how I setup my environment for developing EFI applications.

+ +

Requirements

+ +

On Artix or other Arch-based distros like Manjaro I installed the following packages: gcc nasm iasl

+ +

Here is what the packages do:

+ +
    +
  • GCC is obviously the GNU Compiler Collection and it allows us to compile C code to machine code.
  • +
  • NASM stands for Netwide Assembler. It is an assembler and disassembler for 32 and 64 bit Intel x86 platforms.
  • +
  • IASL stands for the ACPI Source Language Compiler/Decompiler. This will compile any ACPI calls to our local machine’s code.
  • +
+ +

We need all these packages to start our (U)EFI journey. +Now that these are installed, let’s setup our environment.

+ +

Building EDK2

+ +

I used the stable/202011 branch as that is latest stable version of the EDK2 project.

+ +

So first let’s pull the project:

+ +

git clone https://github.com/tianocore/edk2.git

+ +

Now, let’s fetch the tags and switch to the latest stable version:

+ +
cd edk2
+git fetch
+git checkout stable/202011
+
+ +

Perfect! We’re on stable now! Let’s grab all our submodules: git submodule update --init

+ +

This will take a bit the first time you do it. But no fear, once that’s done, we can finally build the base tools.

+ +
make -C BaseTools
+export EDK_TOOLS_PATH=$HOME/Documents/edk2/BaseTools
+. edksetup.sh BaseTools
+
+ +

Notice we source a file with . before continuing. This is needed to load some tools and options into our shell for later. The environment variable EDK_TOOLS_PATH is set so that EDK knows where to find itself later. Now that everything is loaded up, we can modify a config file located at Conf/target.txt.

+ +

The most important options are these, feel free to append them to the end of the file; there is no default value for them.

+ +
+ACTIVE_PLATFORM = MdeModulePkg/MdeModulePkg.dsc
+TOOL_CHAIN_TAG = GCC5
+# for 64-bit development
+TARGET_ARCH = X64
+# for 32-bit development
+TARGET_ARCH = IA32
+# for 32 and 64-bit development
+TARGET_ARCH = IA32 X64
+
+# set multithreading to 1 + (2 x # of cores)
+MAX_CONCURRENT_THREAD_NUMBER = 9
+
+ +

There are other options, but I don’t know about them much, so I’m just sticking with this for now.

+ +

Finally, after all this work, we can build some .efi files. Let’s compile the Helloworld.efi file! +Simply run the build command in the terminal. +You can find your compiled EFI files by running this ls command:

+ +
ls Build/MdeModule/DEBUG_*/*/HelloWorld.efi
+
+ +

This will show all HelloWorld.efi files for all architectures and toolchains (if you decide to change them).

+ +

Running In UEFI Shell

+ +

Once all this is complete, you will want to run your EFI files. +To do so, let’s first add an EFI shell to use at boot. +This will appear as an option in your bootloader, like GRUB, which is what I will show documentation for in this article.

+ +

So, first thing is first, +download and EFI shell file. +Second, move it to a partition (FAT formatted) which can be used for the UEFI. +On my Linux system, this is /boot. On others there may be no FAT filesystem so attach a USB and format it as FAT. +Third, add the EFI Shell option to your grub boot file. +Substitute hdX with the right hard drive (I did it with trial and error) as when it doesn’t work I would hit ‘e’ on grub and add the ls GRUB command. +Substitute the gptX with the correct partition, or msdosX if it is a DOS formatted partition table. +My Shell.efi was placed in /boot/EFI/.

+ +

+
+menuentry "EFI Shell" {
+        insmod part_gpt
+        insmod chain
+        insmod fat
+        set root='(hd4,gpt2)'
+        chainloader /EFI/Shell.efi
+}
+
+ +

Now regenerate your grub configuration file with grub-update (Debian-based) or grub-mkconfig -o /boot/grub/grub.cfg (Other).

+ +

You’ll know if your shell is working if you see the following text on boot into the EFI shell:

+ +
+UEFI Interactive Shell v2.1
+EDK II
+UEFI v2.4 (EDI II, 0x000100000)
+Mapping table:
+	...
+Shell> 
+
+ +

Running Hello World

+ +

When we run our ls command from earlier, remember we saw our HelloWorld.efi file. +Let’s move this file somewhere useful, like for me, /boot. +Then, once we’re in our UEFI shell we can run commands:

+ +
+Shell> .\HelloWorld.efi
+UEFI Hello World!
+Shell> 
+
+ +

And that… All that is how you set up a UEFI development environment.

+ +

Conclusion

+ +

This took me a long time to figure out. +I needed to scrounge resources from around the internet, +and I had to look at my config files for hours to make sure that I hadn’t missed a step that I did without thinking. +I hope this will be useful to you and my future self.

+ +

Happy UEFI hacking :)

+ +
+ +
+
+ +
+ + diff --git a/_site/2021/06/02/pinebook-pro/index.html b/_site/2021/06/02/pinebook-pro/index.html new file mode 100644 index 0000000..68d408f --- /dev/null +++ b/_site/2021/06/02/pinebook-pro/index.html @@ -0,0 +1,191 @@ + + + + + Pinebook Pro, The Ultimate ARM Laptop | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

Pinebook Pro, The Ultimate ARM Laptop

+ +
+
+ +

I recently got my Pinebook Pro. +It was more expensive than I was expecting, coming in at (including shipping and handling) C$335. +I always forget the exchange rate and assume it’s similar to the U.S. dollar, but it never is, haha! +Anyway, this is just my first impressions and what I did to fix a few issues.

+ +

Initial Impressions

+ +

My first impressions of this are quite good. +I like the keyboard; it is firm and not mushy for the price. +It actually has a similar keyboard to my school-supplied Dell, which I quite enjoyed typing on. +The shell is aluminium and doesn’t feel too cheap, but I should note that it sure doesn’t feel like a Macbook if that’s what you’re expecting. +All in all build quality seems pretty good for a product in this price range. +I’m actually using it right now to write this article, and I’m actually typing faster than I would on my desktop.

+ +

The screen is bright enough and has anti-glare applied to it. I can use it with moderate light behind me, but not a sunset. Decent, and I can’t even use my phone with a sunset right on it, so that’s not a huge loss at all as I think my phone costs more than this haha!

+ +

The trackpad is fine. +I don’t use the mouse very often, and if I need it I’m more likely to bring an external one. +It works for what I need though. +I can’t seem to get the glossy protector off the trackpad though so maybe it would be better if I did haha!

+ +

The temperatures are okay. I would consider them not ideal. +The left side closer to the hinge can get quite warm when I push it. +To be expected in some respects, but the metal case certainly makes the heat come out fast and hot! +It is also passively cooled, so a bit of heat makes sense and is reasonable. +I wonder if I could mod this to have an active low-profile fan? +A project for later, I suppose.

+ +

The keyboard is pretty standard for a 14-inch laptop. +No numpad (except with function key), has F1-12 and media keys using function+F1-12. +Screen brightness, sound up, down and mute, and num and scroll lock. +These seem to work no matter what distribution you have (I’ve used Manjaro KDE and Manjaro Sway). +Perhaps this would react differently on Arch for ARM with no key bindings. +I’m not sure if this is implemented in software or hardware.

+ +

The speakers and very tin-y and do not sound good at all. +That said, they look very replaceable, so I’ll look into a mod in the future. +The Pinebook Pro comes with a headphone port, so you could just use that if the sound bothers you.

+ +

Some suggestions

+ +

I had some issues when it first arrived.

+ +
    +
  1. Reboot did not work. The display would glitch out and show horizontal lines. It would only work after a full shutdown.
  2. +
  3. Booting would sometimes not work at all. My SD card would sometimes boot, sometimes not. eMMC would sometimes work and sometimes not. Sometimes I would even get to the login screen, or fully logged in before it decided to freeze/hang. I could “drop to console” (Ctrl+Alt+Fx), but it only made my mouse stop showing, it would not actually display a console. This problem was worse when not plugged in.
  4. +
  5. Performance was not stellar, even for the RK3399.
  6. +
  7. I don’t like the Manjaro logo that displays during boot.
  8. +
+ +

Don’t use KDE

+ +

KDE for me is a bit slow. +It is not a keyboard-driven desktop. +To give it some credit though, it does at least have zoom support built in; this is something I wish other desktops would have enabled by default. +I’m looking at your, Xfce.

+ +

I switched to Manjaro Sway, which is a Wayland-based i3-like tiling window manager. +I’ve used this on my Raspberry Pi 4, and it is by far my preference among other default distro configurations.

+ +

This can be done by flashing an SD card with any random Linux distro, then download Manjaro Sway ARM for the Pinebook Pro.

+ +

Quickly, we should prepare the eMMC. Open fdisk with your eMMC module and remove all partitions. +If you have issues with this, check if any partition is mounted, unmount it, then try again. +fdisk is well documented elsewhere, so I won’t cover it here.

+ +

Once your .xz file is downloaded, unxz the .xz file downloaded.

+ +
+$ cd ~/Downloads
+$ unxz Manjaro-Sway-ARM-pbp-20.10.img.xz
+
+ +

Not exactly those commands, but close.

+ +

Once you have that, flash your eMMC by using dd.

+ +
+# dd if=./Manjaro-Sway-ARM-pbp-20.10.img of=/dev/mmcblkX bs=1M conv=fsync
+
+ +

Now remove your SD card. +U-Boot will prefer your SD card over your eMMC, so if you leave it in, it will boot to your SD card.

+ +

Flash Your U-Boot (BSP)

+ +

U-Boot appeared to be the solution to my other two issues. +I was able to flash a new U-Boot program by using the following commands. +Be sure to run lsblk beforehand to know which /dev/emmcblk to write to. +Replace X with the correct number for your system.

+ +
+# pacman -S uboot-pinebookpro-bsp
+# dd if=/boot/idbloader.img of=/dev/mmcblkX seek=64 conv=notrunc
+# dd if=/boot/uboot.img of=/dev/mmcblkX seek=16384 conv=Notrunc
+# dd if=/boot/trust.img of=/dev/mmcblkX seek=24576 conv=notrunc
+
+ +

The dd instructions are printed out after installing the uboot-pinebookpro-bsp package, so make sure to follow what is printed there if it is different that what I have provided.

+ +

After doing this, not only have I since booted 100% of the time, +but my display now works correctly after a reboot without a full shutdown.

+ +

Whew! Looking good!!!

+ +

Maybe get some of the accessories

+ +

I didn’t buy any accessories from Pine64. +I regret this somewhat. +For one thing, without an accessory to read the eMMC over USB, you need to have a working Linux distro on the SD card to get anywhere with it. +Flashing directly to the eMMC would have saved me a lot of time.

+ +

The other accessory I could see the occasional use for is the Ethernet adapter. +When downloading a big update (1GB+), it could be useful to wire in just temporarily. +Not a huge deal, but worth mentioning.

+ +

I would also be interested in the other batteries they have available. +Even though it comes with a battery, and I also don’t think you can install a second one, I would be interested to see if I could get more life out of it with an improved battery. +If this is a standard battery (Pine64 tends to use standard parts), then I would consider getting it from a supplier as well.

+ +

The Pinebook Pro does not come with any HDMI ports. +It comes with a USB type-C port that can be adapted to HDMI. +Or you can get a display that supports USB type-C. +I do not have a display that supports USB type-C, so it might be worth it for me to buy an adapter or find a compatible one more locally. +Shipping from Hong Kong ain’t cheap.

+ + + +

The boot splash screen can be replaced, but I haven’t figured out how yet. +I will post an update to the blog when I do find out.

+ +

Conclusion

+ +

I really want to use the Pinebook Pro more. +Pine64 do a lot for the open-source community and they do their best to use only open hardware. +They do fail in some respects, but they do much better than the mainline distributors like Dell, HP or ASUS.

+ +

Thanks, Pine64! I’m excited to use your products!

+ +

Happy ARM hacking :)

+ +
+ +
+
+ +
+ + diff --git a/_site/2021/06/21/uefi-audio/index.html b/_site/2021/06/21/uefi-audio/index.html new file mode 100644 index 0000000..10b23f9 --- /dev/null +++ b/_site/2021/06/21/uefi-audio/index.html @@ -0,0 +1,105 @@ + + + + + UEFI Audio Protocol & UEFI BIOS Accessibility | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

UEFI Audio Protocol & UEFI BIOS Accessibility

+ +
+
+ +

Good news about the state of accessibility in the BIOS!

+ +

Preamble

+ +

On my ideas page, I have always had up the idea of an accessibility layer for blind people to be able to use their BIOS. +Although it targets a very small percentage of the population, +computer programming is often at least a hobby of visually imapired individuals as it is (mostly) a text-based job: +You write code in plain text, then run it to get either plain text or some kind of HTML output. +Mostly an accessible career for those who cannot see. +That said, there has always been an issue with low-level computer infrastructure (i.e. the BIOS/UEFI). +These menus—which let you edit your boot order, RAM timings, CPU and GPU overclocking and sometimes even fan speed—they were completely inaccessible to those who could not see them. +Well, until… soon. I had a talk with one of the big bois working on EDK2, the UEFI implementation which is used by most motherboard vendors to create their firmware. +I thought I would share the info I understand, and the conversation in full.

+ +

News

+ +

Here is what I know:

+ +
    +
  1. This year, the GSoC (Google Summer of Code) project had a submission of Ethin Probst to implement VirtIO audio drivers for EDK2.
  2. +
  3. QEMU, the emulator that was chosen to test for this project does not have VirtIO support (yet). I haven’t found info on when this will be done.
  4. +
  5. Because of 2, Ethin and his mentors for his project, Ray Ni and Leif Lindholm decided to first implement USB-dongle audio support first, as this is a) supported in QEMU, and b) is good enough to start squashing bugs at the audio level.
  6. +
  7. Because GSoC is usually over around September, there will likely be some more news coming soon!
  8. +
+ +

The IRC Chat

+ +

Here is the log of the IRC chat for anyone who is interested in anything I might have missed:

+ +
+tait_dot_tech: Hello there, I'm new to IRC so just checking my messages are coming through.
+tait_dot_tech: Looks light it's alright. Ok so I have a question: does anyone know of an active project looking at making UEFI accessible to the blind (i.e. speec) [sic] from within the UEFI environment? Main concern is having blind users be able to boot Linux USBs (I know, very niche thing), but depending on how good it is, could potentially be used to allow blind individuals to change their overclocking,
+tait_dot_tech: hardware RAID, boot order, RAM timings, etc. all on their own. Just wondering if there is any project doing this? I have tried my best to find anything, and am just trying not to duplicate effort. Thanks :)
+leiflindholm: tait_dot_tech: we have a google summer of code project running this year, prototyping a standard for audio output. To hopefully be added to the UEFI specification in the future.
+leiflindholm: once we have a standard for audio output, we can work on adding support for audio output to the Human Interface Infrastructure
+leiflindholm: which is the thing that lets menus be loaded and displayed independent of specific graphical implementation
+tait_dot_tech: Oh wow! Glad to hear there is progress on this! Is there a link to the Google summer of code project, or anything else where I can keep tabs?
+leiflindholm: tait_dot_tech: there isn't much yet, we're only on week 3 of GSoC.
+leiflindholm: https://summerofcode.withgoogle.com/projects/#6499615798460416 is the link if it's something you want to point others to, but any discussion/reporting is likely to hapen [sic] on our mailing lists
+tait_dot_tech: By "our" mailing list, do you mean GSoC, or Edk2?
+leiflindholm: edk2
+leiflindholm: although, on average, at least 99% of edk2-devel will *not* be about audio support
+leiflindholm: When we have anything interesting to say, we'll also post to edk2-discuss/edk2-announce
+tait_dot_tech: Sweet! I'll join that one just in case! I'd be happy to test anything in beta-ish state and report back with any device I can get my hands on. Is that the right list to watch for hepling test it out?
+leiflindholm: I'd say so.
+leiflindholm: The original plan was to start with wirtio [sic] audio support, so anyone could help out anywhere, but that support is not yet upstream in qemu. So for now we're working on an [sic] USB audio class driver. That will certainly be useful to have more people testing with different equipment once we have something working.
+tait_dot_tech: Ahh! So if I want to test, I should get a USB audio dongle. Gotcha! Thank you so much! You've been super helpful!
+leiflindholm: np :)
+
+ +

Things are (slowly) looking up for audio (and eventually screen-reader support) in UEFI! +Phew! Glad I’m not the only one thinking about this!

+ +

Happy UEFI hacking :)

+ +
+ +
+
+ +
+ + diff --git a/_site/2021/08/31/vpns-api/index.html b/_site/2021/08/31/vpns-api/index.html new file mode 100644 index 0000000..998aa89 --- /dev/null +++ b/_site/2021/08/31/vpns-api/index.html @@ -0,0 +1,235 @@ + + + + + Idea For A VPN Service | tait.tech + + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

Idea For A VPN Service

+ +
+
+ +

Recently I’ve been thinking about starting a VPN service. +This service has some interesting requirements that I have never seen a VPN service do before, so I’d like to put down my thoughts as to what might be sensible for a centralized yet encrypted* VPN service.

+ +

I would license all the code and scripts under the AGPLv3. +This creates an environment where I could allow my company to use this code, and any other company for that matter. However, no company would be allowed to take it into their own hands and use it without contributing back to the project.

+ +

E2EE VPN

+ +

I want this service in many ways to be on par with ProtonMail: +end-to-end encrypted (E2EE), and with a focus in data security for the user of the service.

+ +

Full encryption, so that even me, the writer and the deployer of the service, cannot view any information about the user: this is the utmost security. +The bad news is that this is very hard to do in a convenient way. +I’ve decided for now that the best thing to do is to target the Linux nerd. +Target the user who is familiar with these advanced security practices, then make them available to the general public as the layers on top of the robust security are refined.

+ +

Why?

+ +

End-to-end encryption is necessary in a country like Canada, where I may be sent a subpoena to provide customer data. +This is the case especially in the Five Eyes anglophone group of countries, who essentially spy on each others’ citizens for eachother. +In essence, any data in the hand of one government agency in the “Eyes Countries” may be shared between the Five, Nine, and 14 Eyes countries.

+ +

I am not against government surveillance in principle. +In theory, the government should be finding bad guys: pedophiles, sex trafficking rings and drug cartels. +In practice, the U.S. government especially, uses its authority to spy on its own citizens who are simply minding their own business. Bulk data collection mass surveillance is not a freedom respecting characteristic of modern western democracies. +I do run the risk of not being able to help much in the case of a genuine warrant against a genuine, evil criminal. +That is the risk of privacy.

+ +

That said, let’s see what can be built that can do these 2 things:

+
    +
  1. Maximize privacy for the user.
  2. +
  3. Allow for (optional) monetization, depending on the provider. This is in some contradiction to premise 1.
  4. +
+ +

What We Need

+ +

A VPN service needs access to some basic information:

+
    +
  1. Service discontinue time (the amount of time until the customer must renew).
  2. +
  3. Active connections (a number which can not be exceeded by an individual user).
  4. +
+ +

The client needs access to some information from the server as well:

+
    +
  1. A list of VPNs able to be connected to (with filters).
  2. +
  3. For every VPN: +
      +
    1. IP Address.
    2. +
    3. Maximum bandwidth.
    4. +
    5. Number of connected users or connection saturation percentage.
    6. +
    7. Supported protocols.
    8. +
    +
  4. +
+ +

Can we do this in a end-to-end encrypted fashion? +I’m honestly not sure. But here are my ideas so far as to how some of these functions might work.

+ +

How To Do It

+ +

“Usernames”

+ +

There will be one button to create your account: “Generate username” +The username, or unique identifier for a user will be generated for them by a random generator. +I plan to generate a username from a list of Base 64 characters; it will be a guaranteed length of 16. +This gives a total of: 79228162514264337593543950336 or 7.9×10287.9 \times 10^{28} posibilities. +This is sufficient for a username.

+ +

The other option is to use a standard “username” field that uses a modern hash function like SHA512 to store it in the database. +This is less secure as it is vulnerable to a brute-force attack of finding users, +but this is also a very easy attack to defend against, i.e. IP banning after 10-ish tries of not finding a username.

+ +

A non-unique, universal salt will also be used on each username before storing it in the database to make it more secure. +This decreases the possibility of an advanced attacker being able to find usernames in a leaked database using rainbow tables. +That said, the fact that it is a fixed salt makes it much more vulnerable to an attack. +Although it would be known only by the server machine, it would still be somewhat of a vulnerability. +The operator may also store the salt in an encrypted password store of their own in case the server is erased, broken into, etc. +It would be fairly easy, if they have access to the active salt, to migrate to a new salt every few days/months, or perhaps every time a server upgrade/maintenance happens. +This does run the possibility of larger issues if the server is shut down or hangs during a migration and needs to be restarted. +Many users may end up with accounts they cannot access without manual cleanup.

+ +

In the end, the application would need a backup of this salt, otherwise login times would become linear to the number of users as the database checks every user’s salt to see if it matches the hash made with the username input. +Note that the database does not store the salt, so finding it will be very hard, even in the case of a leaked database.

+ +

So, here’s the overview: +The username will be generated, then stored after being salted and hashed. +The salt will be a fixed or rolling salt across all usernames to avoid linear scaling of searching for a user. +The server will only see the username once, when sending it to the user for them to save for the first time; +there will be no database entry with the original username in it.

+ +

This does mean that if the username is lost, the account is lost too. There is no way to recover the account. +Again, this is ok for now, as my target audience is advanced Linux and privacy enthusiasts.

+ +

“Passwords”

+ +

There are a few options for passwords/secret keys.

+ +

I think the best is to treat it similarly to the username is above, except it will not be generated for you. +When a new account is generated, you will be taken to a password reset screen where you will set your password to whatever your want, using your own secure system to handle it. +This is ideal for Linux and tech enthusiasts as they generally already have a password management system setup.

+ +

This will also be salted, with its own unique salt, then hashed and stored alongside the username.

+ +

Active Time Remaining

+ +

It is easy and ideal to have a field connected to a user with their expiry date for their account. +When a payment is made, this date will be increased by the number of days, hours and minutes proportional to the payment received.

+ +

For example: if a “month” (30 days) costs ten dollars, then a payment of fifteen dollars would add 45 days to an account. So essentially 33 cents per day, 1030×24=0.0138 \frac{10}{30 \times 24}=0.0138 dollars per hour, or 1030×24×60=0.00023148\frac{10}{30 \times 24 \times 60}=0.00023\overline{148} dollars per minute. +This is the second biggest threat to the users’ data privacy, as this, by definition, cannot be encrypted as my server needs access to this data to decide whether a user should be allowed to: view a list of VPN nodes available to them or connect to a VPN. +The best I can think of in this case is:

+
    +
  1. Use a system similar to the username: use a common salt and hash algorithm to store them in the database.
  2. +
  3. Use full-disk and full-database encryption to keep the data secure to outside attackers.
  4. +
+ +

This is not a fantastic solution, and still has the threat of a service provider snooping in on the database. +The truth is: a service provider has root access to any machine it hosts. +This necessitates that the physical infrastructure hosting the central database server must by physically owned and operated by the VPN operator and not any third party. +In addition, it means top security root passwords, tamper resistant cases (in the case of a co-hosting or server room environment), sensors to indicate it has been opened or touched. +If you thought this was bad, wait until the next part.

+ +

Active Connections

+ +

In order to stop a user from simply using the entire bandwidth of all the VPN nodes available to them, there must be a way to know how many active connections the user has. +This is by far the biggest issue in terms of user privacy. +There are a few options here:

+
    +
  1. Do not have a limit on the number of connections a user may have. This is dangerous from a DDoS (distributed denial-of-service) perspective. +This also makes the VPN provider vulnerable to be used as a DDoS distribution method by putting all their traffic through the VPN provider, and them not having any logs—the bad guys could use the distributed nature of VPN nodes to attack whoever they see fit. +This is not a viable option.
  2. +
  3. Have a list of connected users sent to the central server every 15 to 30 seconds. This is fairly efficient, but more privacy invasive.
  4. +
  5. When a user connects, log an explicit “connect” message. +When a user disconnects, send an explicit “disconnect”. +Have the VPN server report an implicit “disconnect” after an amount of time, say 15 minutes, then send an implicit “connect” message once traffic continues. This is all in RAM under temporary storage and is lost upon restart of the server.
  6. +
+ +

The best method (used currently by Mullvad VPN) is number 3.

+ +

Panel

+ +

The admin panel will have some broad info about the nodes:

+ +
    +
  • Active connections
  • +
  • Server load (held and reported every minute by the nodes themselves. Not sure how to do this yet.)
  • +
  • Location
  • +
  • IP Address
  • +
  • Failed connections in last X amount of time (i.e. invalid credentials)
  • +
  • Physical server status (i.e. owned by the hoster vs. contracted out to another hosting company in the area)
  • +
+ +

This panel would also have options to stop, start or soft stop the VPN service on each node for maintenance. +A soft stop will stop new connections and remove it from the list of available servers for the end-user. Users will disconnect whenever they feel like it—eventually winding down to zero connections. +This allows maintenance without service disruption.

+ +

I’m not sure how to do this securely. +Best I can think of right now is have an admin login, then have the server have a key in each node machine. +This completely compromises the SSH key system though. +Now every node is secured with nothing but a password. Maybe the console will require connecting to a local instance on a machine through an encrypted connection which will require a key. +Even then, that does make every machine vulnerable to one point of failure (the key to connect to the local instance).

+ +

Another way to approach this, security-wise is to make a shell script (or locally running flask app) which reads info about the servers from a sqlite database. +Then, it uses the local computer to connect to the servers—assuming the local machine has all the keys necessary to do so.

+ +

This fixes one problem and creates another. +It fixes the single point of failure in the cloud. This massively reduces the attack surface to intentionally stealing physical hardware from trusted parties, or software-hacking these same trusted people. +But, if the key is lost by the host… The entire service is kaput. No maintenance may be performed, no checks, bans, addition of servers can be done whatsoever. +This also increases the possibility of sloppy security from trusted parties. +Perhaps a trusted member leaves his laptop unattended for a few minutes and a hacker is able to steal the simple key file. He’s in!!! +This is very unlikely, I must say, but it comes down to: should I trust people or machines more to keep the data secure. +Depending on the person, I might trust them more.

+ +

Conclusion

+ +

With all of these ideas in mind, I have realized how difficult it really is to make a VPN service. +Boy do they deserve every dollar they get! +If you don’t have a VPN, get one. +Doesn’t really matter which one, unless you’re a nerd—for your average person you can just pick whatever the best deal is at the time and you’re off to the races.

+ +

Anyway, I think I’ve rambled on long enough about VPNs and my crazy ideas, so I’m going to leave this one for now.

+ +

Happy VPN hacking :D

+ +
+ +
+
+ +
+ + diff --git a/_site/2021/09/18/how-to-generate-proper-content-mathml-from-katex-or-latex/index.html b/_site/2021/09/18/how-to-generate-proper-content-mathml-from-katex-or-latex/index.html new file mode 100644 index 0000000..442687d --- /dev/null +++ b/_site/2021/09/18/how-to-generate-proper-content-mathml-from-katex-or-latex/index.html @@ -0,0 +1,335 @@ + + + + + How To Produce Semantically Correct MathML From XaTeX/LaTeX (and other accessibility ideas) | tait.tech + + + + + + + + +
+
+

tait.tech

+ + + +
+
+
+
+

How To Produce Semantically Correct MathML From XaTeX/LaTeX (and other accessibility ideas)

+ +
+
+ +

During a recent run-in with the Simon Fraser Fraser University accessibility department, +I learned that they’re 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. +Here’s a quick guide!

+ +

Diagrams

+ +

Most unexplained diagrams I saw were ones which mirrored classic computer-science imagery; +these diagrams, for the most part, were not complex nor exotic; +they are straight-forward to explain in writing, +or easy to turn into a table. +I’ll show two examples here, +one will show a visual aide in relation to stacks and queues, +and the other will show a memory representation of a stack. +Both of these were explained as “image” to the student.

+ +

Stacks

+ +

Diagram 1:

+ +
+ image...lol! Just kidding, will explain it below w/ table +
Simple diagram explaining the push/pop process. Source: Stackoverflow
+
+ +

Ok, so here we have a diagram showing the pushing and popping process of a stack. +Now, “image” is hardly sufficient to explain this, so let’s try it with text. +I won’t finish it because it gets unwieldy very fast:

+ +
+

A diagram showing a stack. It starts with the operation “Push A”, and now the stack contains the variable “A”; now the stack pushes “B”, which displays now “B” on top of “A”…

+
+ +

This is no solution. +It is hard to explain this correctly and accurately without being extremely verbose and frankly, confusing—this defeats the whole purpose of describing the image. +The good news, is that computer science diagrams especially tend to lean towards being tabular data. +Now to be clear, something does not need to look like a table to be tabular data; +this image happens to look almost like a table if you squinted hard enough, +but many data not written down in a table, are still “tabular data”. +I will show an example of that next! +For now though, here is the same idea, same data without words:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
OperatorStack Values
Push A[A]
Push B[B, A]
Push C[C, B, A]
Push D[D, C, B, A]
Pop D[C, B, A]
+ +

Now this diagram does imply you can pop other items, like “Pop A”, which is just not true. +But that’s the fault of the diagram, not the representation of it.

+ +

Here is the raw text equivalent (in Markdown):

+ +
+Operator|Stack Values
+---|---
+Push A|[A]
+Push B|{B, A]
+Push C|[C, B, A]
+Push D|[D, C, B, A]
+Pop (D)|[C, B, A]
+
+ +

Stacks in Memory

+ +

So I couldn’t find a good non-copyright image of a stack in memory, but I’ll write it down here in plain text, and you should get the idea. +Now again, remember this is still labeled “image” to the student, +they do not have access to a text version of this.

+ +
+( ) ( ( ( ) ) ) ( ) ( ( ) ( ( )
+1 0 1 2 3 2 1 0 1 0 1 2 1 2 3 2
+
+ +

Now, someone who looks at this can probably see that the number goes up for a left parenthesis, and down for a right parenthesis. +“Image”, however, does not handle the detail. +The issue here is a transcriber is likely to want to transcribe this as text. +But it’s really not. +This is again, tabular data, which is best represented in a table.

+ +

Table of this:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CharacterCounter
(1
)0
(1
(2
(3
)2
)1
)0
(1
)0
(1
(2
)1
(2
(3
)2
+ +

Raw text in markdown:

+ +
+Character|Counter
+---|---
+(|1
+)|0
+(|1
+(|2
+(|3
+)|2
+)|1
+)|0
+(|1
+)|0
+(|1
+(|2
+)|1
+(|2
+(|3
+)|2
+
+ +

Insanely simple! +Look for clues of tabular data. +Things which have a one to one correspondence of any kind can usually be represented as a table, even if it’s only “aligned” on the slide or note.

+ +

Math Expressions & MathML

+ +

Here is a more complex example: +using math within a presentation.

+ +

Let’s take for example the mathematical expression 16=2416 = 2^{4}. This is a very simple math expression that completely breaks in some cases. +When converting some math expressions to text, it will convert that expression as 16=2416 = 24, erasing the superscript to denote the exponent.

+ +

This gets even worse with large mathematical expressions like this:

+ +

B2U(X)=i=0w1xi×2i +\text{B2U}(X) = \sum_{i=0}^{w-1} x_{i} \times 2^{i} +

+ +

Here is what I get by extracting the text from the PDF:

+ +
+B2U(X ) =
+
+w-1
+
+Σ xi •2
+i=0
+
+i
+
+ +

And this is generous, as the sigma sign, bullet point, equal sign and minus sign were for some reason not UTF-8 encoded so it displayed as a chat sign emoji, down arrow, video camera and book sign respectively. +Not sure about you, but I certainly can’t get the equation out of that mess.

+ +

These can be written in LaTeX, then converted to MathML (an accessible math format) using KaTeX. +Here’s an example of what to write to product the function above:

+ +
+\text{B2U}(X) = \sum_{i=0}^{w-1} x_{i} \times 2^{i}
+
+ +

For someone who is doing transcription as a job for visually impaired students, +I would go so far as to say to learn this is a necessity.

+ +
    +
  1. It’s not difficult. You can learn the vast majority of LaTeX math syntax in an afternoon.
  2. +
  3. It’s easier for everyone to read. Especially with KaTeX. KaTeX is able to convert the formula to both MathML for screenreader users and HTML markup for people who just want to see those fancy math expressions.
  4. +
+ +

Likely, the teacher is already using some LaTeX derivative to create the math in the first place, +they might as well use a program like KaTeX, MathJax or likewise to convert it to MathML.

+ +

Code & Output

+ +

How did it even happen that entire programs and outputs were just ignored with the label “picture of output” is beyond me. +Everything should be transcribed. +Whoever transcribed that document should be fired.

+ +

Conclusion

+ +

To teachers:

+ +

Presenting information in plain text, or at least having alternates forms of images, diagrams and math formulas makes education better for everyone, not just blind students. +It makes it better for people running on cheaper devices which may not handle running heavy software like Microsoft PowerPoint; +it makes it better for people who use operating systems other than MacOS and Windows (this is especially important in the technology sector, where Linux/BSD users make up a sizeable minority of users); +and finally, it makes it easier to search through the content of all notes at once using simple text-manipulation tools.

+ +

To accessibility departments:

+ +

Running a pdftotext program, or simply transcribing handwritten notes is not enough to properly describe slides and notes—handwritten or not. +Every diagram, math equation, annotation, piece of code or output—every single thing must be transcribed to plain text, or some alternate format like MathML.

+ +

I find it sad that a student (with their own full-time job) can product better work than someone who has this job exclusively at a major university. +Perhaps I am mistaken and the university has volunteers do this work. +In that case I guess you can’t ask for too much, but somehow I feel like this is probably not the case.

+ +

Big sad.

+ +
+ +
+
+ +
+ + diff --git a/_site/LICENSE b/_site/LICENSE new file mode 100644 index 0000000..c9c5eb9 --- /dev/null +++ b/_site/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2020, Tait Hoyem +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/_site/README.md b/_site/README.md new file mode 100644 index 0000000..111c6c2 --- /dev/null +++ b/_site/README.md @@ -0,0 +1,23 @@ +# tait.tech +The uncompiled version of my website. Compiled files in _site + +### To compile + +To compile the files, simply use the bundle Ruby gem. + +``` +bundle exec jekyll build +``` + +to compile once. If you decide you want to actually have changes appear instantly, use the following + +``` +bundle exec jekyll watch +``` + +To run a local web server to view the changes on: +``` +bundle exec jekyll server -w +``` + +This will watch for the latest changes, compile them, and make them available on localhost:4000 diff --git a/_site/about/index.html b/_site/about/index.html new file mode 100644 index 0000000..7d0e9af --- /dev/null +++ b/_site/about/index.html @@ -0,0 +1,45 @@ + + + + + About | tait.tech + + + + + + + +
+
+

tait.tech

+ + + +
+
+

Tait Hoyem

+ +

I believe in three fundamentals

+ +
+
+ +
+ + diff --git a/_site/assets/css/book.css b/_site/assets/css/book.css new file mode 100644 index 0000000..3beec10 --- /dev/null +++ b/_site/assets/css/book.css @@ -0,0 +1,9 @@ +body { background-color: #222222; color: #ffffff; font-family: -apple-system, helvetica, arial, sans-serif; line-height: 1.5em; padding: .25em; } + +a { color: #7ad; } + +a:visited { color: #ff3492; } + +h1, h2 { text-align: center; } + +/*# sourceMappingURL=book.css.map */ \ No newline at end of file diff --git a/_site/assets/css/book.css.map b/_site/assets/css/book.css.map new file mode 100644 index 0000000..ebd1a20 --- /dev/null +++ b/_site/assets/css/book.css.map @@ -0,0 +1,16 @@ +{ + "version": 3, + "file": "book.css", + "sources": [ + "book.scss", + "_sass/book.scss", + "_sass/vars.scss" + ], + "sourcesContent": [ + "@import \"book\";\n", + "@import \"vars\";\n\nbody {\n background-color: $background-color;\n color: $normal-text-color;\n font-family: -apple-system, helvetica, arial, sans-serif;\n line-height: $line-height;\n padding: .25em;\n}\na {\n color: $link-color;\n}\na:visited {\n color: $visited-link-color;\n}\nh1, h2 {\n text-align: center;\n}\n", + "$normal-text-color: #ffffff;\n$background-color: #222222;\n$lighter-color: #ffffff;\n$nav-link-color: #ffffff;\n$nav-link-hover-color: black;\n$link-color: #7ad;\n$visited-link-color: #ff3492;\n$last-p-padd: 1.5em;\n$nav-padd: 1em;\n$line-under: 1px solid #aaa; \n$line-height: 1.5em;\n$term-line-height: 1em;\n$file-line-height: 1.2em;\n$terminal-bg: #000000;\n$terminal-fg: #00FF00;\n$file-bg: #444444;\n$file-fg: #ffffff;\n" + ], + "names": [], + "mappings": "ACEA,AAAA,IAAI,CAAC,EACH,gBAAgB,ECFC,OAAO,EDGxB,KAAK,ECJa,OAAO,EDKzB,WAAW,EAAE,2CAA2C,EACxD,WAAW,ECIC,KAAK,EDHjB,OAAO,EAAE,KAAK,GACf;;AACD,AAAA,CAAC,CAAC,EACA,KAAK,ECLM,IAAI,GDMhB;;AACD,AAAA,CAAC,CAAC,OAAO,CAAC,EACR,KAAK,ECPc,OAAO,GDQ3B;;AACD,AAAA,EAAE,EAAE,EAAE,CAAC,EACL,UAAU,EAAE,MAAM,GACnB" +} \ No newline at end of file diff --git a/_site/assets/css/fonts/KaTeX_AMS-Regular.ttf b/_site/assets/css/fonts/KaTeX_AMS-Regular.ttf new file mode 100644 index 0000000..31b8d8d Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_AMS-Regular.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_AMS-Regular.woff b/_site/assets/css/fonts/KaTeX_AMS-Regular.woff new file mode 100644 index 0000000..13000fc Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_AMS-Regular.woff differ diff --git a/_site/assets/css/fonts/KaTeX_AMS-Regular.woff2 b/_site/assets/css/fonts/KaTeX_AMS-Regular.woff2 new file mode 100644 index 0000000..378b798 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_AMS-Regular.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Caligraphic-Bold.ttf b/_site/assets/css/fonts/KaTeX_Caligraphic-Bold.ttf new file mode 100644 index 0000000..b3e756c Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Caligraphic-Bold.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Caligraphic-Bold.woff b/_site/assets/css/fonts/KaTeX_Caligraphic-Bold.woff new file mode 100644 index 0000000..cf82f36 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Caligraphic-Bold.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Caligraphic-Bold.woff2 b/_site/assets/css/fonts/KaTeX_Caligraphic-Bold.woff2 new file mode 100644 index 0000000..6e9d50d Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Caligraphic-Bold.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Caligraphic-Regular.ttf b/_site/assets/css/fonts/KaTeX_Caligraphic-Regular.ttf new file mode 100644 index 0000000..a8cdd0e Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Caligraphic-Regular.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Caligraphic-Regular.woff b/_site/assets/css/fonts/KaTeX_Caligraphic-Regular.woff new file mode 100644 index 0000000..24f3b7b Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Caligraphic-Regular.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Caligraphic-Regular.woff2 b/_site/assets/css/fonts/KaTeX_Caligraphic-Regular.woff2 new file mode 100644 index 0000000..0bcce6f Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Caligraphic-Regular.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Fraktur-Bold.ttf b/_site/assets/css/fonts/KaTeX_Fraktur-Bold.ttf new file mode 100644 index 0000000..57cef5c Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Fraktur-Bold.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Fraktur-Bold.woff b/_site/assets/css/fonts/KaTeX_Fraktur-Bold.woff new file mode 100644 index 0000000..56aeb69 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Fraktur-Bold.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Fraktur-Bold.woff2 b/_site/assets/css/fonts/KaTeX_Fraktur-Bold.woff2 new file mode 100644 index 0000000..e4ad521 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Fraktur-Bold.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Fraktur-Regular.ttf b/_site/assets/css/fonts/KaTeX_Fraktur-Regular.ttf new file mode 100644 index 0000000..1793994 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Fraktur-Regular.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Fraktur-Regular.woff b/_site/assets/css/fonts/KaTeX_Fraktur-Regular.woff new file mode 100644 index 0000000..2e15d01 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Fraktur-Regular.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Fraktur-Regular.woff2 b/_site/assets/css/fonts/KaTeX_Fraktur-Regular.woff2 new file mode 100644 index 0000000..f481b14 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Fraktur-Regular.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Main-Bold.ttf b/_site/assets/css/fonts/KaTeX_Main-Bold.ttf new file mode 100644 index 0000000..e657894 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Main-Bold.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Main-Bold.woff b/_site/assets/css/fonts/KaTeX_Main-Bold.woff new file mode 100644 index 0000000..495fc43 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Main-Bold.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Main-Bold.woff2 b/_site/assets/css/fonts/KaTeX_Main-Bold.woff2 new file mode 100644 index 0000000..cdb9ecc Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Main-Bold.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Main-BoldItalic.ttf b/_site/assets/css/fonts/KaTeX_Main-BoldItalic.ttf new file mode 100644 index 0000000..c11cde7 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Main-BoldItalic.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Main-BoldItalic.woff b/_site/assets/css/fonts/KaTeX_Main-BoldItalic.woff new file mode 100644 index 0000000..121e242 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Main-BoldItalic.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Main-BoldItalic.woff2 b/_site/assets/css/fonts/KaTeX_Main-BoldItalic.woff2 new file mode 100644 index 0000000..42171ec Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Main-BoldItalic.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Main-Italic.ttf b/_site/assets/css/fonts/KaTeX_Main-Italic.ttf new file mode 100644 index 0000000..2f270de Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Main-Italic.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Main-Italic.woff b/_site/assets/css/fonts/KaTeX_Main-Italic.woff new file mode 100644 index 0000000..c669377 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Main-Italic.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Main-Italic.woff2 b/_site/assets/css/fonts/KaTeX_Main-Italic.woff2 new file mode 100644 index 0000000..e89824d Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Main-Italic.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Main-Regular.ttf b/_site/assets/css/fonts/KaTeX_Main-Regular.ttf new file mode 100644 index 0000000..741db9c Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Main-Regular.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Main-Regular.woff b/_site/assets/css/fonts/KaTeX_Main-Regular.woff new file mode 100644 index 0000000..4c8de9e Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Main-Regular.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Main-Regular.woff2 b/_site/assets/css/fonts/KaTeX_Main-Regular.woff2 new file mode 100644 index 0000000..2aa480a Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Main-Regular.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Math-BoldItalic.ttf b/_site/assets/css/fonts/KaTeX_Math-BoldItalic.ttf new file mode 100644 index 0000000..c3a1c3e Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Math-BoldItalic.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Math-BoldItalic.woff b/_site/assets/css/fonts/KaTeX_Math-BoldItalic.woff new file mode 100644 index 0000000..2c47198 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Math-BoldItalic.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Math-BoldItalic.woff2 b/_site/assets/css/fonts/KaTeX_Math-BoldItalic.woff2 new file mode 100644 index 0000000..82f609f Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Math-BoldItalic.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Math-Italic.ttf b/_site/assets/css/fonts/KaTeX_Math-Italic.ttf new file mode 100644 index 0000000..b58dc88 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Math-Italic.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Math-Italic.woff b/_site/assets/css/fonts/KaTeX_Math-Italic.woff new file mode 100644 index 0000000..3ee35dc Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Math-Italic.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Math-Italic.woff2 b/_site/assets/css/fonts/KaTeX_Math-Italic.woff2 new file mode 100644 index 0000000..a2f3617 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Math-Italic.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_SansSerif-Bold.ttf b/_site/assets/css/fonts/KaTeX_SansSerif-Bold.ttf new file mode 100644 index 0000000..68d11ee Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_SansSerif-Bold.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_SansSerif-Bold.woff b/_site/assets/css/fonts/KaTeX_SansSerif-Bold.woff new file mode 100644 index 0000000..cd6dbb1 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_SansSerif-Bold.woff differ diff --git a/_site/assets/css/fonts/KaTeX_SansSerif-Bold.woff2 b/_site/assets/css/fonts/KaTeX_SansSerif-Bold.woff2 new file mode 100644 index 0000000..c2b93c8 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_SansSerif-Bold.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_SansSerif-Italic.ttf b/_site/assets/css/fonts/KaTeX_SansSerif-Italic.ttf new file mode 100644 index 0000000..2ea5de4 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_SansSerif-Italic.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_SansSerif-Italic.woff b/_site/assets/css/fonts/KaTeX_SansSerif-Italic.woff new file mode 100644 index 0000000..d022507 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_SansSerif-Italic.woff differ diff --git a/_site/assets/css/fonts/KaTeX_SansSerif-Italic.woff2 b/_site/assets/css/fonts/KaTeX_SansSerif-Italic.woff2 new file mode 100644 index 0000000..e890b37 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_SansSerif-Italic.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_SansSerif-Regular.ttf b/_site/assets/css/fonts/KaTeX_SansSerif-Regular.ttf new file mode 100644 index 0000000..c2066ca Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_SansSerif-Regular.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_SansSerif-Regular.woff b/_site/assets/css/fonts/KaTeX_SansSerif-Regular.woff new file mode 100644 index 0000000..e43b4a2 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_SansSerif-Regular.woff differ diff --git a/_site/assets/css/fonts/KaTeX_SansSerif-Regular.woff2 b/_site/assets/css/fonts/KaTeX_SansSerif-Regular.woff2 new file mode 100644 index 0000000..51037b4 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_SansSerif-Regular.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Script-Regular.ttf b/_site/assets/css/fonts/KaTeX_Script-Regular.ttf new file mode 100644 index 0000000..1753e88 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Script-Regular.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Script-Regular.woff b/_site/assets/css/fonts/KaTeX_Script-Regular.woff new file mode 100644 index 0000000..2f8b979 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Script-Regular.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Script-Regular.woff2 b/_site/assets/css/fonts/KaTeX_Script-Regular.woff2 new file mode 100644 index 0000000..e84ca23 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Script-Regular.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Size1-Regular.ttf b/_site/assets/css/fonts/KaTeX_Size1-Regular.ttf new file mode 100644 index 0000000..31f438b Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Size1-Regular.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Size1-Regular.woff b/_site/assets/css/fonts/KaTeX_Size1-Regular.woff new file mode 100644 index 0000000..b0a7bb2 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Size1-Regular.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Size1-Regular.woff2 b/_site/assets/css/fonts/KaTeX_Size1-Regular.woff2 new file mode 100644 index 0000000..f10ebd2 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Size1-Regular.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Size2-Regular.ttf b/_site/assets/css/fonts/KaTeX_Size2-Regular.ttf new file mode 100644 index 0000000..8a309fd Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Size2-Regular.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Size2-Regular.woff b/_site/assets/css/fonts/KaTeX_Size2-Regular.woff new file mode 100644 index 0000000..79ddf33 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Size2-Regular.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Size2-Regular.woff2 b/_site/assets/css/fonts/KaTeX_Size2-Regular.woff2 new file mode 100644 index 0000000..4cddb8b Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Size2-Regular.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Size3-Regular.ttf b/_site/assets/css/fonts/KaTeX_Size3-Regular.ttf new file mode 100644 index 0000000..14fe2db Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Size3-Regular.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Size3-Regular.woff b/_site/assets/css/fonts/KaTeX_Size3-Regular.woff new file mode 100644 index 0000000..1ecfff9 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Size3-Regular.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Size3-Regular.woff2 b/_site/assets/css/fonts/KaTeX_Size3-Regular.woff2 new file mode 100644 index 0000000..e89276f Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Size3-Regular.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Size4-Regular.ttf b/_site/assets/css/fonts/KaTeX_Size4-Regular.ttf new file mode 100644 index 0000000..f88f27b Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Size4-Regular.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Size4-Regular.woff b/_site/assets/css/fonts/KaTeX_Size4-Regular.woff new file mode 100644 index 0000000..d4223a3 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Size4-Regular.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Size4-Regular.woff2 b/_site/assets/css/fonts/KaTeX_Size4-Regular.woff2 new file mode 100644 index 0000000..93c7e82 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Size4-Regular.woff2 differ diff --git a/_site/assets/css/fonts/KaTeX_Typewriter-Regular.ttf b/_site/assets/css/fonts/KaTeX_Typewriter-Regular.ttf new file mode 100644 index 0000000..15b7a74 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Typewriter-Regular.ttf differ diff --git a/_site/assets/css/fonts/KaTeX_Typewriter-Regular.woff b/_site/assets/css/fonts/KaTeX_Typewriter-Regular.woff new file mode 100644 index 0000000..d786826 Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Typewriter-Regular.woff differ diff --git a/_site/assets/css/fonts/KaTeX_Typewriter-Regular.woff2 b/_site/assets/css/fonts/KaTeX_Typewriter-Regular.woff2 new file mode 100644 index 0000000..e2ca86a Binary files /dev/null and b/_site/assets/css/fonts/KaTeX_Typewriter-Regular.woff2 differ diff --git a/_site/assets/css/highlight.css b/_site/assets/css/highlight.css new file mode 100644 index 0000000..dae8346 --- /dev/null +++ b/_site/assets/css/highlight.css @@ -0,0 +1,80 @@ +/* code highlghting */ +.highlight .hll { background-color: #ffffcc } +.highlight { background: #f0f0f0; } +.highlight .c { color: #60a0b0; font-style: italic } /* Comment */ +.highlight .err { border: 1px solid #FF0000 } /* Error */ +.highlight .k { color: #007020; font-weight: bold } /* Keyword */ +.highlight .o { color: #666666 } /* Operator */ +.highlight .ch { color: #60a0b0; font-style: italic } /* Comment.Hashbang */ +.highlight .cm { color: #60a0b0; font-style: italic } /* Comment.Multiline */ +.highlight .cp { color: #007020 } /* Comment.Preproc */ +.highlight .cpf { color: #60a0b0; font-style: italic } /* Comment.PreprocFile */ +.highlight .c1 { color: #60a0b0; font-style: italic } /* Comment.Single */ +.highlight .cs { color: #60a0b0; background-color: #fff0f0 } /* Comment.Special */ +.highlight .gd { color: #A00000 } /* Generic.Deleted */ +.highlight .ge { font-style: italic } /* Generic.Emph */ +.highlight .gr { color: #FF0000 } /* Generic.Error */ +.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */ +.highlight .gi { color: #00A000 } /* Generic.Inserted */ +.highlight .go { color: #888888 } /* Generic.Output */ +.highlight .gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */ +.highlight .gs { font-weight: bold } /* Generic.Strong */ +.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ +.highlight .gt { color: #0044DD } /* Generic.Traceback */ +.highlight .kc { color: #007020; font-weight: bold } /* Keyword.Constant */ +.highlight .kd { color: #007020; font-weight: bold } /* Keyword.Declaration */ +.highlight .kn { color: #007020; font-weight: bold } /* Keyword.Namespace */ +.highlight .kp { color: #007020 } /* Keyword.Pseudo */ +.highlight .kr { color: #007020; font-weight: bold } /* Keyword.Reserved */ +.highlight .kt { color: #902000 } /* Keyword.Type */ +.highlight .m { color: #40a070 } /* Literal.Number */ +.highlight .s { color: #4070a0 } /* Literal.String */ +.highlight .na { color: #4070a0 } /* Name.Attribute */ +.highlight .nb { color: #007020 } /* Name.Builtin */ +.highlight .nc { color: #0e84b5; font-weight: bold } /* Name.Class */ +.highlight .no { color: #60add5 } /* Name.Constant */ +.highlight .nd { color: #555555; font-weight: bold } /* Name.Decorator */ +.highlight .ni { color: #d55537; font-weight: bold } /* Name.Entity */ +.highlight .ne { color: #007020 } /* Name.Exception */ +.highlight .nf { color: #06287e } /* Name.Function */ +.highlight .nl { color: #002070; font-weight: bold } /* Name.Label */ +.highlight .nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */ +.highlight .nt { color: #062873; font-weight: bold } /* Name.Tag */ +.highlight .nv { color: #bb60d5 } /* Name.Variable */ +.highlight .ow { color: #007020; font-weight: bold } /* Operator.Word */ +.highlight .w { color: #bbbbbb } /* Text.Whitespace */ +.highlight .mb { color: #40a070 } /* Literal.Number.Bin */ +.highlight .mf { color: #40a070 } /* Literal.Number.Float */ +.highlight .mh { color: #40a070 } /* Literal.Number.Hex */ +.highlight .mi { color: #40a070 } /* Literal.Number.Integer */ +.highlight .mo { color: #40a070 } /* Literal.Number.Oct */ +.highlight .sa { color: #4070a0 } /* Literal.String.Affix */ +.highlight .sb { color: #4070a0 } /* Literal.String.Backtick */ +.highlight .sc { color: #4070a0 } /* Literal.String.Char */ +.highlight .dl { color: #4070a0 } /* Literal.String.Delimiter */ +.highlight .sd { color: #4070a0; font-style: italic } /* Literal.String.Doc */ +.highlight .s2 { color: #4070a0 } /* Literal.String.Double */ +.highlight .se { color: #4070a0; font-weight: bold } /* Literal.String.Escape */ +.highlight .sh { color: #4070a0 } /* Literal.String.Heredoc */ +.highlight .si { color: #70a0d0; font-style: italic } /* Literal.String.Interpol */ +.highlight .sx { color: #c65d09 } /* Literal.String.Other */ +.highlight .sr { color: #235388 } /* Literal.String.Regex */ +.highlight .s1 { color: #4070a0 } /* Literal.String.Single */ +.highlight .ss { color: #517918 } /* Literal.String.Symbol */ +.highlight .bp { color: #007020 } /* Name.Builtin.Pseudo */ +.highlight .fm { color: #06287e } /* Name.Function.Magic */ +.highlight .vc { color: #bb60d5 } /* Name.Variable.Class */ +.highlight .vg { color: #bb60d5 } /* Name.Variable.Global */ +.highlight .vi { color: #bb60d5 } /* Name.Variable.Instance */ +.highlight .vm { color: #bb60d5 } /* Name.Variable.Magic */ +.highlight .il { color: #40a070 } /* Literal.Number.Integer.Long */ + +/* ADDED CUSTOM */ +.highlight .n { color: #000; } /* General name */ + +div.highlight { + padding: 0px 12px; + width: 85%; + margin: auto; + overflow-x: scroll; +} diff --git a/_site/assets/css/katex.css b/_site/assets/css/katex.css new file mode 100644 index 0000000..e0c115d --- /dev/null +++ b/_site/assets/css/katex.css @@ -0,0 +1 @@ +@font-face{font-family:KaTeX_AMS;font-style:normal;font-weight:400;src:url(fonts/KaTeX_AMS-Regular.woff2) format("woff2"),url(fonts/KaTeX_AMS-Regular.woff) format("woff"),url(fonts/KaTeX_AMS-Regular.ttf) format("truetype")}@font-face{font-family:KaTeX_Caligraphic;font-style:normal;font-weight:700;src:url(fonts/KaTeX_Caligraphic-Bold.woff2) format("woff2"),url(fonts/KaTeX_Caligraphic-Bold.woff) format("woff"),url(fonts/KaTeX_Caligraphic-Bold.ttf) format("truetype")}@font-face{font-family:KaTeX_Caligraphic;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Caligraphic-Regular.woff2) format("woff2"),url(fonts/KaTeX_Caligraphic-Regular.woff) format("woff"),url(fonts/KaTeX_Caligraphic-Regular.ttf) format("truetype")}@font-face{font-family:KaTeX_Fraktur;font-style:normal;font-weight:700;src:url(fonts/KaTeX_Fraktur-Bold.woff2) format("woff2"),url(fonts/KaTeX_Fraktur-Bold.woff) format("woff"),url(fonts/KaTeX_Fraktur-Bold.ttf) format("truetype")}@font-face{font-family:KaTeX_Fraktur;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Fraktur-Regular.woff2) format("woff2"),url(fonts/KaTeX_Fraktur-Regular.woff) format("woff"),url(fonts/KaTeX_Fraktur-Regular.ttf) format("truetype")}@font-face{font-family:KaTeX_Main;font-style:normal;font-weight:700;src:url(fonts/KaTeX_Main-Bold.woff2) format("woff2"),url(fonts/KaTeX_Main-Bold.woff) format("woff"),url(fonts/KaTeX_Main-Bold.ttf) format("truetype")}@font-face{font-family:KaTeX_Main;font-style:italic;font-weight:700;src:url(fonts/KaTeX_Main-BoldItalic.woff2) format("woff2"),url(fonts/KaTeX_Main-BoldItalic.woff) format("woff"),url(fonts/KaTeX_Main-BoldItalic.ttf) format("truetype")}@font-face{font-family:KaTeX_Main;font-style:italic;font-weight:400;src:url(fonts/KaTeX_Main-Italic.woff2) format("woff2"),url(fonts/KaTeX_Main-Italic.woff) format("woff"),url(fonts/KaTeX_Main-Italic.ttf) format("truetype")}@font-face{font-family:KaTeX_Main;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Main-Regular.woff2) format("woff2"),url(fonts/KaTeX_Main-Regular.woff) format("woff"),url(fonts/KaTeX_Main-Regular.ttf) format("truetype")}@font-face{font-family:KaTeX_Math;font-style:italic;font-weight:700;src:url(fonts/KaTeX_Math-BoldItalic.woff2) format("woff2"),url(fonts/KaTeX_Math-BoldItalic.woff) format("woff"),url(fonts/KaTeX_Math-BoldItalic.ttf) format("truetype")}@font-face{font-family:KaTeX_Math;font-style:italic;font-weight:400;src:url(fonts/KaTeX_Math-Italic.woff2) format("woff2"),url(fonts/KaTeX_Math-Italic.woff) format("woff"),url(fonts/KaTeX_Math-Italic.ttf) format("truetype")}@font-face{font-family:"KaTeX_SansSerif";font-style:normal;font-weight:700;src:url(fonts/KaTeX_SansSerif-Bold.woff2) format("woff2"),url(fonts/KaTeX_SansSerif-Bold.woff) format("woff"),url(fonts/KaTeX_SansSerif-Bold.ttf) format("truetype")}@font-face{font-family:"KaTeX_SansSerif";font-style:italic;font-weight:400;src:url(fonts/KaTeX_SansSerif-Italic.woff2) format("woff2"),url(fonts/KaTeX_SansSerif-Italic.woff) format("woff"),url(fonts/KaTeX_SansSerif-Italic.ttf) format("truetype")}@font-face{font-family:"KaTeX_SansSerif";font-style:normal;font-weight:400;src:url(fonts/KaTeX_SansSerif-Regular.woff2) format("woff2"),url(fonts/KaTeX_SansSerif-Regular.woff) format("woff"),url(fonts/KaTeX_SansSerif-Regular.ttf) format("truetype")}@font-face{font-family:KaTeX_Script;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Script-Regular.woff2) format("woff2"),url(fonts/KaTeX_Script-Regular.woff) format("woff"),url(fonts/KaTeX_Script-Regular.ttf) format("truetype")}@font-face{font-family:KaTeX_Size1;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Size1-Regular.woff2) format("woff2"),url(fonts/KaTeX_Size1-Regular.woff) format("woff"),url(fonts/KaTeX_Size1-Regular.ttf) format("truetype")}@font-face{font-family:KaTeX_Size2;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Size2-Regular.woff2) format("woff2"),url(fonts/KaTeX_Size2-Regular.woff) format("woff"),url(fonts/KaTeX_Size2-Regular.ttf) format("truetype")}@font-face{font-family:KaTeX_Size3;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Size3-Regular.woff2) format("woff2"),url(fonts/KaTeX_Size3-Regular.woff) format("woff"),url(fonts/KaTeX_Size3-Regular.ttf) format("truetype")}@font-face{font-family:KaTeX_Size4;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Size4-Regular.woff2) format("woff2"),url(fonts/KaTeX_Size4-Regular.woff) format("woff"),url(fonts/KaTeX_Size4-Regular.ttf) format("truetype")}@font-face{font-family:KaTeX_Typewriter;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Typewriter-Regular.woff2) format("woff2"),url(fonts/KaTeX_Typewriter-Regular.woff) format("woff"),url(fonts/KaTeX_Typewriter-Regular.ttf) format("truetype")}.katex{text-rendering:auto;font:normal 1.21em KaTeX_Main,Times New Roman,serif;line-height:1.2;text-indent:0}.katex *{-ms-high-contrast-adjust:none!important;border-color:currentColor}.katex .katex-version:after{content:"0.13.19"}.katex .katex-mathml{clip:rect(1px,1px,1px,1px);border:0;height:1px;overflow:hidden;padding:0;position:absolute;width:1px}.katex .katex-html>.newline{display:block}.katex .base{position:relative;white-space:nowrap;width:-webkit-min-content;width:-moz-min-content;width:min-content}.katex .base,.katex .strut{display:inline-block}.katex .textbf{font-weight:700}.katex .textit{font-style:italic}.katex .textrm{font-family:KaTeX_Main}.katex .textsf{font-family:KaTeX_SansSerif}.katex .texttt{font-family:KaTeX_Typewriter}.katex .mathnormal{font-family:KaTeX_Math;font-style:italic}.katex .mathit{font-family:KaTeX_Main;font-style:italic}.katex .mathrm{font-style:normal}.katex .mathbf{font-family:KaTeX_Main;font-weight:700}.katex .boldsymbol{font-family:KaTeX_Math;font-style:italic;font-weight:700}.katex .amsrm,.katex .mathbb,.katex .textbb{font-family:KaTeX_AMS}.katex .mathcal{font-family:KaTeX_Caligraphic}.katex .mathfrak,.katex .textfrak{font-family:KaTeX_Fraktur}.katex .mathtt{font-family:KaTeX_Typewriter}.katex .mathscr,.katex .textscr{font-family:KaTeX_Script}.katex .mathsf,.katex .textsf{font-family:KaTeX_SansSerif}.katex .mathboldsf,.katex .textboldsf{font-family:KaTeX_SansSerif;font-weight:700}.katex .mathitsf,.katex .textitsf{font-family:KaTeX_SansSerif;font-style:italic}.katex .mainrm{font-family:KaTeX_Main;font-style:normal}.katex .vlist-t{border-collapse:collapse;display:inline-table;table-layout:fixed}.katex .vlist-r{display:table-row}.katex .vlist{display:table-cell;position:relative;vertical-align:bottom}.katex .vlist>span{display:block;height:0;position:relative}.katex .vlist>span>span{display:inline-block}.katex .vlist>span>.pstrut{overflow:hidden;width:0}.katex .vlist-t2{margin-right:-2px}.katex .vlist-s{display:table-cell;font-size:1px;min-width:2px;vertical-align:bottom;width:2px}.katex .vbox{align-items:baseline;display:inline-flex;flex-direction:column}.katex .hbox{width:100%}.katex .hbox,.katex .thinbox{display:inline-flex;flex-direction:row}.katex .thinbox{max-width:0;width:0}.katex .msupsub{text-align:left}.katex .mfrac>span>span{text-align:center}.katex .mfrac .frac-line{border-bottom-style:solid;display:inline-block;width:100%}.katex .hdashline,.katex .hline,.katex .mfrac .frac-line,.katex .overline .overline-line,.katex .rule,.katex .underline .underline-line{min-height:1px}.katex .mspace{display:inline-block}.katex .clap,.katex .llap,.katex .rlap{position:relative;width:0}.katex .clap>.inner,.katex .llap>.inner,.katex .rlap>.inner{position:absolute}.katex .clap>.fix,.katex .llap>.fix,.katex .rlap>.fix{display:inline-block}.katex .llap>.inner{right:0}.katex .clap>.inner,.katex .rlap>.inner{left:0}.katex .clap>.inner>span{margin-left:-50%;margin-right:50%}.katex .rule{border:0 solid;display:inline-block;position:relative}.katex .hline,.katex .overline .overline-line,.katex .underline .underline-line{border-bottom-style:solid;display:inline-block;width:100%}.katex .hdashline{border-bottom-style:dashed;display:inline-block;width:100%}.katex .sqrt>.root{margin-left:.27777778em;margin-right:-.55555556em}.katex .fontsize-ensurer.reset-size1.size1,.katex .sizing.reset-size1.size1{font-size:1em}.katex .fontsize-ensurer.reset-size1.size2,.katex .sizing.reset-size1.size2{font-size:1.2em}.katex .fontsize-ensurer.reset-size1.size3,.katex .sizing.reset-size1.size3{font-size:1.4em}.katex .fontsize-ensurer.reset-size1.size4,.katex .sizing.reset-size1.size4{font-size:1.6em}.katex .fontsize-ensurer.reset-size1.size5,.katex .sizing.reset-size1.size5{font-size:1.8em}.katex .fontsize-ensurer.reset-size1.size6,.katex .sizing.reset-size1.size6{font-size:2em}.katex .fontsize-ensurer.reset-size1.size7,.katex .sizing.reset-size1.size7{font-size:2.4em}.katex .fontsize-ensurer.reset-size1.size8,.katex .sizing.reset-size1.size8{font-size:2.88em}.katex .fontsize-ensurer.reset-size1.size9,.katex .sizing.reset-size1.size9{font-size:3.456em}.katex .fontsize-ensurer.reset-size1.size10,.katex .sizing.reset-size1.size10{font-size:4.148em}.katex .fontsize-ensurer.reset-size1.size11,.katex .sizing.reset-size1.size11{font-size:4.976em}.katex .fontsize-ensurer.reset-size2.size1,.katex .sizing.reset-size2.size1{font-size:.83333333em}.katex .fontsize-ensurer.reset-size2.size2,.katex .sizing.reset-size2.size2{font-size:1em}.katex .fontsize-ensurer.reset-size2.size3,.katex .sizing.reset-size2.size3{font-size:1.16666667em}.katex .fontsize-ensurer.reset-size2.size4,.katex .sizing.reset-size2.size4{font-size:1.33333333em}.katex .fontsize-ensurer.reset-size2.size5,.katex .sizing.reset-size2.size5{font-size:1.5em}.katex .fontsize-ensurer.reset-size2.size6,.katex .sizing.reset-size2.size6{font-size:1.66666667em}.katex .fontsize-ensurer.reset-size2.size7,.katex .sizing.reset-size2.size7{font-size:2em}.katex .fontsize-ensurer.reset-size2.size8,.katex .sizing.reset-size2.size8{font-size:2.4em}.katex .fontsize-ensurer.reset-size2.size9,.katex .sizing.reset-size2.size9{font-size:2.88em}.katex .fontsize-ensurer.reset-size2.size10,.katex .sizing.reset-size2.size10{font-size:3.45666667em}.katex .fontsize-ensurer.reset-size2.size11,.katex .sizing.reset-size2.size11{font-size:4.14666667em}.katex .fontsize-ensurer.reset-size3.size1,.katex .sizing.reset-size3.size1{font-size:.71428571em}.katex .fontsize-ensurer.reset-size3.size2,.katex .sizing.reset-size3.size2{font-size:.85714286em}.katex .fontsize-ensurer.reset-size3.size3,.katex .sizing.reset-size3.size3{font-size:1em}.katex .fontsize-ensurer.reset-size3.size4,.katex .sizing.reset-size3.size4{font-size:1.14285714em}.katex .fontsize-ensurer.reset-size3.size5,.katex .sizing.reset-size3.size5{font-size:1.28571429em}.katex .fontsize-ensurer.reset-size3.size6,.katex .sizing.reset-size3.size6{font-size:1.42857143em}.katex .fontsize-ensurer.reset-size3.size7,.katex .sizing.reset-size3.size7{font-size:1.71428571em}.katex .fontsize-ensurer.reset-size3.size8,.katex .sizing.reset-size3.size8{font-size:2.05714286em}.katex .fontsize-ensurer.reset-size3.size9,.katex .sizing.reset-size3.size9{font-size:2.46857143em}.katex .fontsize-ensurer.reset-size3.size10,.katex .sizing.reset-size3.size10{font-size:2.96285714em}.katex .fontsize-ensurer.reset-size3.size11,.katex .sizing.reset-size3.size11{font-size:3.55428571em}.katex .fontsize-ensurer.reset-size4.size1,.katex .sizing.reset-size4.size1{font-size:.625em}.katex .fontsize-ensurer.reset-size4.size2,.katex .sizing.reset-size4.size2{font-size:.75em}.katex .fontsize-ensurer.reset-size4.size3,.katex .sizing.reset-size4.size3{font-size:.875em}.katex .fontsize-ensurer.reset-size4.size4,.katex .sizing.reset-size4.size4{font-size:1em}.katex .fontsize-ensurer.reset-size4.size5,.katex .sizing.reset-size4.size5{font-size:1.125em}.katex .fontsize-ensurer.reset-size4.size6,.katex .sizing.reset-size4.size6{font-size:1.25em}.katex .fontsize-ensurer.reset-size4.size7,.katex .sizing.reset-size4.size7{font-size:1.5em}.katex .fontsize-ensurer.reset-size4.size8,.katex .sizing.reset-size4.size8{font-size:1.8em}.katex .fontsize-ensurer.reset-size4.size9,.katex .sizing.reset-size4.size9{font-size:2.16em}.katex .fontsize-ensurer.reset-size4.size10,.katex .sizing.reset-size4.size10{font-size:2.5925em}.katex .fontsize-ensurer.reset-size4.size11,.katex .sizing.reset-size4.size11{font-size:3.11em}.katex .fontsize-ensurer.reset-size5.size1,.katex .sizing.reset-size5.size1{font-size:.55555556em}.katex .fontsize-ensurer.reset-size5.size2,.katex .sizing.reset-size5.size2{font-size:.66666667em}.katex .fontsize-ensurer.reset-size5.size3,.katex .sizing.reset-size5.size3{font-size:.77777778em}.katex .fontsize-ensurer.reset-size5.size4,.katex .sizing.reset-size5.size4{font-size:.88888889em}.katex .fontsize-ensurer.reset-size5.size5,.katex .sizing.reset-size5.size5{font-size:1em}.katex .fontsize-ensurer.reset-size5.size6,.katex .sizing.reset-size5.size6{font-size:1.11111111em}.katex .fontsize-ensurer.reset-size5.size7,.katex .sizing.reset-size5.size7{font-size:1.33333333em}.katex .fontsize-ensurer.reset-size5.size8,.katex .sizing.reset-size5.size8{font-size:1.6em}.katex .fontsize-ensurer.reset-size5.size9,.katex .sizing.reset-size5.size9{font-size:1.92em}.katex .fontsize-ensurer.reset-size5.size10,.katex .sizing.reset-size5.size10{font-size:2.30444444em}.katex .fontsize-ensurer.reset-size5.size11,.katex .sizing.reset-size5.size11{font-size:2.76444444em}.katex .fontsize-ensurer.reset-size6.size1,.katex .sizing.reset-size6.size1{font-size:.5em}.katex .fontsize-ensurer.reset-size6.size2,.katex .sizing.reset-size6.size2{font-size:.6em}.katex .fontsize-ensurer.reset-size6.size3,.katex .sizing.reset-size6.size3{font-size:.7em}.katex .fontsize-ensurer.reset-size6.size4,.katex .sizing.reset-size6.size4{font-size:.8em}.katex .fontsize-ensurer.reset-size6.size5,.katex .sizing.reset-size6.size5{font-size:.9em}.katex .fontsize-ensurer.reset-size6.size6,.katex .sizing.reset-size6.size6{font-size:1em}.katex .fontsize-ensurer.reset-size6.size7,.katex .sizing.reset-size6.size7{font-size:1.2em}.katex .fontsize-ensurer.reset-size6.size8,.katex .sizing.reset-size6.size8{font-size:1.44em}.katex .fontsize-ensurer.reset-size6.size9,.katex .sizing.reset-size6.size9{font-size:1.728em}.katex .fontsize-ensurer.reset-size6.size10,.katex .sizing.reset-size6.size10{font-size:2.074em}.katex .fontsize-ensurer.reset-size6.size11,.katex .sizing.reset-size6.size11{font-size:2.488em}.katex .fontsize-ensurer.reset-size7.size1,.katex .sizing.reset-size7.size1{font-size:.41666667em}.katex .fontsize-ensurer.reset-size7.size2,.katex .sizing.reset-size7.size2{font-size:.5em}.katex .fontsize-ensurer.reset-size7.size3,.katex .sizing.reset-size7.size3{font-size:.58333333em}.katex .fontsize-ensurer.reset-size7.size4,.katex .sizing.reset-size7.size4{font-size:.66666667em}.katex .fontsize-ensurer.reset-size7.size5,.katex .sizing.reset-size7.size5{font-size:.75em}.katex .fontsize-ensurer.reset-size7.size6,.katex .sizing.reset-size7.size6{font-size:.83333333em}.katex .fontsize-ensurer.reset-size7.size7,.katex .sizing.reset-size7.size7{font-size:1em}.katex .fontsize-ensurer.reset-size7.size8,.katex .sizing.reset-size7.size8{font-size:1.2em}.katex .fontsize-ensurer.reset-size7.size9,.katex .sizing.reset-size7.size9{font-size:1.44em}.katex .fontsize-ensurer.reset-size7.size10,.katex .sizing.reset-size7.size10{font-size:1.72833333em}.katex .fontsize-ensurer.reset-size7.size11,.katex .sizing.reset-size7.size11{font-size:2.07333333em}.katex .fontsize-ensurer.reset-size8.size1,.katex .sizing.reset-size8.size1{font-size:.34722222em}.katex .fontsize-ensurer.reset-size8.size2,.katex .sizing.reset-size8.size2{font-size:.41666667em}.katex .fontsize-ensurer.reset-size8.size3,.katex .sizing.reset-size8.size3{font-size:.48611111em}.katex .fontsize-ensurer.reset-size8.size4,.katex .sizing.reset-size8.size4{font-size:.55555556em}.katex .fontsize-ensurer.reset-size8.size5,.katex .sizing.reset-size8.size5{font-size:.625em}.katex .fontsize-ensurer.reset-size8.size6,.katex .sizing.reset-size8.size6{font-size:.69444444em}.katex .fontsize-ensurer.reset-size8.size7,.katex .sizing.reset-size8.size7{font-size:.83333333em}.katex .fontsize-ensurer.reset-size8.size8,.katex .sizing.reset-size8.size8{font-size:1em}.katex .fontsize-ensurer.reset-size8.size9,.katex .sizing.reset-size8.size9{font-size:1.2em}.katex .fontsize-ensurer.reset-size8.size10,.katex .sizing.reset-size8.size10{font-size:1.44027778em}.katex .fontsize-ensurer.reset-size8.size11,.katex .sizing.reset-size8.size11{font-size:1.72777778em}.katex .fontsize-ensurer.reset-size9.size1,.katex .sizing.reset-size9.size1{font-size:.28935185em}.katex .fontsize-ensurer.reset-size9.size2,.katex .sizing.reset-size9.size2{font-size:.34722222em}.katex .fontsize-ensurer.reset-size9.size3,.katex .sizing.reset-size9.size3{font-size:.40509259em}.katex .fontsize-ensurer.reset-size9.size4,.katex .sizing.reset-size9.size4{font-size:.46296296em}.katex .fontsize-ensurer.reset-size9.size5,.katex .sizing.reset-size9.size5{font-size:.52083333em}.katex .fontsize-ensurer.reset-size9.size6,.katex .sizing.reset-size9.size6{font-size:.5787037em}.katex .fontsize-ensurer.reset-size9.size7,.katex .sizing.reset-size9.size7{font-size:.69444444em}.katex .fontsize-ensurer.reset-size9.size8,.katex .sizing.reset-size9.size8{font-size:.83333333em}.katex .fontsize-ensurer.reset-size9.size9,.katex .sizing.reset-size9.size9{font-size:1em}.katex .fontsize-ensurer.reset-size9.size10,.katex .sizing.reset-size9.size10{font-size:1.20023148em}.katex .fontsize-ensurer.reset-size9.size11,.katex .sizing.reset-size9.size11{font-size:1.43981481em}.katex .fontsize-ensurer.reset-size10.size1,.katex .sizing.reset-size10.size1{font-size:.24108004em}.katex .fontsize-ensurer.reset-size10.size2,.katex .sizing.reset-size10.size2{font-size:.28929605em}.katex .fontsize-ensurer.reset-size10.size3,.katex .sizing.reset-size10.size3{font-size:.33751205em}.katex .fontsize-ensurer.reset-size10.size4,.katex .sizing.reset-size10.size4{font-size:.38572806em}.katex .fontsize-ensurer.reset-size10.size5,.katex .sizing.reset-size10.size5{font-size:.43394407em}.katex .fontsize-ensurer.reset-size10.size6,.katex .sizing.reset-size10.size6{font-size:.48216008em}.katex .fontsize-ensurer.reset-size10.size7,.katex .sizing.reset-size10.size7{font-size:.57859209em}.katex .fontsize-ensurer.reset-size10.size8,.katex .sizing.reset-size10.size8{font-size:.69431051em}.katex .fontsize-ensurer.reset-size10.size9,.katex .sizing.reset-size10.size9{font-size:.83317261em}.katex .fontsize-ensurer.reset-size10.size10,.katex .sizing.reset-size10.size10{font-size:1em}.katex .fontsize-ensurer.reset-size10.size11,.katex .sizing.reset-size10.size11{font-size:1.19961427em}.katex .fontsize-ensurer.reset-size11.size1,.katex .sizing.reset-size11.size1{font-size:.20096463em}.katex .fontsize-ensurer.reset-size11.size2,.katex .sizing.reset-size11.size2{font-size:.24115756em}.katex .fontsize-ensurer.reset-size11.size3,.katex .sizing.reset-size11.size3{font-size:.28135048em}.katex .fontsize-ensurer.reset-size11.size4,.katex .sizing.reset-size11.size4{font-size:.32154341em}.katex .fontsize-ensurer.reset-size11.size5,.katex .sizing.reset-size11.size5{font-size:.36173633em}.katex .fontsize-ensurer.reset-size11.size6,.katex .sizing.reset-size11.size6{font-size:.40192926em}.katex .fontsize-ensurer.reset-size11.size7,.katex .sizing.reset-size11.size7{font-size:.48231511em}.katex .fontsize-ensurer.reset-size11.size8,.katex .sizing.reset-size11.size8{font-size:.57877814em}.katex .fontsize-ensurer.reset-size11.size9,.katex .sizing.reset-size11.size9{font-size:.69453376em}.katex .fontsize-ensurer.reset-size11.size10,.katex .sizing.reset-size11.size10{font-size:.83360129em}.katex .fontsize-ensurer.reset-size11.size11,.katex .sizing.reset-size11.size11{font-size:1em}.katex .delimsizing.size1{font-family:KaTeX_Size1}.katex .delimsizing.size2{font-family:KaTeX_Size2}.katex .delimsizing.size3{font-family:KaTeX_Size3}.katex .delimsizing.size4{font-family:KaTeX_Size4}.katex .delimsizing.mult .delim-size1>span{font-family:KaTeX_Size1}.katex .delimsizing.mult .delim-size4>span{font-family:KaTeX_Size4}.katex .nulldelimiter{display:inline-block;width:.12em}.katex .delimcenter,.katex .op-symbol{position:relative}.katex .op-symbol.small-op{font-family:KaTeX_Size1}.katex .op-symbol.large-op{font-family:KaTeX_Size2}.katex .accent>.vlist-t,.katex .op-limits>.vlist-t{text-align:center}.katex .accent .accent-body{position:relative}.katex .accent .accent-body:not(.accent-full){width:0}.katex .overlay{display:block}.katex .mtable .vertical-separator{display:inline-block;min-width:1px}.katex .mtable .arraycolsep{display:inline-block}.katex .mtable .col-align-c>.vlist-t{text-align:center}.katex .mtable .col-align-l>.vlist-t{text-align:left}.katex .mtable .col-align-r>.vlist-t{text-align:right}.katex .svg-align{text-align:left}.katex svg{fill:currentColor;stroke:currentColor;fill-rule:nonzero;fill-opacity:1;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;display:block;height:inherit;position:absolute;width:100%}.katex svg path{stroke:none}.katex img{border-style:none;max-height:none;max-width:none;min-height:0;min-width:0}.katex .stretchy{display:block;overflow:hidden;position:relative;width:100%}.katex .stretchy:after,.katex .stretchy:before{content:""}.katex .hide-tail{overflow:hidden;position:relative;width:100%}.katex .halfarrow-left{left:0;overflow:hidden;position:absolute;width:50.2%}.katex .halfarrow-right{overflow:hidden;position:absolute;right:0;width:50.2%}.katex .brace-left{left:0;overflow:hidden;position:absolute;width:25.1%}.katex .brace-center{left:25%;overflow:hidden;position:absolute;width:50%}.katex .brace-right{overflow:hidden;position:absolute;right:0;width:25.1%}.katex .x-arrow-pad{padding:0 .5em}.katex .cd-arrow-pad{padding:0 .55556em 0 .27778em}.katex .mover,.katex .munder,.katex .x-arrow{text-align:center}.katex .boxpad{padding:0 .3em}.katex .fbox,.katex .fcolorbox{border:.04em solid;box-sizing:border-box}.katex .cancel-pad{padding:0 .2em}.katex .cancel-lap{margin-left:-.2em;margin-right:-.2em}.katex .sout{border-bottom-style:solid;border-bottom-width:.08em}.katex .angl{border-right:.049em solid;border-top:.049em solid;box-sizing:border-box;margin-right:.03889em}.katex .anglpad{padding:0 .03889em}.katex .eqn-num:before{content:"(" counter(katexEqnNo) ")";counter-increment:katexEqnNo}.katex .mml-eqn-num:before{content:"(" counter(mmlEqnNo) ")";counter-increment:mmlEqnNo}.katex .mtr-glue{width:50%}.katex .cd-vert-arrow{display:inline-block;position:relative}.katex .cd-label-left{display:inline-block;position:absolute;right:calc(50% + .3em);text-align:left}.katex .cd-label-right{display:inline-block;left:calc(50% + .3em);position:absolute;text-align:right}.katex-display{display:block;margin:1em 0;text-align:center}.katex-display>.katex{display:block;text-align:center;white-space:nowrap}.katex-display>.katex>.katex-html{display:block;position:relative}.katex-display>.katex>.katex-html>.tag{position:absolute;right:0}.katex-display.leqno>.katex>.katex-html>.tag{left:0;right:auto}.katex-display.fleqn>.katex{padding-left:2em;text-align:left}body{counter-reset:katexEqnNo mmlEqnNo} diff --git a/_site/assets/css/markdown.css b/_site/assets/css/markdown.css new file mode 100644 index 0000000..4393059 --- /dev/null +++ b/_site/assets/css/markdown.css @@ -0,0 +1,3 @@ +textarea { width: 100%; height: 300px; overflow-y: scroll; } + +/*# sourceMappingURL=markdown.css.map */ \ No newline at end of file diff --git a/_site/assets/css/markdown.css.map b/_site/assets/css/markdown.css.map new file mode 100644 index 0000000..df55b0e --- /dev/null +++ b/_site/assets/css/markdown.css.map @@ -0,0 +1,16 @@ +{ + "version": 3, + "file": "markdown.css", + "sources": [ + "markdown.scss", + "_sass/markdown.scss", + "_sass/vars.scss" + ], + "sourcesContent": [ + "@import \"markdown\";\n", + "@import \"vars\";\n\ntextarea {\n width: 100%;\n height: 300px;\n overflow-y: scroll;\n}\n\n", + "$normal-text-color: #ffffff;\n$background-color: #222222;\n$lighter-color: #ffffff;\n$nav-link-color: #ffffff;\n$nav-link-hover-color: black;\n$link-color: #7ad;\n$visited-link-color: #ff3492;\n$last-p-padd: 1.5em;\n$nav-padd: 1em;\n$line-under: 1px solid #aaa; \n$line-height: 1.5em;\n$term-line-height: 1em;\n$file-line-height: 1.2em;\n$terminal-bg: #000000;\n$terminal-fg: #00FF00;\n$file-bg: #444444;\n$file-fg: #ffffff;\n" + ], + "names": [], + "mappings": "ACEA,AAAA,QAAQ,CAAC,EACP,KAAK,EAAE,IAAI,EACX,MAAM,EAAE,KAAK,EACb,UAAU,EAAE,MAAM,GACnB" +} \ No newline at end of file diff --git a/_site/assets/css/style.css b/_site/assets/css/style.css new file mode 100644 index 0000000..75c1db5 --- /dev/null +++ b/_site/assets/css/style.css @@ -0,0 +1,75 @@ +body { background-color: #222222; padding: 16px; font-family: -apple-system, helvetica, arial, sans-serif; font-size: 16px; color: #ffffff; line-height: 1.5em; overflow-wrap: break-word; } + +#wrapper { max-width: 800px; margin: auto; } + +#wrapper > header { text-align: center; } + +.clear-list { list-style-type: none; margin: 0; padding: 0; } + +.clear-list li { margin: 0; padding: 0; } + +.projects .namelink { font-weight: bold; } + +h1, h2, h3, h4, h5, h6 { line-height: 1em; } + +.center { text-align: center; } + +header { margin-bottom: 18px; } + +hr { border: none; border-bottom: 1px solid #999; } + +a { text-decoration: underline; color: #7ad; } + +a:visited { color: #ff3492; } + +a.nav-link, a.post-title-link { color: #ffffff; text-decoration: none; } + +.post-title { line-height: 1.5em; color: #ffffff; margin-bottom: 8px; } + +a.citation-link { text-decoration: none; } + +nav { text-align: center; padding: 1em 0px; } + +nav a { margin: 1em; color: #ffffff; font-weight: bold; font-style: none; } + +nav a:hover, a.post-title-link:hover { text-decoration: underline; } + +li { margin: .5em; } + +table, table tr, table td, table th { border: 1px solid #aaa; border-collapse: collapse; padding: 5px; font-weight: normal; } + +table th { font-weight: bold; } + +table { width: 75%; margin: auto; } + +img { display: block; width: 55%; margin-left: auto; margin-right: auto; } + +blockquote { font-style: italic; } + +address { font-style: normal; } + +@media screen and (max-width: 600px) { body { width: 90%; } nav { text-align: left; width: 100%; } nav a { display: block; text-align: left; padding-left: 0; margin-left: 0; } } + +.mono { font-family: monospace; } + +.bold { font-weight: bold; } + +figcaption { margin-top: 10px; font-style: italic; } + +footer { padding-top: 16px; margin-bottom: 100px; } + +.terminal, .file { padding: 10px; overflow-x: scroll; } + +.terminal { line-height: 1em; color: #00FF00; background-color: #000000; } + +.file { line-height: 1.2em; background-color: #444444; color: #ffffff; } + +.small-image { width: 100%; } + +.post-date { text-transform: uppercase; font-weight: bold; color: #ffffff; } + +.post-excerpt { margin-left: 24px; } + +.post-header { margin-bottom: 8px; } + +/*# sourceMappingURL=style.css.map */ \ No newline at end of file diff --git a/_site/assets/css/style.css.map b/_site/assets/css/style.css.map new file mode 100644 index 0000000..d5e02e0 --- /dev/null +++ b/_site/assets/css/style.css.map @@ -0,0 +1,16 @@ +{ + "version": 3, + "file": "style.css", + "sources": [ + "style.scss", + "_sass/main.scss", + "_sass/vars.scss" + ], + "sourcesContent": [ + "@import \"main\";\n", + "@import \"vars\";\n\nbody {\n background-color: $background-color;\n padding: 16px;\n font-family: -apple-system, helvetica, arial, sans-serif;\n font-size: 16px;\n color: $normal-text-color;\n line-height: $line-height;\n overflow-wrap: break-word;\n}\n\n#wrapper {\n max-width: 800px;\n margin: auto;\n > header {\n text-align: center;\n }\n}\n\n.clear-list {\n list-style-type: none;\n margin: 0;\n padding: 0;\n}\n\n.clear-list li {\n margin: 0;\n padding: 0;\n}\n\n.projects .namelink {\n font-weight: bold;\n}\n\nh1, h2, h3, h4, h5, h6 {\n line-height: 1em;\n}\n\n.center {\n text-align: center;\n}\n\nheader {\n margin-bottom: 18px;\n}\n\nhr {\n border: none;\n border-bottom: 1px solid #999;\n}\n\na {\n text-decoration: underline;\n color: $link-color;\n} \n\na:visited {\n color: $visited-link-color;\n}\n\na.nav-link,\na.post-title-link {\n color: $nav-link-color;\n text-decoration: none;\n}\n\n.post-title {\n line-height: $line-height;\n color: $nav-link-color;\n margin-bottom: 8px;\n}\n\na.citation-link {\n text-decoration: none;\n}\n\nnav {\n text-align: center;\n padding: $nav-padd 0px;\n}\n\nnav a {\n margin: 1em;\n color: $nav-link-color;\n font-weight: bold;\n font-style: none;\n}\n\nnav a:hover,\na.post-title-link:hover {\n text-decoration: underline; \n}\n\nli {\n margin: .5em;\n}\n\ntable,\ntable tr,\ntable td,\ntable th {\n border: 1px solid #aaa;\n border-collapse: collapse;\n padding: 5px;\n font-weight: normal;\n}\ntable th {\n font-weight: bold;\n}\ntable {\n width: 75%;\n margin: auto;\n}\n\nimg {\n display: block;\n width: 55%;\n margin-left: auto;\n margin-right: auto;\n}\n\nblockquote {\n font-style: italic;\n}\n\naddress { \n font-style: normal;\n}\n\n@media screen and (max-width: 600px){\n body {\n width: 90%;\n }\n nav {\n text-align: left;\n width: 100%;\n }\n nav a {\n display: block;\n text-align: left;\n padding-left: 0;\n margin-left: 0;\n }\n}\n\n.mono {\n font-family: monospace;\n}\n\n.bold {\n font-weight: bold;\n}\n\nfigcaption {\n margin-top: 10px;\n font-style: italic;\n}\n\nfooter {\n padding-top: 16px;\n margin-bottom: 100px;\n}\n\n.terminal,\n.file {\n padding: 10px;\n overflow-x: scroll;\n}\n\n.terminal {\n line-height: $term-line-height;\n color: $terminal-fg;\n background-color: $terminal-bg;\n}\n\n.file {\n line-height: $file-line-height;\n background-color: $file-bg;\n color: $file-fg;\n}\n\n.small-image {\n width: 100%;\n}\n\n.post-date {\n text-transform: uppercase;\n font-weight: bold;\n color: $lighter-color;\n}\n\n.post-excerpt {\n margin-left: 24px;\n}\n\n.post-header {\n margin-bottom: 8px;\n}\n", + "$normal-text-color: #ffffff;\n$background-color: #222222;\n$lighter-color: #ffffff;\n$nav-link-color: #ffffff;\n$nav-link-hover-color: black;\n$link-color: #7ad;\n$visited-link-color: #ff3492;\n$last-p-padd: 1.5em;\n$nav-padd: 1em;\n$line-under: 1px solid #aaa; \n$line-height: 1.5em;\n$term-line-height: 1em;\n$file-line-height: 1.2em;\n$terminal-bg: #000000;\n$terminal-fg: #00FF00;\n$file-bg: #444444;\n$file-fg: #ffffff;\n" + ], + "names": [], + "mappings": "ACEA,AAAA,IAAI,CAAC,EACH,gBAAgB,ECFC,OAAO,EDGxB,OAAO,EAAE,IAAI,EACb,WAAW,EAAE,2CAA2C,EACxD,SAAS,EAAE,IAAI,EACf,KAAK,ECPa,OAAO,EDQzB,WAAW,ECEC,KAAK,EDDjB,aAAa,EAAE,UAAU,GAC1B;;AAED,AAAA,QAAQ,CAAC,EACP,SAAS,EAAE,KAAK,EAChB,MAAM,EAAE,IAAI,GAIb;;AAND,AAGE,QAHM,GAGJ,MAAM,CAAC,EACP,UAAU,EAAE,MAAM,GACnB;;AAGH,AAAA,WAAW,CAAC,EACV,eAAe,EAAE,IAAI,EACrB,MAAM,EAAE,CAAC,EACT,OAAO,EAAE,CAAC,GACX;;AAED,AAAA,WAAW,CAAC,EAAE,CAAC,EACb,MAAM,EAAE,CAAC,EACT,OAAO,EAAE,CAAC,GACX;;AAED,AAAA,SAAS,CAAC,SAAS,CAAC,EAClB,WAAW,EAAE,IAAI,GAClB;;AAED,AAAA,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EACrB,WAAW,EAAE,GAAG,GACjB;;AAED,AAAA,OAAO,CAAC,EACN,UAAU,EAAE,MAAM,GACnB;;AAED,AAAA,MAAM,CAAC,EACL,aAAa,EAAE,IAAI,GACpB;;AAED,AAAA,EAAE,CAAC,EACD,MAAM,EAAE,IAAI,EACZ,aAAa,EAAE,cAAc,GAC9B;;AAED,AAAA,CAAC,CAAC,EACA,eAAe,EAAE,SAAS,EAC1B,KAAK,ECjDM,IAAI,GDkDhB;;AAED,AAAA,CAAC,CAAC,OAAO,CAAC,EACR,KAAK,ECpDc,OAAO,GDqD3B;;AAED,AAAA,CAAC,AAAA,SAAS,EACV,CAAC,AAAA,gBAAgB,CAAC,EAChB,KAAK,EC5DU,OAAO,ED6DtB,eAAe,EAAE,IAAI,GACtB;;AAED,AAAA,WAAW,CAAC,EACV,WAAW,EC1DC,KAAK,ED2DjB,KAAK,EClEU,OAAO,EDmEtB,aAAa,EAAE,GAAG,GACnB;;AAED,AAAA,CAAC,AAAA,cAAc,CAAC,EACd,eAAe,EAAE,IAAI,GACtB;;AAED,AAAA,GAAG,CAAC,EACF,UAAU,EAAE,MAAM,EAClB,OAAO,ECvEE,GAAG,CDuEO,GAAG,GACvB;;AAED,AAAA,GAAG,CAAC,CAAC,CAAC,EACJ,MAAM,EAAE,GAAG,EACX,KAAK,ECjFU,OAAO,EDkFtB,WAAW,EAAE,IAAI,EACjB,UAAU,EAAE,IAAI,GACjB;;AAED,AAAA,GAAG,CAAC,CAAC,CAAC,KAAK,EACX,CAAC,AAAA,gBAAgB,CAAC,KAAK,CAAC,EACtB,eAAe,EAAE,SAAS,GAC3B;;AAED,AAAA,EAAE,CAAC,EACD,MAAM,EAAE,IAAI,GACb;;AAED,AAAA,KAAK,EACL,KAAK,CAAC,EAAE,EACR,KAAK,CAAC,EAAE,EACR,KAAK,CAAC,EAAE,CAAC,EACP,MAAM,EAAE,cAAc,EACtB,eAAe,EAAE,QAAQ,EACzB,OAAO,EAAE,GAAG,EACZ,WAAW,EAAE,MAAM,GACpB;;AACD,AAAA,KAAK,CAAC,EAAE,CAAC,EACP,WAAW,EAAE,IAAI,GAClB;;AACD,AAAA,KAAK,CAAC,EACJ,KAAK,EAAE,GAAG,EACV,MAAM,EAAE,IAAI,GACb;;AAED,AAAA,GAAG,CAAC,EACF,OAAO,EAAE,KAAK,EACd,KAAK,EAAE,GAAG,EACV,WAAW,EAAE,IAAI,EACjB,YAAY,EAAE,IAAI,GACnB;;AAED,AAAA,UAAU,CAAC,EACT,UAAU,EAAE,MAAM,GACnB;;AAED,AAAA,OAAO,CAAC,EACN,UAAU,EAAE,MAAM,GACnB;;AAED,MAAM,8BACJ,GAAA,AAAA,IAAI,CAAC,EACH,KAAK,EAAE,GAAG,GACX,CACD,AAAA,GAAG,CAAC,EACF,UAAU,EAAE,IAAI,EAChB,KAAK,EAAE,IAAI,GACZ,CACD,AAAA,GAAG,CAAC,CAAC,CAAC,EACJ,OAAO,EAAE,KAAK,EACd,UAAU,EAAE,IAAI,EAChB,YAAY,EAAE,CAAC,EACf,WAAW,EAAE,CAAC,GACf,EAVA;;AAaH,AAAA,KAAK,CAAC,EACJ,WAAW,EAAE,SAAS,GACvB;;AAED,AAAA,KAAK,CAAC,EACJ,WAAW,EAAE,IAAI,GAClB;;AAED,AAAA,UAAU,CAAC,EACT,UAAU,EAAE,IAAI,EAChB,UAAU,EAAE,MAAM,GACnB;;AAED,AAAA,MAAM,CAAC,EACL,WAAW,EAAE,IAAI,EACjB,aAAa,EAAE,KAAK,GACrB;;AAED,AAAA,SAAS,EACT,KAAK,CAAC,EACJ,OAAO,EAAE,IAAI,EACb,UAAU,EAAE,MAAM,GACnB;;AAED,AAAA,SAAS,CAAC,EACR,WAAW,EChKM,GAAG,EDiKpB,KAAK,EC9JO,OAAO,ED+JnB,gBAAgB,EChKJ,OAAO,GDiKpB;;AAED,AAAA,KAAK,CAAC,EACJ,WAAW,ECrKM,KAAK,EDsKtB,gBAAgB,ECnKR,OAAO,EDoKf,KAAK,ECnKG,OAAO,GDoKhB;;AAED,AAAA,YAAY,CAAC,EACX,KAAK,EAAE,IAAI,GACZ;;AAED,AAAA,UAAU,CAAC,EACT,cAAc,EAAE,SAAS,EACzB,WAAW,EAAE,IAAI,EACjB,KAAK,EC3LS,OAAO,GD4LtB;;AAED,AAAA,aAAa,CAAC,EACZ,WAAW,EAAE,IAAI,GAClB;;AAED,AAAA,YAAY,CAAC,EACX,aAAa,EAAE,GAAG,GACnB" +} \ No newline at end of file diff --git a/_site/assets/emacspeak-tutorials/arrays.zip b/_site/assets/emacspeak-tutorials/arrays.zip new file mode 100644 index 0000000..5914427 Binary files /dev/null and b/_site/assets/emacspeak-tutorials/arrays.zip differ diff --git a/_site/assets/emacspeak-tutorials/functions.zip b/_site/assets/emacspeak-tutorials/functions.zip new file mode 100644 index 0000000..f7be3f1 Binary files /dev/null and b/_site/assets/emacspeak-tutorials/functions.zip differ diff --git a/_site/assets/emacspeak-tutorials/loops.zip b/_site/assets/emacspeak-tutorials/loops.zip new file mode 100644 index 0000000..4f874bf Binary files /dev/null and b/_site/assets/emacspeak-tutorials/loops.zip differ diff --git a/_site/assets/emacspeak-tutorials/pointers.zip b/_site/assets/emacspeak-tutorials/pointers.zip new file mode 100644 index 0000000..347765b Binary files /dev/null and b/_site/assets/emacspeak-tutorials/pointers.zip differ diff --git a/_site/assets/emacspeak-tutorials/printing.zip b/_site/assets/emacspeak-tutorials/printing.zip new file mode 100644 index 0000000..7387503 Binary files /dev/null and b/_site/assets/emacspeak-tutorials/printing.zip differ diff --git a/_site/assets/emacspeak-tutorials/structs.zip b/_site/assets/emacspeak-tutorials/structs.zip new file mode 100644 index 0000000..0e5de0c Binary files /dev/null and b/_site/assets/emacspeak-tutorials/structs.zip differ diff --git a/_site/assets/emacspeak-tutorials/variables.zip b/_site/assets/emacspeak-tutorials/variables.zip new file mode 100644 index 0000000..35dc9d5 Binary files /dev/null and b/_site/assets/emacspeak-tutorials/variables.zip differ diff --git a/_site/assets/fonts/KaTeX_AMS-Regular.ttf b/_site/assets/fonts/KaTeX_AMS-Regular.ttf new file mode 100644 index 0000000..737cf8e Binary files /dev/null and b/_site/assets/fonts/KaTeX_AMS-Regular.ttf differ diff --git a/_site/assets/fonts/KaTeX_AMS-Regular.woff b/_site/assets/fonts/KaTeX_AMS-Regular.woff new file mode 100644 index 0000000..38378bf Binary files /dev/null and b/_site/assets/fonts/KaTeX_AMS-Regular.woff differ diff --git a/_site/assets/fonts/KaTeX_AMS-Regular.woff2 b/_site/assets/fonts/KaTeX_AMS-Regular.woff2 new file mode 100644 index 0000000..a4d1ba6 Binary files /dev/null and b/_site/assets/fonts/KaTeX_AMS-Regular.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Caligraphic-Bold.ttf b/_site/assets/fonts/KaTeX_Caligraphic-Bold.ttf new file mode 100644 index 0000000..04d28ab Binary files /dev/null and b/_site/assets/fonts/KaTeX_Caligraphic-Bold.ttf differ diff --git a/_site/assets/fonts/KaTeX_Caligraphic-Bold.woff b/_site/assets/fonts/KaTeX_Caligraphic-Bold.woff new file mode 100644 index 0000000..a01ce90 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Caligraphic-Bold.woff differ diff --git a/_site/assets/fonts/KaTeX_Caligraphic-Bold.woff2 b/_site/assets/fonts/KaTeX_Caligraphic-Bold.woff2 new file mode 100644 index 0000000..3792727 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Caligraphic-Bold.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Caligraphic-Regular.ttf b/_site/assets/fonts/KaTeX_Caligraphic-Regular.ttf new file mode 100644 index 0000000..b2ce555 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Caligraphic-Regular.ttf differ diff --git a/_site/assets/fonts/KaTeX_Caligraphic-Regular.woff b/_site/assets/fonts/KaTeX_Caligraphic-Regular.woff new file mode 100644 index 0000000..bc169b7 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Caligraphic-Regular.woff differ diff --git a/_site/assets/fonts/KaTeX_Caligraphic-Regular.woff2 b/_site/assets/fonts/KaTeX_Caligraphic-Regular.woff2 new file mode 100644 index 0000000..f1e38bb Binary files /dev/null and b/_site/assets/fonts/KaTeX_Caligraphic-Regular.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Fraktur-Bold.ttf b/_site/assets/fonts/KaTeX_Fraktur-Bold.ttf new file mode 100644 index 0000000..c42d169 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Fraktur-Bold.ttf differ diff --git a/_site/assets/fonts/KaTeX_Fraktur-Bold.woff b/_site/assets/fonts/KaTeX_Fraktur-Bold.woff new file mode 100644 index 0000000..f30b54b Binary files /dev/null and b/_site/assets/fonts/KaTeX_Fraktur-Bold.woff differ diff --git a/_site/assets/fonts/KaTeX_Fraktur-Bold.woff2 b/_site/assets/fonts/KaTeX_Fraktur-Bold.woff2 new file mode 100644 index 0000000..b7a8359 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Fraktur-Bold.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Fraktur-Regular.ttf b/_site/assets/fonts/KaTeX_Fraktur-Regular.ttf new file mode 100644 index 0000000..4133228 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Fraktur-Regular.ttf differ diff --git a/_site/assets/fonts/KaTeX_Fraktur-Regular.woff b/_site/assets/fonts/KaTeX_Fraktur-Regular.woff new file mode 100644 index 0000000..5af51de Binary files /dev/null and b/_site/assets/fonts/KaTeX_Fraktur-Regular.woff differ diff --git a/_site/assets/fonts/KaTeX_Fraktur-Regular.woff2 b/_site/assets/fonts/KaTeX_Fraktur-Regular.woff2 new file mode 100644 index 0000000..3874f93 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Fraktur-Regular.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Main-Bold.ttf b/_site/assets/fonts/KaTeX_Main-Bold.ttf new file mode 100644 index 0000000..14390e0 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Main-Bold.ttf differ diff --git a/_site/assets/fonts/KaTeX_Main-Bold.woff b/_site/assets/fonts/KaTeX_Main-Bold.woff new file mode 100644 index 0000000..33b4199 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Main-Bold.woff differ diff --git a/_site/assets/fonts/KaTeX_Main-Bold.woff2 b/_site/assets/fonts/KaTeX_Main-Bold.woff2 new file mode 100644 index 0000000..f9b71cb Binary files /dev/null and b/_site/assets/fonts/KaTeX_Main-Bold.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Main-BoldItalic.ttf b/_site/assets/fonts/KaTeX_Main-BoldItalic.ttf new file mode 100644 index 0000000..ad0761f Binary files /dev/null and b/_site/assets/fonts/KaTeX_Main-BoldItalic.ttf differ diff --git a/_site/assets/fonts/KaTeX_Main-BoldItalic.woff b/_site/assets/fonts/KaTeX_Main-BoldItalic.woff new file mode 100644 index 0000000..115af4f Binary files /dev/null and b/_site/assets/fonts/KaTeX_Main-BoldItalic.woff differ diff --git a/_site/assets/fonts/KaTeX_Main-BoldItalic.woff2 b/_site/assets/fonts/KaTeX_Main-BoldItalic.woff2 new file mode 100644 index 0000000..5c500c2 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Main-BoldItalic.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Main-Italic.ttf b/_site/assets/fonts/KaTeX_Main-Italic.ttf new file mode 100644 index 0000000..fc8625c Binary files /dev/null and b/_site/assets/fonts/KaTeX_Main-Italic.ttf differ diff --git a/_site/assets/fonts/KaTeX_Main-Italic.woff b/_site/assets/fonts/KaTeX_Main-Italic.woff new file mode 100644 index 0000000..2d3087a Binary files /dev/null and b/_site/assets/fonts/KaTeX_Main-Italic.woff differ diff --git a/_site/assets/fonts/KaTeX_Main-Italic.woff2 b/_site/assets/fonts/KaTeX_Main-Italic.woff2 new file mode 100644 index 0000000..08510d8 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Main-Italic.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Main-Regular.ttf b/_site/assets/fonts/KaTeX_Main-Regular.ttf new file mode 100644 index 0000000..5115a04 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Main-Regular.ttf differ diff --git a/_site/assets/fonts/KaTeX_Main-Regular.woff b/_site/assets/fonts/KaTeX_Main-Regular.woff new file mode 100644 index 0000000..42b74ab Binary files /dev/null and b/_site/assets/fonts/KaTeX_Main-Regular.woff differ diff --git a/_site/assets/fonts/KaTeX_Main-Regular.woff2 b/_site/assets/fonts/KaTeX_Main-Regular.woff2 new file mode 100644 index 0000000..18647fa Binary files /dev/null and b/_site/assets/fonts/KaTeX_Main-Regular.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Math-BoldItalic.ttf b/_site/assets/fonts/KaTeX_Math-BoldItalic.ttf new file mode 100644 index 0000000..326b523 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Math-BoldItalic.ttf differ diff --git a/_site/assets/fonts/KaTeX_Math-BoldItalic.woff b/_site/assets/fonts/KaTeX_Math-BoldItalic.woff new file mode 100644 index 0000000..5b4041a Binary files /dev/null and b/_site/assets/fonts/KaTeX_Math-BoldItalic.woff differ diff --git a/_site/assets/fonts/KaTeX_Math-BoldItalic.woff2 b/_site/assets/fonts/KaTeX_Math-BoldItalic.woff2 new file mode 100644 index 0000000..ba55276 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Math-BoldItalic.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Math-Italic.ttf b/_site/assets/fonts/KaTeX_Math-Italic.ttf new file mode 100644 index 0000000..f148fce Binary files /dev/null and b/_site/assets/fonts/KaTeX_Math-Italic.ttf differ diff --git a/_site/assets/fonts/KaTeX_Math-Italic.woff b/_site/assets/fonts/KaTeX_Math-Italic.woff new file mode 100644 index 0000000..31d0038 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Math-Italic.woff differ diff --git a/_site/assets/fonts/KaTeX_Math-Italic.woff2 b/_site/assets/fonts/KaTeX_Math-Italic.woff2 new file mode 100644 index 0000000..9871ab6 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Math-Italic.woff2 differ diff --git a/_site/assets/fonts/KaTeX_SansSerif-Bold.ttf b/_site/assets/fonts/KaTeX_SansSerif-Bold.ttf new file mode 100644 index 0000000..dce35c8 Binary files /dev/null and b/_site/assets/fonts/KaTeX_SansSerif-Bold.ttf differ diff --git a/_site/assets/fonts/KaTeX_SansSerif-Bold.woff b/_site/assets/fonts/KaTeX_SansSerif-Bold.woff new file mode 100644 index 0000000..992cb3d Binary files /dev/null and b/_site/assets/fonts/KaTeX_SansSerif-Bold.woff differ diff --git a/_site/assets/fonts/KaTeX_SansSerif-Bold.woff2 b/_site/assets/fonts/KaTeX_SansSerif-Bold.woff2 new file mode 100644 index 0000000..6dd1038 Binary files /dev/null and b/_site/assets/fonts/KaTeX_SansSerif-Bold.woff2 differ diff --git a/_site/assets/fonts/KaTeX_SansSerif-Italic.ttf b/_site/assets/fonts/KaTeX_SansSerif-Italic.ttf new file mode 100644 index 0000000..a3eb86c Binary files /dev/null and b/_site/assets/fonts/KaTeX_SansSerif-Italic.ttf differ diff --git a/_site/assets/fonts/KaTeX_SansSerif-Italic.woff b/_site/assets/fonts/KaTeX_SansSerif-Italic.woff new file mode 100644 index 0000000..f4fa252 Binary files /dev/null and b/_site/assets/fonts/KaTeX_SansSerif-Italic.woff differ diff --git a/_site/assets/fonts/KaTeX_SansSerif-Italic.woff2 b/_site/assets/fonts/KaTeX_SansSerif-Italic.woff2 new file mode 100644 index 0000000..9f2501a Binary files /dev/null and b/_site/assets/fonts/KaTeX_SansSerif-Italic.woff2 differ diff --git a/_site/assets/fonts/KaTeX_SansSerif-Regular.ttf b/_site/assets/fonts/KaTeX_SansSerif-Regular.ttf new file mode 100644 index 0000000..3be73ce Binary files /dev/null and b/_site/assets/fonts/KaTeX_SansSerif-Regular.ttf differ diff --git a/_site/assets/fonts/KaTeX_SansSerif-Regular.woff b/_site/assets/fonts/KaTeX_SansSerif-Regular.woff new file mode 100644 index 0000000..ec283f4 Binary files /dev/null and b/_site/assets/fonts/KaTeX_SansSerif-Regular.woff differ diff --git a/_site/assets/fonts/KaTeX_SansSerif-Regular.woff2 b/_site/assets/fonts/KaTeX_SansSerif-Regular.woff2 new file mode 100644 index 0000000..e46094f Binary files /dev/null and b/_site/assets/fonts/KaTeX_SansSerif-Regular.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Script-Regular.ttf b/_site/assets/fonts/KaTeX_Script-Regular.ttf new file mode 100644 index 0000000..40c8a99 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Script-Regular.ttf differ diff --git a/_site/assets/fonts/KaTeX_Script-Regular.woff b/_site/assets/fonts/KaTeX_Script-Regular.woff new file mode 100644 index 0000000..4eafae7 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Script-Regular.woff differ diff --git a/_site/assets/fonts/KaTeX_Script-Regular.woff2 b/_site/assets/fonts/KaTeX_Script-Regular.woff2 new file mode 100644 index 0000000..69b1754 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Script-Regular.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Size1-Regular.ttf b/_site/assets/fonts/KaTeX_Size1-Regular.ttf new file mode 100644 index 0000000..f0aff83 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Size1-Regular.ttf differ diff --git a/_site/assets/fonts/KaTeX_Size1-Regular.woff b/_site/assets/fonts/KaTeX_Size1-Regular.woff new file mode 100644 index 0000000..0358ee4 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Size1-Regular.woff differ diff --git a/_site/assets/fonts/KaTeX_Size1-Regular.woff2 b/_site/assets/fonts/KaTeX_Size1-Regular.woff2 new file mode 100644 index 0000000..f951ed0 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Size1-Regular.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Size2-Regular.ttf b/_site/assets/fonts/KaTeX_Size2-Regular.ttf new file mode 100644 index 0000000..4f72f16 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Size2-Regular.ttf differ diff --git a/_site/assets/fonts/KaTeX_Size2-Regular.woff b/_site/assets/fonts/KaTeX_Size2-Regular.woff new file mode 100644 index 0000000..8a053d2 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Size2-Regular.woff differ diff --git a/_site/assets/fonts/KaTeX_Size2-Regular.woff2 b/_site/assets/fonts/KaTeX_Size2-Regular.woff2 new file mode 100644 index 0000000..181d962 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Size2-Regular.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Size3-Regular.ttf b/_site/assets/fonts/KaTeX_Size3-Regular.ttf new file mode 100644 index 0000000..56d2dc6 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Size3-Regular.ttf differ diff --git a/_site/assets/fonts/KaTeX_Size3-Regular.woff b/_site/assets/fonts/KaTeX_Size3-Regular.woff new file mode 100644 index 0000000..0ec99ad Binary files /dev/null and b/_site/assets/fonts/KaTeX_Size3-Regular.woff differ diff --git a/_site/assets/fonts/KaTeX_Size3-Regular.woff2 b/_site/assets/fonts/KaTeX_Size3-Regular.woff2 new file mode 100644 index 0000000..c2985cd Binary files /dev/null and b/_site/assets/fonts/KaTeX_Size3-Regular.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Size4-Regular.ttf b/_site/assets/fonts/KaTeX_Size4-Regular.ttf new file mode 100644 index 0000000..baf0209 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Size4-Regular.ttf differ diff --git a/_site/assets/fonts/KaTeX_Size4-Regular.woff b/_site/assets/fonts/KaTeX_Size4-Regular.woff new file mode 100644 index 0000000..ff67319 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Size4-Regular.woff differ diff --git a/_site/assets/fonts/KaTeX_Size4-Regular.woff2 b/_site/assets/fonts/KaTeX_Size4-Regular.woff2 new file mode 100644 index 0000000..a4e810d Binary files /dev/null and b/_site/assets/fonts/KaTeX_Size4-Regular.woff2 differ diff --git a/_site/assets/fonts/KaTeX_Typewriter-Regular.ttf b/_site/assets/fonts/KaTeX_Typewriter-Regular.ttf new file mode 100644 index 0000000..e66c218 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Typewriter-Regular.ttf differ diff --git a/_site/assets/fonts/KaTeX_Typewriter-Regular.woff b/_site/assets/fonts/KaTeX_Typewriter-Regular.woff new file mode 100644 index 0000000..c66d149 Binary files /dev/null and b/_site/assets/fonts/KaTeX_Typewriter-Regular.woff differ diff --git a/_site/assets/fonts/KaTeX_Typewriter-Regular.woff2 b/_site/assets/fonts/KaTeX_Typewriter-Regular.woff2 new file mode 100644 index 0000000..e5bf2ce Binary files /dev/null and b/_site/assets/fonts/KaTeX_Typewriter-Regular.woff2 differ diff --git a/_site/assets/img/access1/stack.png b/_site/assets/img/access1/stack.png new file mode 100644 index 0000000..3fec3f6 Binary files /dev/null and b/_site/assets/img/access1/stack.png differ diff --git a/_site/assets/img/alice-sign-to-bob.png b/_site/assets/img/alice-sign-to-bob.png new file mode 100644 index 0000000..cfce6bf Binary files /dev/null and b/_site/assets/img/alice-sign-to-bob.png differ diff --git a/_site/assets/img/alice-to-bob.png b/_site/assets/img/alice-to-bob.png new file mode 100644 index 0000000..eaf5e64 Binary files /dev/null and b/_site/assets/img/alice-to-bob.png differ diff --git a/_site/assets/img/banff.jpg b/_site/assets/img/banff.jpg new file mode 100644 index 0000000..28bff9f Binary files /dev/null and b/_site/assets/img/banff.jpg differ diff --git a/_site/assets/img/ceasar1.jpg b/_site/assets/img/ceasar1.jpg new file mode 100644 index 0000000..67bf033 Binary files /dev/null and b/_site/assets/img/ceasar1.jpg differ diff --git a/_site/assets/img/ceasar13.png b/_site/assets/img/ceasar13.png new file mode 100644 index 0000000..8f3d905 Binary files /dev/null and b/_site/assets/img/ceasar13.png differ diff --git a/_site/assets/img/ceasar2.jpg b/_site/assets/img/ceasar2.jpg new file mode 100644 index 0000000..ec504d4 Binary files /dev/null and b/_site/assets/img/ceasar2.jpg differ diff --git a/_site/assets/img/ec.png b/_site/assets/img/ec.png new file mode 100644 index 0000000..c7532a2 Binary files /dev/null and b/_site/assets/img/ec.png differ diff --git a/_site/assets/img/encrypted-web-traffic.png b/_site/assets/img/encrypted-web-traffic.png new file mode 100644 index 0000000..ccdc74a Binary files /dev/null and b/_site/assets/img/encrypted-web-traffic.png differ diff --git a/_site/assets/img/keyring.jpg b/_site/assets/img/keyring.jpg new file mode 100644 index 0000000..279c46d Binary files /dev/null and b/_site/assets/img/keyring.jpg differ diff --git a/_site/assets/img/public-key-example1.svg b/_site/assets/img/public-key-example1.svg new file mode 100644 index 0000000..a9220e8 --- /dev/null +++ b/_site/assets/img/public-key-example1.svg @@ -0,0 +1,201 @@ + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + 鮑伯 + 你好! + + + Hello + Bob! + + + + + + 鮑伯的 + 私有密鑰 + + + Bob's + Private Key + + + + + 加密 + Encrypt + + + 6EB6957008E03CE4 + + + + + 鮑伯 + 你好! + + + Hello + Bob! + + + + + 解密 + Decrypt + + + + + 鮑伯的 + 公開金鑰 + + + Bob's + Public Key + + + + 愛麗斯 + Alice + + + + 鮑伯 + Bob + + + + + + + + diff --git a/_site/assets/img/qyf-xss/1-input.png b/_site/assets/img/qyf-xss/1-input.png new file mode 100644 index 0000000..0a381df Binary files /dev/null and b/_site/assets/img/qyf-xss/1-input.png differ diff --git a/_site/assets/img/qyf-xss/2-bold.png b/_site/assets/img/qyf-xss/2-bold.png new file mode 100644 index 0000000..3e77c20 Binary files /dev/null and b/_site/assets/img/qyf-xss/2-bold.png differ diff --git a/_site/assets/img/qyf-xss/3-bold-leaderboard.png b/_site/assets/img/qyf-xss/3-bold-leaderboard.png new file mode 100644 index 0000000..37204ad Binary files /dev/null and b/_site/assets/img/qyf-xss/3-bold-leaderboard.png differ diff --git a/_site/assets/img/qyf-xss/3-steve-board.png b/_site/assets/img/qyf-xss/3-steve-board.png new file mode 100644 index 0000000..ce2920a Binary files /dev/null and b/_site/assets/img/qyf-xss/3-steve-board.png differ diff --git a/_site/assets/img/qyf-xss/5-maxlength.png b/_site/assets/img/qyf-xss/5-maxlength.png new file mode 100644 index 0000000..f8c236e Binary files /dev/null and b/_site/assets/img/qyf-xss/5-maxlength.png differ diff --git a/_site/assets/img/w3c-padding-margin.png b/_site/assets/img/w3c-padding-margin.png new file mode 100644 index 0000000..956a4f1 Binary files /dev/null and b/_site/assets/img/w3c-padding-margin.png differ diff --git a/_site/assets/img/wget-log b/_site/assets/img/wget-log new file mode 100644 index 0000000..6e8c91c --- /dev/null +++ b/_site/assets/img/wget-log @@ -0,0 +1,12 @@ +--2020-04-02 20:52:24-- https://www.google.com/url?sa=i +Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt' +Resolving www.google.com (www.google.com)... 172.217.3.196, 2607:f8b0:400a:809::2004 +Connecting to www.google.com (www.google.com)|172.217.3.196|:443... connected. +HTTP request sent, awaiting response... 200 OK +Length: unspecified [text/html] +Saving to: 'url?sa=i' + + url?sa=i [<=> ] 0 --.-KB/s url?sa=i [ <=> ] 1.38K --.-KB/s in 0s + +2020-04-02 20:52:24 (10.1 MB/s) - 'url?sa=i' saved [1414] + diff --git a/_site/assets/js/hacked.js b/_site/assets/js/hacked.js new file mode 100644 index 0000000..212749b --- /dev/null +++ b/_site/assets/js/hacked.js @@ -0,0 +1,2 @@ +alert("Warning! This site is insecure and open to an attack called XSS. You are now being redirected to an article about this vulnerability."); +window.href = "https://tait.tech/2020/04/22/quiz-your-friends.html"; diff --git a/_site/assets/js/katex.js b/_site/assets/js/katex.js new file mode 100644 index 0000000..8e56e01 --- /dev/null +++ b/_site/assets/js/katex.js @@ -0,0 +1 @@ +!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.katex=t():e.katex=t()}("undefined"!=typeof self?self:this,(function(){return function(){"use strict";var e={d:function(t,r){for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)}},t={};e.d(t,{default:function(){return Zn}});var r=function e(t,r){this.position=void 0;var n,a="KaTeX parse error: "+t,i=r&&r.loc;if(i&&i.start<=i.end){var o=i.lexer.input;n=i.start;var s=i.end;n===o.length?a+=" at end of input: ":a+=" at position "+(n+1)+": ";var l=o.slice(n,s).replace(/[^]/g,"$&\u0332");a+=(n>15?"\u2026"+o.slice(n-15,n):o.slice(0,n))+l+(s+15":">","<":"<",'"':""","'":"'"},o=/[&><"']/g;var s=function e(t){return"ordgroup"===t.type||"color"===t.type?1===t.body.length?e(t.body[0]):t:"font"===t.type?e(t.body):t},l={contains:function(e,t){return-1!==e.indexOf(t)},deflt:function(e,t){return void 0===e?t:e},escape:function(e){return String(e).replace(o,(function(e){return i[e]}))},hyphenate:function(e){return e.replace(a,"-$1").toLowerCase()},getBaseElem:s,isCharacterBox:function(e){var t=s(e);return"mathord"===t.type||"textord"===t.type||"atom"===t.type},protocolFromUrl:function(e){var t=/^\s*([^\\/#]*?)(?::|�*58|�*3a)/i.exec(e);return null!=t?t[1]:"_relative"}},h=function(){function e(e){this.displayMode=void 0,this.output=void 0,this.leqno=void 0,this.fleqn=void 0,this.throwOnError=void 0,this.errorColor=void 0,this.macros=void 0,this.minRuleThickness=void 0,this.colorIsTextColor=void 0,this.strict=void 0,this.trust=void 0,this.maxSize=void 0,this.maxExpand=void 0,this.globalGroup=void 0,e=e||{},this.displayMode=l.deflt(e.displayMode,!1),this.output=l.deflt(e.output,"htmlAndMathml"),this.leqno=l.deflt(e.leqno,!1),this.fleqn=l.deflt(e.fleqn,!1),this.throwOnError=l.deflt(e.throwOnError,!0),this.errorColor=l.deflt(e.errorColor,"#cc0000"),this.macros=e.macros||{},this.minRuleThickness=Math.max(0,l.deflt(e.minRuleThickness,0)),this.colorIsTextColor=l.deflt(e.colorIsTextColor,!1),this.strict=l.deflt(e.strict,"warn"),this.trust=l.deflt(e.trust,!1),this.maxSize=Math.max(0,l.deflt(e.maxSize,1/0)),this.maxExpand=Math.max(0,l.deflt(e.maxExpand,1e3)),this.globalGroup=l.deflt(e.globalGroup,!1)}var t=e.prototype;return t.reportNonstrict=function(e,t,r){var a=this.strict;if("function"==typeof a&&(a=a(e,t,r)),a&&"ignore"!==a){if(!0===a||"error"===a)throw new n("LaTeX-incompatible input and strict mode is set to 'error': "+t+" ["+e+"]",r);"warn"===a?"undefined"!=typeof console&&console.warn("LaTeX-incompatible input and strict mode is set to 'warn': "+t+" ["+e+"]"):"undefined"!=typeof console&&console.warn("LaTeX-incompatible input and strict mode is set to unrecognized '"+a+"': "+t+" ["+e+"]")}},t.useStrictBehavior=function(e,t,r){var n=this.strict;if("function"==typeof n)try{n=n(e,t,r)}catch(e){n="error"}return!(!n||"ignore"===n)&&(!0===n||"error"===n||("warn"===n?("undefined"!=typeof console&&console.warn("LaTeX-incompatible input and strict mode is set to 'warn': "+t+" ["+e+"]"),!1):("undefined"!=typeof console&&console.warn("LaTeX-incompatible input and strict mode is set to unrecognized '"+n+"': "+t+" ["+e+"]"),!1)))},t.isTrusted=function(e){e.url&&!e.protocol&&(e.protocol=l.protocolFromUrl(e.url));var t="function"==typeof this.trust?this.trust(e):this.trust;return Boolean(t)},e}(),m=function(){function e(e,t,r){this.id=void 0,this.size=void 0,this.cramped=void 0,this.id=e,this.size=t,this.cramped=r}var t=e.prototype;return t.sup=function(){return c[u[this.id]]},t.sub=function(){return c[p[this.id]]},t.fracNum=function(){return c[d[this.id]]},t.fracDen=function(){return c[f[this.id]]},t.cramp=function(){return c[g[this.id]]},t.text=function(){return c[v[this.id]]},t.isTight=function(){return this.size>=2},e}(),c=[new m(0,0,!1),new m(1,0,!0),new m(2,1,!1),new m(3,1,!0),new m(4,2,!1),new m(5,2,!0),new m(6,3,!1),new m(7,3,!0)],u=[4,5,4,5,6,7,6,7],p=[5,5,5,5,7,7,7,7],d=[2,3,4,5,6,7,6,7],f=[3,3,5,5,7,7,7,7],g=[1,1,3,3,5,5,7,7],v=[0,1,2,3,2,3,2,3],b={DISPLAY:c[0],TEXT:c[2],SCRIPT:c[4],SCRIPTSCRIPT:c[6]},y=[{name:"latin",blocks:[[256,591],[768,879]]},{name:"cyrillic",blocks:[[1024,1279]]},{name:"armenian",blocks:[[1328,1423]]},{name:"brahmic",blocks:[[2304,4255]]},{name:"georgian",blocks:[[4256,4351]]},{name:"cjk",blocks:[[12288,12543],[19968,40879],[65280,65376]]},{name:"hangul",blocks:[[44032,55215]]}];var x=[];function w(e){for(var t=0;t=x[t]&&e<=x[t+1])return!0;return!1}y.forEach((function(e){return e.blocks.forEach((function(e){return x.push.apply(x,e)}))}));var k=80,S={doubleleftarrow:"M262 157\nl10-10c34-36 62.7-77 86-123 3.3-8 5-13.3 5-16 0-5.3-6.7-8-20-8-7.3\n 0-12.2.5-14.5 1.5-2.3 1-4.8 4.5-7.5 10.5-49.3 97.3-121.7 169.3-217 216-28\n 14-57.3 25-88 33-6.7 2-11 3.8-13 5.5-2 1.7-3 4.2-3 7.5s1 5.8 3 7.5\nc2 1.7 6.3 3.5 13 5.5 68 17.3 128.2 47.8 180.5 91.5 52.3 43.7 93.8 96.2 124.5\n 157.5 9.3 8 15.3 12.3 18 13h6c12-.7 18-4 18-10 0-2-1.7-7-5-15-23.3-46-52-87\n-86-123l-10-10h399738v-40H218c328 0 0 0 0 0l-10-8c-26.7-20-65.7-43-117-69 2.7\n-2 6-3.7 10-5 36.7-16 72.3-37.3 107-64l10-8h399782v-40z\nm8 0v40h399730v-40zm0 194v40h399730v-40z",doublerightarrow:"M399738 392l\n-10 10c-34 36-62.7 77-86 123-3.3 8-5 13.3-5 16 0 5.3 6.7 8 20 8 7.3 0 12.2-.5\n 14.5-1.5 2.3-1 4.8-4.5 7.5-10.5 49.3-97.3 121.7-169.3 217-216 28-14 57.3-25 88\n-33 6.7-2 11-3.8 13-5.5 2-1.7 3-4.2 3-7.5s-1-5.8-3-7.5c-2-1.7-6.3-3.5-13-5.5-68\n-17.3-128.2-47.8-180.5-91.5-52.3-43.7-93.8-96.2-124.5-157.5-9.3-8-15.3-12.3-18\n-13h-6c-12 .7-18 4-18 10 0 2 1.7 7 5 15 23.3 46 52 87 86 123l10 10H0v40h399782\nc-328 0 0 0 0 0l10 8c26.7 20 65.7 43 117 69-2.7 2-6 3.7-10 5-36.7 16-72.3 37.3\n-107 64l-10 8H0v40zM0 157v40h399730v-40zm0 194v40h399730v-40z",leftarrow:"M400000 241H110l3-3c68.7-52.7 113.7-120\n 135-202 4-14.7 6-23 6-25 0-7.3-7-11-21-11-8 0-13.2.8-15.5 2.5-2.3 1.7-4.2 5.8\n-5.5 12.5-1.3 4.7-2.7 10.3-4 17-12 48.7-34.8 92-68.5 130S65.3 228.3 18 247\nc-10 4-16 7.7-18 11 0 8.7 6 14.3 18 17 47.3 18.7 87.8 47 121.5 85S196 441.3 208\n 490c.7 2 1.3 5 2 9s1.2 6.7 1.5 8c.3 1.3 1 3.3 2 6s2.2 4.5 3.5 5.5c1.3 1 3.3\n 1.8 6 2.5s6 1 10 1c14 0 21-3.7 21-11 0-2-2-10.3-6-25-20-79.3-65-146.7-135-202\n l-3-3h399890zM100 241v40h399900v-40z",leftbrace:"M6 548l-6-6v-35l6-11c56-104 135.3-181.3 238-232 57.3-28.7 117\n-45 179-50h399577v120H403c-43.3 7-81 15-113 26-100.7 33-179.7 91-237 174-2.7\n 5-6 9-10 13-.7 1-7.3 1-20 1H6z",leftbraceunder:"M0 6l6-6h17c12.688 0 19.313.3 20 1 4 4 7.313 8.3 10 13\n 35.313 51.3 80.813 93.8 136.5 127.5 55.688 33.7 117.188 55.8 184.5 66.5.688\n 0 2 .3 4 1 18.688 2.7 76 4.3 172 5h399450v120H429l-6-1c-124.688-8-235-61.7\n-331-161C60.687 138.7 32.312 99.3 7 54L0 41V6z",leftgroup:"M400000 80\nH435C64 80 168.3 229.4 21 260c-5.9 1.2-18 0-18 0-2 0-3-1-3-3v-38C76 61 257 0\n 435 0h399565z",leftgroupunder:"M400000 262\nH435C64 262 168.3 112.6 21 82c-5.9-1.2-18 0-18 0-2 0-3 1-3 3v38c76 158 257 219\n 435 219h399565z",leftharpoon:"M0 267c.7 5.3 3 10 7 14h399993v-40H93c3.3\n-3.3 10.2-9.5 20.5-18.5s17.8-15.8 22.5-20.5c50.7-52 88-110.3 112-175 4-11.3 5\n-18.3 3-21-1.3-4-7.3-6-18-6-8 0-13 .7-15 2s-4.7 6.7-8 16c-42 98.7-107.3 174.7\n-196 228-6.7 4.7-10.7 8-12 10-1.3 2-2 5.7-2 11zm100-26v40h399900v-40z",leftharpoonplus:"M0 267c.7 5.3 3 10 7 14h399993v-40H93c3.3-3.3 10.2-9.5\n 20.5-18.5s17.8-15.8 22.5-20.5c50.7-52 88-110.3 112-175 4-11.3 5-18.3 3-21-1.3\n-4-7.3-6-18-6-8 0-13 .7-15 2s-4.7 6.7-8 16c-42 98.7-107.3 174.7-196 228-6.7 4.7\n-10.7 8-12 10-1.3 2-2 5.7-2 11zm100-26v40h399900v-40zM0 435v40h400000v-40z\nm0 0v40h400000v-40z",leftharpoondown:"M7 241c-4 4-6.333 8.667-7 14 0 5.333.667 9 2 11s5.333\n 5.333 12 10c90.667 54 156 130 196 228 3.333 10.667 6.333 16.333 9 17 2 .667 5\n 1 9 1h5c10.667 0 16.667-2 18-6 2-2.667 1-9.667-3-21-32-87.333-82.667-157.667\n-152-211l-3-3h399907v-40zM93 281 H400000 v-40L7 241z",leftharpoondownplus:"M7 435c-4 4-6.3 8.7-7 14 0 5.3.7 9 2 11s5.3 5.3 12\n 10c90.7 54 156 130 196 228 3.3 10.7 6.3 16.3 9 17 2 .7 5 1 9 1h5c10.7 0 16.7\n-2 18-6 2-2.7 1-9.7-3-21-32-87.3-82.7-157.7-152-211l-3-3h399907v-40H7zm93 0\nv40h399900v-40zM0 241v40h399900v-40zm0 0v40h399900v-40z",lefthook:"M400000 281 H103s-33-11.2-61-33.5S0 197.3 0 164s14.2-61.2 42.5\n-83.5C70.8 58.2 104 47 142 47 c16.7 0 25 6.7 25 20 0 12-8.7 18.7-26 20-40 3.3\n-68.7 15.7-86 37-10 12-15 25.3-15 40 0 22.7 9.8 40.7 29.5 54 19.7 13.3 43.5 21\n 71.5 23h399859zM103 281v-40h399897v40z",leftlinesegment:"M40 281 V428 H0 V94 H40 V241 H400000 v40z\nM40 281 V428 H0 V94 H40 V241 H400000 v40z",leftmapsto:"M40 281 V448H0V74H40V241H400000v40z\nM40 281 V448H0V74H40V241H400000v40z",leftToFrom:"M0 147h400000v40H0zm0 214c68 40 115.7 95.7 143 167h22c15.3 0 23\n-.3 23-1 0-1.3-5.3-13.7-16-37-18-35.3-41.3-69-70-101l-7-8h399905v-40H95l7-8\nc28.7-32 52-65.7 70-101 10.7-23.3 16-35.7 16-37 0-.7-7.7-1-23-1h-22C115.7 265.3\n 68 321 0 361zm0-174v-40h399900v40zm100 154v40h399900v-40z",longequal:"M0 50 h400000 v40H0z m0 194h40000v40H0z\nM0 50 h400000 v40H0z m0 194h40000v40H0z",midbrace:"M200428 334\nc-100.7-8.3-195.3-44-280-108-55.3-42-101.7-93-139-153l-9-14c-2.7 4-5.7 8.7-9 14\n-53.3 86.7-123.7 153-211 199-66.7 36-137.3 56.3-212 62H0V214h199568c178.3-11.7\n 311.7-78.3 403-201 6-8 9.7-12 11-12 .7-.7 6.7-1 18-1s17.3.3 18 1c1.3 0 5 4 11\n 12 44.7 59.3 101.3 106.3 170 141s145.3 54.3 229 60h199572v120z",midbraceunder:"M199572 214\nc100.7 8.3 195.3 44 280 108 55.3 42 101.7 93 139 153l9 14c2.7-4 5.7-8.7 9-14\n 53.3-86.7 123.7-153 211-199 66.7-36 137.3-56.3 212-62h199568v120H200432c-178.3\n 11.7-311.7 78.3-403 201-6 8-9.7 12-11 12-.7.7-6.7 1-18 1s-17.3-.3-18-1c-1.3 0\n-5-4-11-12-44.7-59.3-101.3-106.3-170-141s-145.3-54.3-229-60H0V214z",oiintSize1:"M512.6 71.6c272.6 0 320.3 106.8 320.3 178.2 0 70.8-47.7 177.6\n-320.3 177.6S193.1 320.6 193.1 249.8c0-71.4 46.9-178.2 319.5-178.2z\nm368.1 178.2c0-86.4-60.9-215.4-368.1-215.4-306.4 0-367.3 129-367.3 215.4 0 85.8\n60.9 214.8 367.3 214.8 307.2 0 368.1-129 368.1-214.8z",oiintSize2:"M757.8 100.1c384.7 0 451.1 137.6 451.1 230 0 91.3-66.4 228.8\n-451.1 228.8-386.3 0-452.7-137.5-452.7-228.8 0-92.4 66.4-230 452.7-230z\nm502.4 230c0-111.2-82.4-277.2-502.4-277.2s-504 166-504 277.2\nc0 110 84 276 504 276s502.4-166 502.4-276z",oiiintSize1:"M681.4 71.6c408.9 0 480.5 106.8 480.5 178.2 0 70.8-71.6 177.6\n-480.5 177.6S202.1 320.6 202.1 249.8c0-71.4 70.5-178.2 479.3-178.2z\nm525.8 178.2c0-86.4-86.8-215.4-525.7-215.4-437.9 0-524.7 129-524.7 215.4 0\n85.8 86.8 214.8 524.7 214.8 438.9 0 525.7-129 525.7-214.8z",oiiintSize2:"M1021.2 53c603.6 0 707.8 165.8 707.8 277.2 0 110-104.2 275.8\n-707.8 275.8-606 0-710.2-165.8-710.2-275.8C311 218.8 415.2 53 1021.2 53z\nm770.4 277.1c0-131.2-126.4-327.6-770.5-327.6S248.4 198.9 248.4 330.1\nc0 130 128.8 326.4 772.7 326.4s770.5-196.4 770.5-326.4z",rightarrow:"M0 241v40h399891c-47.3 35.3-84 78-110 128\n-16.7 32-27.7 63.7-33 95 0 1.3-.2 2.7-.5 4-.3 1.3-.5 2.3-.5 3 0 7.3 6.7 11 20\n 11 8 0 13.2-.8 15.5-2.5 2.3-1.7 4.2-5.5 5.5-11.5 2-13.3 5.7-27 11-41 14.7-44.7\n 39-84.5 73-119.5s73.7-60.2 119-75.5c6-2 9-5.7 9-11s-3-9-9-11c-45.3-15.3-85\n-40.5-119-75.5s-58.3-74.8-73-119.5c-4.7-14-8.3-27.3-11-40-1.3-6.7-3.2-10.8-5.5\n-12.5-2.3-1.7-7.5-2.5-15.5-2.5-14 0-21 3.7-21 11 0 2 2 10.3 6 25 20.7 83.3 67\n 151.7 139 205zm0 0v40h399900v-40z",rightbrace:"M400000 542l\n-6 6h-17c-12.7 0-19.3-.3-20-1-4-4-7.3-8.3-10-13-35.3-51.3-80.8-93.8-136.5-127.5\ns-117.2-55.8-184.5-66.5c-.7 0-2-.3-4-1-18.7-2.7-76-4.3-172-5H0V214h399571l6 1\nc124.7 8 235 61.7 331 161 31.3 33.3 59.7 72.7 85 118l7 13v35z",rightbraceunder:"M399994 0l6 6v35l-6 11c-56 104-135.3 181.3-238 232-57.3\n 28.7-117 45-179 50H-300V214h399897c43.3-7 81-15 113-26 100.7-33 179.7-91 237\n-174 2.7-5 6-9 10-13 .7-1 7.3-1 20-1h17z",rightgroup:"M0 80h399565c371 0 266.7 149.4 414 180 5.9 1.2 18 0 18 0 2 0\n 3-1 3-3v-38c-76-158-257-219-435-219H0z",rightgroupunder:"M0 262h399565c371 0 266.7-149.4 414-180 5.9-1.2 18 0 18\n 0 2 0 3 1 3 3v38c-76 158-257 219-435 219H0z",rightharpoon:"M0 241v40h399993c4.7-4.7 7-9.3 7-14 0-9.3\n-3.7-15.3-11-18-92.7-56.7-159-133.7-199-231-3.3-9.3-6-14.7-8-16-2-1.3-7-2-15-2\n-10.7 0-16.7 2-18 6-2 2.7-1 9.7 3 21 15.3 42 36.7 81.8 64 119.5 27.3 37.7 58\n 69.2 92 94.5zm0 0v40h399900v-40z",rightharpoonplus:"M0 241v40h399993c4.7-4.7 7-9.3 7-14 0-9.3-3.7-15.3-11\n-18-92.7-56.7-159-133.7-199-231-3.3-9.3-6-14.7-8-16-2-1.3-7-2-15-2-10.7 0-16.7\n 2-18 6-2 2.7-1 9.7 3 21 15.3 42 36.7 81.8 64 119.5 27.3 37.7 58 69.2 92 94.5z\nm0 0v40h399900v-40z m100 194v40h399900v-40zm0 0v40h399900v-40z",rightharpoondown:"M399747 511c0 7.3 6.7 11 20 11 8 0 13-.8 15-2.5s4.7-6.8\n 8-15.5c40-94 99.3-166.3 178-217 13.3-8 20.3-12.3 21-13 5.3-3.3 8.5-5.8 9.5\n-7.5 1-1.7 1.5-5.2 1.5-10.5s-2.3-10.3-7-15H0v40h399908c-34 25.3-64.7 57-92 95\n-27.3 38-48.7 77.7-64 119-3.3 8.7-5 14-5 16zM0 241v40h399900v-40z",rightharpoondownplus:"M399747 705c0 7.3 6.7 11 20 11 8 0 13-.8\n 15-2.5s4.7-6.8 8-15.5c40-94 99.3-166.3 178-217 13.3-8 20.3-12.3 21-13 5.3-3.3\n 8.5-5.8 9.5-7.5 1-1.7 1.5-5.2 1.5-10.5s-2.3-10.3-7-15H0v40h399908c-34 25.3\n-64.7 57-92 95-27.3 38-48.7 77.7-64 119-3.3 8.7-5 14-5 16zM0 435v40h399900v-40z\nm0-194v40h400000v-40zm0 0v40h400000v-40z",righthook:"M399859 241c-764 0 0 0 0 0 40-3.3 68.7-15.7 86-37 10-12 15-25.3\n 15-40 0-22.7-9.8-40.7-29.5-54-19.7-13.3-43.5-21-71.5-23-17.3-1.3-26-8-26-20 0\n-13.3 8.7-20 26-20 38 0 71 11.2 99 33.5 0 0 7 5.6 21 16.7 14 11.2 21 33.5 21\n 66.8s-14 61.2-42 83.5c-28 22.3-61 33.5-99 33.5L0 241z M0 281v-40h399859v40z",rightlinesegment:"M399960 241 V94 h40 V428 h-40 V281 H0 v-40z\nM399960 241 V94 h40 V428 h-40 V281 H0 v-40z",rightToFrom:"M400000 167c-70.7-42-118-97.7-142-167h-23c-15.3 0-23 .3-23\n 1 0 1.3 5.3 13.7 16 37 18 35.3 41.3 69 70 101l7 8H0v40h399905l-7 8c-28.7 32\n-52 65.7-70 101-10.7 23.3-16 35.7-16 37 0 .7 7.7 1 23 1h23c24-69.3 71.3-125 142\n-167z M100 147v40h399900v-40zM0 341v40h399900v-40z",twoheadleftarrow:"M0 167c68 40\n 115.7 95.7 143 167h22c15.3 0 23-.3 23-1 0-1.3-5.3-13.7-16-37-18-35.3-41.3-69\n-70-101l-7-8h125l9 7c50.7 39.3 85 86 103 140h46c0-4.7-6.3-18.7-19-42-18-35.3\n-40-67.3-66-96l-9-9h399716v-40H284l9-9c26-28.7 48-60.7 66-96 12.7-23.333 19\n-37.333 19-42h-46c-18 54-52.3 100.7-103 140l-9 7H95l7-8c28.7-32 52-65.7 70-101\n 10.7-23.333 16-35.7 16-37 0-.7-7.7-1-23-1h-22C115.7 71.3 68 127 0 167z",twoheadrightarrow:"M400000 167\nc-68-40-115.7-95.7-143-167h-22c-15.3 0-23 .3-23 1 0 1.3 5.3 13.7 16 37 18 35.3\n 41.3 69 70 101l7 8h-125l-9-7c-50.7-39.3-85-86-103-140h-46c0 4.7 6.3 18.7 19 42\n 18 35.3 40 67.3 66 96l9 9H0v40h399716l-9 9c-26 28.7-48 60.7-66 96-12.7 23.333\n-19 37.333-19 42h46c18-54 52.3-100.7 103-140l9-7h125l-7 8c-28.7 32-52 65.7-70\n 101-10.7 23.333-16 35.7-16 37 0 .7 7.7 1 23 1h22c27.3-71.3 75-127 143-167z",tilde1:"M200 55.538c-77 0-168 73.953-177 73.953-3 0-7\n-2.175-9-5.437L2 97c-1-2-2-4-2-6 0-4 2-7 5-9l20-12C116 12 171 0 207 0c86 0\n 114 68 191 68 78 0 168-68 177-68 4 0 7 2 9 5l12 19c1 2.175 2 4.35 2 6.525 0\n 4.35-2 7.613-5 9.788l-19 13.05c-92 63.077-116.937 75.308-183 76.128\n-68.267.847-113-73.952-191-73.952z",tilde2:"M344 55.266c-142 0-300.638 81.316-311.5 86.418\n-8.01 3.762-22.5 10.91-23.5 5.562L1 120c-1-2-1-3-1-4 0-5 3-9 8-10l18.4-9C160.9\n 31.9 283 0 358 0c148 0 188 122 331 122s314-97 326-97c4 0 8 2 10 7l7 21.114\nc1 2.14 1 3.21 1 4.28 0 5.347-3 9.626-7 10.696l-22.3 12.622C852.6 158.372 751\n 181.476 676 181.476c-149 0-189-126.21-332-126.21z",tilde3:"M786 59C457 59 32 175.242 13 175.242c-6 0-10-3.457\n-11-10.37L.15 138c-1-7 3-12 10-13l19.2-6.4C378.4 40.7 634.3 0 804.3 0c337 0\n 411.8 157 746.8 157 328 0 754-112 773-112 5 0 10 3 11 9l1 14.075c1 8.066-.697\n 16.595-6.697 17.492l-21.052 7.31c-367.9 98.146-609.15 122.696-778.15 122.696\n -338 0-409-156.573-744-156.573z",tilde4:"M786 58C457 58 32 177.487 13 177.487c-6 0-10-3.345\n-11-10.035L.15 143c-1-7 3-12 10-13l22-6.7C381.2 35 637.15 0 807.15 0c337 0 409\n 177 744 177 328 0 754-127 773-127 5 0 10 3 11 9l1 14.794c1 7.805-3 13.38-9\n 14.495l-20.7 5.574c-366.85 99.79-607.3 139.372-776.3 139.372-338 0-409\n -175.236-744-175.236z",vec:"M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5\n3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11\n10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63\n-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1\n-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59\nH213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359\nc-16-25.333-24-45-24-59z",widehat1:"M529 0h5l519 115c5 1 9 5 9 10 0 1-1 2-1 3l-4 22\nc-1 5-5 9-11 9h-2L532 67 19 159h-2c-5 0-9-4-11-9l-5-22c-1-6 2-12 8-13z",widehat2:"M1181 0h2l1171 176c6 0 10 5 10 11l-2 23c-1 6-5 10\n-11 10h-1L1182 67 15 220h-1c-6 0-10-4-11-10l-2-23c-1-6 4-11 10-11z",widehat3:"M1181 0h2l1171 236c6 0 10 5 10 11l-2 23c-1 6-5 10\n-11 10h-1L1182 67 15 280h-1c-6 0-10-4-11-10l-2-23c-1-6 4-11 10-11z",widehat4:"M1181 0h2l1171 296c6 0 10 5 10 11l-2 23c-1 6-5 10\n-11 10h-1L1182 67 15 340h-1c-6 0-10-4-11-10l-2-23c-1-6 4-11 10-11z",widecheck1:"M529,159h5l519,-115c5,-1,9,-5,9,-10c0,-1,-1,-2,-1,-3l-4,-22c-1,\n-5,-5,-9,-11,-9h-2l-512,92l-513,-92h-2c-5,0,-9,4,-11,9l-5,22c-1,6,2,12,8,13z",widecheck2:"M1181,220h2l1171,-176c6,0,10,-5,10,-11l-2,-23c-1,-6,-5,-10,\n-11,-10h-1l-1168,153l-1167,-153h-1c-6,0,-10,4,-11,10l-2,23c-1,6,4,11,10,11z",widecheck3:"M1181,280h2l1171,-236c6,0,10,-5,10,-11l-2,-23c-1,-6,-5,-10,\n-11,-10h-1l-1168,213l-1167,-213h-1c-6,0,-10,4,-11,10l-2,23c-1,6,4,11,10,11z",widecheck4:"M1181,340h2l1171,-296c6,0,10,-5,10,-11l-2,-23c-1,-6,-5,-10,\n-11,-10h-1l-1168,273l-1167,-273h-1c-6,0,-10,4,-11,10l-2,23c-1,6,4,11,10,11z",baraboveleftarrow:"M400000 620h-399890l3 -3c68.7 -52.7 113.7 -120 135 -202\nc4 -14.7 6 -23 6 -25c0 -7.3 -7 -11 -21 -11c-8 0 -13.2 0.8 -15.5 2.5\nc-2.3 1.7 -4.2 5.8 -5.5 12.5c-1.3 4.7 -2.7 10.3 -4 17c-12 48.7 -34.8 92 -68.5 130\ns-74.2 66.3 -121.5 85c-10 4 -16 7.7 -18 11c0 8.7 6 14.3 18 17c47.3 18.7 87.8 47\n121.5 85s56.5 81.3 68.5 130c0.7 2 1.3 5 2 9s1.2 6.7 1.5 8c0.3 1.3 1 3.3 2 6\ns2.2 4.5 3.5 5.5c1.3 1 3.3 1.8 6 2.5s6 1 10 1c14 0 21 -3.7 21 -11\nc0 -2 -2 -10.3 -6 -25c-20 -79.3 -65 -146.7 -135 -202l-3 -3h399890z\nM100 620v40h399900v-40z M0 241v40h399900v-40zM0 241v40h399900v-40z",rightarrowabovebar:"M0 241v40h399891c-47.3 35.3-84 78-110 128-16.7 32\n-27.7 63.7-33 95 0 1.3-.2 2.7-.5 4-.3 1.3-.5 2.3-.5 3 0 7.3 6.7 11 20 11 8 0\n13.2-.8 15.5-2.5 2.3-1.7 4.2-5.5 5.5-11.5 2-13.3 5.7-27 11-41 14.7-44.7 39\n-84.5 73-119.5s73.7-60.2 119-75.5c6-2 9-5.7 9-11s-3-9-9-11c-45.3-15.3-85-40.5\n-119-75.5s-58.3-74.8-73-119.5c-4.7-14-8.3-27.3-11-40-1.3-6.7-3.2-10.8-5.5\n-12.5-2.3-1.7-7.5-2.5-15.5-2.5-14 0-21 3.7-21 11 0 2 2 10.3 6 25 20.7 83.3 67\n151.7 139 205zm96 379h399894v40H0zm0 0h399904v40H0z",baraboveshortleftharpoon:"M507,435c-4,4,-6.3,8.7,-7,14c0,5.3,0.7,9,2,11\nc1.3,2,5.3,5.3,12,10c90.7,54,156,130,196,228c3.3,10.7,6.3,16.3,9,17\nc2,0.7,5,1,9,1c0,0,5,0,5,0c10.7,0,16.7,-2,18,-6c2,-2.7,1,-9.7,-3,-21\nc-32,-87.3,-82.7,-157.7,-152,-211c0,0,-3,-3,-3,-3l399351,0l0,-40\nc-398570,0,-399437,0,-399437,0z M593 435 v40 H399500 v-40z\nM0 281 v-40 H399908 v40z M0 281 v-40 H399908 v40z",rightharpoonaboveshortbar:"M0,241 l0,40c399126,0,399993,0,399993,0\nc4.7,-4.7,7,-9.3,7,-14c0,-9.3,-3.7,-15.3,-11,-18c-92.7,-56.7,-159,-133.7,-199,\n-231c-3.3,-9.3,-6,-14.7,-8,-16c-2,-1.3,-7,-2,-15,-2c-10.7,0,-16.7,2,-18,6\nc-2,2.7,-1,9.7,3,21c15.3,42,36.7,81.8,64,119.5c27.3,37.7,58,69.2,92,94.5z\nM0 241 v40 H399908 v-40z M0 475 v-40 H399500 v40z M0 475 v-40 H399500 v40z",shortbaraboveleftharpoon:"M7,435c-4,4,-6.3,8.7,-7,14c0,5.3,0.7,9,2,11\nc1.3,2,5.3,5.3,12,10c90.7,54,156,130,196,228c3.3,10.7,6.3,16.3,9,17c2,0.7,5,1,9,\n1c0,0,5,0,5,0c10.7,0,16.7,-2,18,-6c2,-2.7,1,-9.7,-3,-21c-32,-87.3,-82.7,-157.7,\n-152,-211c0,0,-3,-3,-3,-3l399907,0l0,-40c-399126,0,-399993,0,-399993,0z\nM93 435 v40 H400000 v-40z M500 241 v40 H400000 v-40z M500 241 v40 H400000 v-40z",shortrightharpoonabovebar:"M53,241l0,40c398570,0,399437,0,399437,0\nc4.7,-4.7,7,-9.3,7,-14c0,-9.3,-3.7,-15.3,-11,-18c-92.7,-56.7,-159,-133.7,-199,\n-231c-3.3,-9.3,-6,-14.7,-8,-16c-2,-1.3,-7,-2,-15,-2c-10.7,0,-16.7,2,-18,6\nc-2,2.7,-1,9.7,3,21c15.3,42,36.7,81.8,64,119.5c27.3,37.7,58,69.2,92,94.5z\nM500 241 v40 H399408 v-40z M500 435 v40 H400000 v-40z"},M=function(){function e(e){this.children=void 0,this.classes=void 0,this.height=void 0,this.depth=void 0,this.maxFontSize=void 0,this.style=void 0,this.children=e,this.classes=[],this.height=0,this.depth=0,this.maxFontSize=0,this.style={}}var t=e.prototype;return t.hasClass=function(e){return l.contains(this.classes,e)},t.toNode=function(){for(var e=document.createDocumentFragment(),t=0;t"},N=function(){function e(e,t,r,n){this.children=void 0,this.attributes=void 0,this.classes=void 0,this.height=void 0,this.depth=void 0,this.width=void 0,this.maxFontSize=void 0,this.style=void 0,A.call(this,e,r,n),this.children=t||[]}var t=e.prototype;return t.setAttribute=function(e,t){this.attributes[e]=t},t.hasClass=function(e){return l.contains(this.classes,e)},t.toNode=function(){return T.call(this,"span")},t.toMarkup=function(){return B.call(this,"span")},e}(),q=function(){function e(e,t,r,n){this.children=void 0,this.attributes=void 0,this.classes=void 0,this.height=void 0,this.depth=void 0,this.maxFontSize=void 0,this.style=void 0,A.call(this,t,n),this.children=r||[],this.setAttribute("href",e)}var t=e.prototype;return t.setAttribute=function(e,t){this.attributes[e]=t},t.hasClass=function(e){return l.contains(this.classes,e)},t.toNode=function(){return T.call(this,"a")},t.toMarkup=function(){return B.call(this,"a")},e}(),C=function(){function e(e,t,r){this.src=void 0,this.alt=void 0,this.classes=void 0,this.height=void 0,this.depth=void 0,this.maxFontSize=void 0,this.style=void 0,this.alt=t,this.src=e,this.classes=["mord"],this.style=r}var t=e.prototype;return t.hasClass=function(e){return l.contains(this.classes,e)},t.toNode=function(){var e=document.createElement("img");for(var t in e.src=this.src,e.alt=this.alt,e.className="mord",this.style)this.style.hasOwnProperty(t)&&(e.style[t]=this.style[t]);return e},t.toMarkup=function(){var e=""+this.alt+"=a[0]&&e<=a[1])return r.name}return null}(this.text.charCodeAt(0));l&&this.classes.push(l+"_fallback"),/[\xee\xef\xed\xec]/.test(this.text)&&(this.text=I[this.text])}var t=e.prototype;return t.hasClass=function(e){return l.contains(this.classes,e)},t.toNode=function(){var e=document.createTextNode(this.text),t=null;for(var r in this.italic>0&&((t=document.createElement("span")).style.marginRight=this.italic+"em"),this.classes.length>0&&((t=t||document.createElement("span")).className=z(this.classes)),this.style)this.style.hasOwnProperty(r)&&((t=t||document.createElement("span")).style[r]=this.style[r]);return t?(t.appendChild(e),t):e},t.toMarkup=function(){var e=!1,t="0&&(r+="margin-right:"+this.italic+"em;"),this.style)this.style.hasOwnProperty(n)&&(r+=l.hyphenate(n)+":"+this.style[n]+";");r&&(e=!0,t+=' style="'+l.escape(r)+'"');var a=l.escape(this.text);return e?(t+=">",t+=a,t+=""):a},e}(),O=function(){function e(e,t){this.children=void 0,this.attributes=void 0,this.children=e||[],this.attributes=t||{}}var t=e.prototype;return t.toNode=function(){var e=document.createElementNS("http://www.w3.org/2000/svg","svg");for(var t in this.attributes)Object.prototype.hasOwnProperty.call(this.attributes,t)&&e.setAttribute(t,this.attributes[t]);for(var r=0;r":""},e}(),H=function(){function e(e){this.attributes=void 0,this.attributes=e||{}}var t=e.prototype;return t.toNode=function(){var e=document.createElementNS("http://www.w3.org/2000/svg","line");for(var t in this.attributes)Object.prototype.hasOwnProperty.call(this.attributes,t)&&e.setAttribute(t,this.attributes[t]);return e},t.toMarkup=function(){var e="","\\gt",!0),_(j,Z,oe,"\u2208","\\in",!0),_(j,Z,oe,"\ue020","\\@not"),_(j,Z,oe,"\u2282","\\subset",!0),_(j,Z,oe,"\u2283","\\supset",!0),_(j,Z,oe,"\u2286","\\subseteq",!0),_(j,Z,oe,"\u2287","\\supseteq",!0),_(j,K,oe,"\u2288","\\nsubseteq",!0),_(j,K,oe,"\u2289","\\nsupseteq",!0),_(j,Z,oe,"\u22a8","\\models"),_(j,Z,oe,"\u2190","\\leftarrow",!0),_(j,Z,oe,"\u2264","\\le"),_(j,Z,oe,"\u2264","\\leq",!0),_(j,Z,oe,"<","\\lt",!0),_(j,Z,oe,"\u2192","\\rightarrow",!0),_(j,Z,oe,"\u2192","\\to"),_(j,K,oe,"\u2271","\\ngeq",!0),_(j,K,oe,"\u2270","\\nleq",!0),_(j,Z,se,"\xa0","\\ "),_(j,Z,se,"\xa0","\\space"),_(j,Z,se,"\xa0","\\nobreakspace"),_($,Z,se,"\xa0","\\ "),_($,Z,se,"\xa0"," "),_($,Z,se,"\xa0","\\space"),_($,Z,se,"\xa0","\\nobreakspace"),_(j,Z,se,null,"\\nobreak"),_(j,Z,se,null,"\\allowbreak"),_(j,Z,ie,",",","),_(j,Z,ie,";",";"),_(j,K,Q,"\u22bc","\\barwedge",!0),_(j,K,Q,"\u22bb","\\veebar",!0),_(j,Z,Q,"\u2299","\\odot",!0),_(j,Z,Q,"\u2295","\\oplus",!0),_(j,Z,Q,"\u2297","\\otimes",!0),_(j,Z,le,"\u2202","\\partial",!0),_(j,Z,Q,"\u2298","\\oslash",!0),_(j,K,Q,"\u229a","\\circledcirc",!0),_(j,K,Q,"\u22a1","\\boxdot",!0),_(j,Z,Q,"\u25b3","\\bigtriangleup"),_(j,Z,Q,"\u25bd","\\bigtriangledown"),_(j,Z,Q,"\u2020","\\dagger"),_(j,Z,Q,"\u22c4","\\diamond"),_(j,Z,Q,"\u22c6","\\star"),_(j,Z,Q,"\u25c3","\\triangleleft"),_(j,Z,Q,"\u25b9","\\triangleright"),_(j,Z,ae,"{","\\{"),_($,Z,le,"{","\\{"),_($,Z,le,"{","\\textbraceleft"),_(j,Z,ee,"}","\\}"),_($,Z,le,"}","\\}"),_($,Z,le,"}","\\textbraceright"),_(j,Z,ae,"{","\\lbrace"),_(j,Z,ee,"}","\\rbrace"),_(j,Z,ae,"[","\\lbrack",!0),_($,Z,le,"[","\\lbrack",!0),_(j,Z,ee,"]","\\rbrack",!0),_($,Z,le,"]","\\rbrack",!0),_(j,Z,ae,"(","\\lparen",!0),_(j,Z,ee,")","\\rparen",!0),_($,Z,le,"<","\\textless",!0),_($,Z,le,">","\\textgreater",!0),_(j,Z,ae,"\u230a","\\lfloor",!0),_(j,Z,ee,"\u230b","\\rfloor",!0),_(j,Z,ae,"\u2308","\\lceil",!0),_(j,Z,ee,"\u2309","\\rceil",!0),_(j,Z,le,"\\","\\backslash"),_(j,Z,le,"\u2223","|"),_(j,Z,le,"\u2223","\\vert"),_($,Z,le,"|","\\textbar",!0),_(j,Z,le,"\u2225","\\|"),_(j,Z,le,"\u2225","\\Vert"),_($,Z,le,"\u2225","\\textbardbl"),_($,Z,le,"~","\\textasciitilde"),_($,Z,le,"\\","\\textbackslash"),_($,Z,le,"^","\\textasciicircum"),_(j,Z,oe,"\u2191","\\uparrow",!0),_(j,Z,oe,"\u21d1","\\Uparrow",!0),_(j,Z,oe,"\u2193","\\downarrow",!0),_(j,Z,oe,"\u21d3","\\Downarrow",!0),_(j,Z,oe,"\u2195","\\updownarrow",!0),_(j,Z,oe,"\u21d5","\\Updownarrow",!0),_(j,Z,ne,"\u2210","\\coprod"),_(j,Z,ne,"\u22c1","\\bigvee"),_(j,Z,ne,"\u22c0","\\bigwedge"),_(j,Z,ne,"\u2a04","\\biguplus"),_(j,Z,ne,"\u22c2","\\bigcap"),_(j,Z,ne,"\u22c3","\\bigcup"),_(j,Z,ne,"\u222b","\\int"),_(j,Z,ne,"\u222b","\\intop"),_(j,Z,ne,"\u222c","\\iint"),_(j,Z,ne,"\u222d","\\iiint"),_(j,Z,ne,"\u220f","\\prod"),_(j,Z,ne,"\u2211","\\sum"),_(j,Z,ne,"\u2a02","\\bigotimes"),_(j,Z,ne,"\u2a01","\\bigoplus"),_(j,Z,ne,"\u2a00","\\bigodot"),_(j,Z,ne,"\u222e","\\oint"),_(j,Z,ne,"\u222f","\\oiint"),_(j,Z,ne,"\u2230","\\oiiint"),_(j,Z,ne,"\u2a06","\\bigsqcup"),_(j,Z,ne,"\u222b","\\smallint"),_($,Z,te,"\u2026","\\textellipsis"),_(j,Z,te,"\u2026","\\mathellipsis"),_($,Z,te,"\u2026","\\ldots",!0),_(j,Z,te,"\u2026","\\ldots",!0),_(j,Z,te,"\u22ef","\\@cdots",!0),_(j,Z,te,"\u22f1","\\ddots",!0),_(j,Z,le,"\u22ee","\\varvdots"),_(j,Z,J,"\u02ca","\\acute"),_(j,Z,J,"\u02cb","\\grave"),_(j,Z,J,"\xa8","\\ddot"),_(j,Z,J,"~","\\tilde"),_(j,Z,J,"\u02c9","\\bar"),_(j,Z,J,"\u02d8","\\breve"),_(j,Z,J,"\u02c7","\\check"),_(j,Z,J,"^","\\hat"),_(j,Z,J,"\u20d7","\\vec"),_(j,Z,J,"\u02d9","\\dot"),_(j,Z,J,"\u02da","\\mathring"),_(j,Z,re,"\ue131","\\@imath"),_(j,Z,re,"\ue237","\\@jmath"),_(j,Z,le,"\u0131","\u0131"),_(j,Z,le,"\u0237","\u0237"),_($,Z,le,"\u0131","\\i",!0),_($,Z,le,"\u0237","\\j",!0),_($,Z,le,"\xdf","\\ss",!0),_($,Z,le,"\xe6","\\ae",!0),_($,Z,le,"\u0153","\\oe",!0),_($,Z,le,"\xf8","\\o",!0),_($,Z,le,"\xc6","\\AE",!0),_($,Z,le,"\u0152","\\OE",!0),_($,Z,le,"\xd8","\\O",!0),_($,Z,J,"\u02ca","\\'"),_($,Z,J,"\u02cb","\\`"),_($,Z,J,"\u02c6","\\^"),_($,Z,J,"\u02dc","\\~"),_($,Z,J,"\u02c9","\\="),_($,Z,J,"\u02d8","\\u"),_($,Z,J,"\u02d9","\\."),_($,Z,J,"\xb8","\\c"),_($,Z,J,"\u02da","\\r"),_($,Z,J,"\u02c7","\\v"),_($,Z,J,"\xa8",'\\"'),_($,Z,J,"\u02dd","\\H"),_($,Z,J,"\u25ef","\\textcircled");var he={"--":!0,"---":!0,"``":!0,"''":!0};_($,Z,le,"\u2013","--",!0),_($,Z,le,"\u2013","\\textendash"),_($,Z,le,"\u2014","---",!0),_($,Z,le,"\u2014","\\textemdash"),_($,Z,le,"\u2018","`",!0),_($,Z,le,"\u2018","\\textquoteleft"),_($,Z,le,"\u2019","'",!0),_($,Z,le,"\u2019","\\textquoteright"),_($,Z,le,"\u201c","``",!0),_($,Z,le,"\u201c","\\textquotedblleft"),_($,Z,le,"\u201d","''",!0),_($,Z,le,"\u201d","\\textquotedblright"),_(j,Z,le,"\xb0","\\degree",!0),_($,Z,le,"\xb0","\\degree"),_($,Z,le,"\xb0","\\textdegree",!0),_(j,Z,le,"\xa3","\\pounds"),_(j,Z,le,"\xa3","\\mathsterling",!0),_($,Z,le,"\xa3","\\pounds"),_($,Z,le,"\xa3","\\textsterling",!0),_(j,K,le,"\u2720","\\maltese"),_($,K,le,"\u2720","\\maltese");for(var me='0123456789/@."',ce=0;ce=5?0:e>=3?1:2]){var r=G[t]={cssEmPerMu:P.quad[t]/18};for(var n in P)P.hasOwnProperty(n)&&(r[n]=P[n][t])}return G[t]}(this.size)),this._fontMetrics},t.getColor=function(){return this.phantom?"transparent":this.color},e}();Ie.BASESIZE=6;var Re=Ie,Oe={pt:1,mm:7227/2540,cm:7227/254,in:72.27,bp:1.00375,pc:12,dd:1238/1157,cc:14856/1157,nd:685/642,nc:1370/107,sp:1/65536,px:1.00375},Ee={ex:!0,em:!0,mu:!0},He=function(e){return"string"!=typeof e&&(e=e.unit),e in Oe||e in Ee||"ex"===e},Le=function(e,t){var r;if(e.unit in Oe)r=Oe[e.unit]/t.fontMetrics().ptPerEm/t.sizeMultiplier;else if("mu"===e.unit)r=t.fontMetrics().cssEmPerMu;else{var a;if(a=t.style.isTight()?t.havingStyle(t.style.text()):t,"ex"===e.unit)r=a.fontMetrics().xHeight;else{if("em"!==e.unit)throw new n("Invalid unit: '"+e.unit+"'");r=a.fontMetrics().quad}a!==t&&(r*=a.sizeMultiplier/t.sizeMultiplier)}return Math.min(e.number*r,t.maxSize)},De=function(e,t,r){return X[r][e]&&X[r][e].replace&&(e=X[r][e].replace),{value:e,metrics:V(e,t,r)}},Pe=function(e,t,r,n,a){var i,o=De(e,t,r),s=o.metrics;if(e=o.value,s){var l=s.italic;("text"===r||n&&"mathit"===n.font)&&(l=0),i=new R(e,s.height,s.depth,l,s.skew,s.width,a)}else"undefined"!=typeof console&&console.warn("No character metrics for '"+e+"' in style '"+t+"' and mode '"+r+"'"),i=new R(e,0,0,0,0,0,a);if(n){i.maxFontSize=n.sizeMultiplier,n.style.isTight()&&i.classes.push("mtight");var h=n.getColor();h&&(i.style.color=h)}return i},Fe=function(e,t){if(z(e.classes)!==z(t.classes)||e.skew!==t.skew||e.maxFontSize!==t.maxFontSize)return!1;if(1===e.classes.length){var r=e.classes[0];if("mbin"===r||"mord"===r)return!1}for(var n in e.style)if(e.style.hasOwnProperty(n)&&e.style[n]!==t.style[n])return!1;for(var a in t.style)if(t.style.hasOwnProperty(a)&&e.style[a]!==t.style[a])return!1;return!0},Ve=function(e){for(var t=0,r=0,n=0,a=0;at&&(t=i.height),i.depth>r&&(r=i.depth),i.maxFontSize>n&&(n=i.maxFontSize)}e.height=t,e.depth=r,e.maxFontSize=n},Ge=function(e,t,r,n){var a=new N(e,t,r,n);return Ve(a),a},Ue=function(e,t,r,n){return new N(e,t,r,n)},Ye=function(e){var t=new M(e);return Ve(t),t},We=function(e,t,r){var n="";switch(e){case"amsrm":n="AMS";break;case"textrm":n="Main";break;case"textsf":n="SansSerif";break;case"texttt":n="Typewriter";break;default:n=e}return n+"-"+("textbf"===t&&"textit"===r?"BoldItalic":"textbf"===t?"Bold":"textit"===t?"Italic":"Regular")},Xe={mathbf:{variant:"bold",fontName:"Main-Bold"},mathrm:{variant:"normal",fontName:"Main-Regular"},textit:{variant:"italic",fontName:"Main-Italic"},mathit:{variant:"italic",fontName:"Main-Italic"},mathnormal:{variant:"italic",fontName:"Math-Italic"},mathbb:{variant:"double-struck",fontName:"AMS-Regular"},mathcal:{variant:"script",fontName:"Caligraphic-Regular"},mathfrak:{variant:"fraktur",fontName:"Fraktur-Regular"},mathscr:{variant:"script",fontName:"Script-Regular"},mathsf:{variant:"sans-serif",fontName:"SansSerif-Regular"},mathtt:{variant:"monospace",fontName:"Typewriter-Regular"}},_e={vec:["vec",.471,.714],oiintSize1:["oiintSize1",.957,.499],oiintSize2:["oiintSize2",1.472,.659],oiiintSize1:["oiiintSize1",1.304,.499],oiiintSize2:["oiiintSize2",1.98,.659]},je={fontMap:Xe,makeSymbol:Pe,mathsym:function(e,t,r,n){return void 0===n&&(n=[]),"boldsymbol"===r.font&&De(e,"Main-Bold",t).metrics?Pe(e,"Main-Bold",t,r,n.concat(["mathbf"])):"\\"===e||"main"===X[t][e].font?Pe(e,"Main-Regular",t,r,n):Pe(e,"AMS-Regular",t,r,n.concat(["amsrm"]))},makeSpan:Ge,makeSvgSpan:Ue,makeLineSpan:function(e,t,r){var n=Ge([e],[],t);return n.height=Math.max(r||t.fontMetrics().defaultRuleThickness,t.minRuleThickness),n.style.borderBottomWidth=n.height+"em",n.maxFontSize=1,n},makeAnchor:function(e,t,r,n){var a=new q(e,t,r,n);return Ve(a),a},makeFragment:Ye,wrapFragment:function(e,t){return e instanceof M?Ge([],[e],t):e},makeVList:function(e,t){for(var r=function(e){if("individualShift"===e.positionType){for(var t=e.children,r=[t[0]],n=-t[0].shift-t[0].elem.depth,a=n,i=1;i0&&(o.push(yt(s,t)),s=[]),o.push(a[l]));s.length>0&&o.push(yt(s,t)),r?((i=yt(ut(r,t,!0))).classes=["tag"],o.push(i)):n&&o.push(n);var m=st(["katex-html"],o);if(m.setAttribute("aria-hidden","true"),i){var c=i.children[0];c.style.height=m.height+m.depth+"em",c.style.verticalAlign=-m.depth+"em"}return m}function wt(e){return new M(e)}var kt=function(){function e(e,t,r){this.type=void 0,this.attributes=void 0,this.children=void 0,this.classes=void 0,this.type=e,this.attributes={},this.children=t||[],this.classes=r||[]}var t=e.prototype;return t.setAttribute=function(e,t){this.attributes[e]=t},t.getAttribute=function(e){return this.attributes[e]},t.toNode=function(){var e=document.createElementNS("http://www.w3.org/1998/Math/MathML",this.type);for(var t in this.attributes)Object.prototype.hasOwnProperty.call(this.attributes,t)&&e.setAttribute(t,this.attributes[t]);this.classes.length>0&&(e.className=z(this.classes));for(var r=0;r0&&(e+=' class ="'+l.escape(z(this.classes))+'"'),e+=">";for(var r=0;r"},t.toText=function(){return this.children.map((function(e){return e.toText()})).join("")},e}(),St=function(){function e(e){this.text=void 0,this.text=e}var t=e.prototype;return t.toNode=function(){return document.createTextNode(this.text)},t.toMarkup=function(){return l.escape(this.toText())},t.toText=function(){return this.text},e}(),Mt={MathNode:kt,TextNode:St,SpaceNode:function(){function e(e){this.width=void 0,this.character=void 0,this.width=e,this.character=e>=.05555&&e<=.05556?"\u200a":e>=.1666&&e<=.1667?"\u2009":e>=.2222&&e<=.2223?"\u2005":e>=.2777&&e<=.2778?"\u2005\u200a":e>=-.05556&&e<=-.05555?"\u200a\u2063":e>=-.1667&&e<=-.1666?"\u2009\u2063":e>=-.2223&&e<=-.2222?"\u205f\u2063":e>=-.2778&&e<=-.2777?"\u2005\u2063":null}var t=e.prototype;return t.toNode=function(){if(this.character)return document.createTextNode(this.character);var e=document.createElementNS("http://www.w3.org/1998/Math/MathML","mspace");return e.setAttribute("width",this.width+"em"),e},t.toMarkup=function(){return this.character?""+this.character+"":''},t.toText=function(){return this.character?this.character:" "},e}(),newDocumentFragment:wt},zt=function(e,t,r){return!X[t][e]||!X[t][e].replace||55349===e.charCodeAt(0)||he.hasOwnProperty(e)&&r&&(r.fontFamily&&"tt"===r.fontFamily.substr(4,2)||r.font&&"tt"===r.font.substr(4,2))||(e=X[t][e].replace),new Mt.TextNode(e)},At=function(e){return 1===e.length?e[0]:new Mt.MathNode("mrow",e)},Tt=function(e,t){if("texttt"===t.fontFamily)return"monospace";if("textsf"===t.fontFamily)return"textit"===t.fontShape&&"textbf"===t.fontWeight?"sans-serif-bold-italic":"textit"===t.fontShape?"sans-serif-italic":"textbf"===t.fontWeight?"bold-sans-serif":"sans-serif";if("textit"===t.fontShape&&"textbf"===t.fontWeight)return"bold-italic";if("textit"===t.fontShape)return"italic";if("textbf"===t.fontWeight)return"bold";var r=t.font;if(!r||"mathnormal"===r)return null;var n=e.mode;if("mathit"===r)return"italic";if("boldsymbol"===r)return"textord"===e.type?"bold":"bold-italic";if("mathbf"===r)return"bold";if("mathbb"===r)return"double-struck";if("mathfrak"===r)return"fraktur";if("mathscr"===r||"mathcal"===r)return"script";if("mathsf"===r)return"sans-serif";if("mathtt"===r)return"monospace";var a=e.text;return l.contains(["\\imath","\\jmath"],a)?null:(X[n][a]&&X[n][a].replace&&(a=X[n][a].replace),V(a,je.fontMap[r].fontName,n)?je.fontMap[r].variant:null)},Bt=function(e,t,r){if(1===e.length){var n=qt(e[0],t);return r&&n instanceof kt&&"mo"===n.type&&(n.setAttribute("lspace","0em"),n.setAttribute("rspace","0em")),[n]}for(var a,i=[],o=0;o0&&(p.text=p.text.slice(0,1)+"\u0338"+p.text.slice(1),i.pop())}}}i.push(s),a=s}return i},Nt=function(e,t,r){return At(Bt(e,t,r))},qt=function(e,t){if(!e)return new Mt.MathNode("mrow");if(rt[e.type])return rt[e.type](e,t);throw new n("Got group of unknown type: '"+e.type+"'")};function Ct(e,t,r,n,a){var i,o=Bt(e,r);i=1===o.length&&o[0]instanceof kt&&l.contains(["mrow","mtable"],o[0].type)?o[0]:new Mt.MathNode("mrow",o);var s=new Mt.MathNode("annotation",[new Mt.TextNode(t)]);s.setAttribute("encoding","application/x-tex");var h=new Mt.MathNode("semantics",[i,s]),m=new Mt.MathNode("math",[h]);m.setAttribute("xmlns","http://www.w3.org/1998/Math/MathML"),n&&m.setAttribute("display","block");var c=a?"katex":"katex-mathml";return je.makeSpan([c],[m])}var It=function(e){return new Re({style:e.displayMode?b.DISPLAY:b.TEXT,maxSize:e.maxSize,minRuleThickness:e.minRuleThickness})},Rt=function(e,t){if(t.displayMode){var r=["katex-display"];t.leqno&&r.push("leqno"),t.fleqn&&r.push("fleqn"),e=je.makeSpan(r,[e])}return e},Ot=function(e,t,r){var n,a=It(r);if("mathml"===r.output)return Ct(e,t,a,r.displayMode,!0);if("html"===r.output){var i=xt(e,a);n=je.makeSpan(["katex"],[i])}else{var o=Ct(e,t,a,r.displayMode,!1),s=xt(e,a);n=je.makeSpan(["katex"],[o,s])}return Rt(n,r)},Et={widehat:"^",widecheck:"\u02c7",widetilde:"~",utilde:"~",overleftarrow:"\u2190",underleftarrow:"\u2190",xleftarrow:"\u2190",overrightarrow:"\u2192",underrightarrow:"\u2192",xrightarrow:"\u2192",underbrace:"\u23df",overbrace:"\u23de",overgroup:"\u23e0",undergroup:"\u23e1",overleftrightarrow:"\u2194",underleftrightarrow:"\u2194",xleftrightarrow:"\u2194",Overrightarrow:"\u21d2",xRightarrow:"\u21d2",overleftharpoon:"\u21bc",xleftharpoonup:"\u21bc",overrightharpoon:"\u21c0",xrightharpoonup:"\u21c0",xLeftarrow:"\u21d0",xLeftrightarrow:"\u21d4",xhookleftarrow:"\u21a9",xhookrightarrow:"\u21aa",xmapsto:"\u21a6",xrightharpoondown:"\u21c1",xleftharpoondown:"\u21bd",xrightleftharpoons:"\u21cc",xleftrightharpoons:"\u21cb",xtwoheadleftarrow:"\u219e",xtwoheadrightarrow:"\u21a0",xlongequal:"=",xtofrom:"\u21c4",xrightleftarrows:"\u21c4",xrightequilibrium:"\u21cc",xleftequilibrium:"\u21cb","\\cdrightarrow":"\u2192","\\cdleftarrow":"\u2190","\\cdlongequal":"="},Ht={overrightarrow:[["rightarrow"],.888,522,"xMaxYMin"],overleftarrow:[["leftarrow"],.888,522,"xMinYMin"],underrightarrow:[["rightarrow"],.888,522,"xMaxYMin"],underleftarrow:[["leftarrow"],.888,522,"xMinYMin"],xrightarrow:[["rightarrow"],1.469,522,"xMaxYMin"],"\\cdrightarrow":[["rightarrow"],3,522,"xMaxYMin"],xleftarrow:[["leftarrow"],1.469,522,"xMinYMin"],"\\cdleftarrow":[["leftarrow"],3,522,"xMinYMin"],Overrightarrow:[["doublerightarrow"],.888,560,"xMaxYMin"],xRightarrow:[["doublerightarrow"],1.526,560,"xMaxYMin"],xLeftarrow:[["doubleleftarrow"],1.526,560,"xMinYMin"],overleftharpoon:[["leftharpoon"],.888,522,"xMinYMin"],xleftharpoonup:[["leftharpoon"],.888,522,"xMinYMin"],xleftharpoondown:[["leftharpoondown"],.888,522,"xMinYMin"],overrightharpoon:[["rightharpoon"],.888,522,"xMaxYMin"],xrightharpoonup:[["rightharpoon"],.888,522,"xMaxYMin"],xrightharpoondown:[["rightharpoondown"],.888,522,"xMaxYMin"],xlongequal:[["longequal"],.888,334,"xMinYMin"],"\\cdlongequal":[["longequal"],3,334,"xMinYMin"],xtwoheadleftarrow:[["twoheadleftarrow"],.888,334,"xMinYMin"],xtwoheadrightarrow:[["twoheadrightarrow"],.888,334,"xMaxYMin"],overleftrightarrow:[["leftarrow","rightarrow"],.888,522],overbrace:[["leftbrace","midbrace","rightbrace"],1.6,548],underbrace:[["leftbraceunder","midbraceunder","rightbraceunder"],1.6,548],underleftrightarrow:[["leftarrow","rightarrow"],.888,522],xleftrightarrow:[["leftarrow","rightarrow"],1.75,522],xLeftrightarrow:[["doubleleftarrow","doublerightarrow"],1.75,560],xrightleftharpoons:[["leftharpoondownplus","rightharpoonplus"],1.75,716],xleftrightharpoons:[["leftharpoonplus","rightharpoondownplus"],1.75,716],xhookleftarrow:[["leftarrow","righthook"],1.08,522],xhookrightarrow:[["lefthook","rightarrow"],1.08,522],overlinesegment:[["leftlinesegment","rightlinesegment"],.888,522],underlinesegment:[["leftlinesegment","rightlinesegment"],.888,522],overgroup:[["leftgroup","rightgroup"],.888,342],undergroup:[["leftgroupunder","rightgroupunder"],.888,342],xmapsto:[["leftmapsto","rightarrow"],1.5,522],xtofrom:[["leftToFrom","rightToFrom"],1.75,528],xrightleftarrows:[["baraboveleftarrow","rightarrowabovebar"],1.75,901],xrightequilibrium:[["baraboveshortleftharpoon","rightharpoonaboveshortbar"],1.75,716],xleftequilibrium:[["shortbaraboveleftharpoon","shortrightharpoonabovebar"],1.75,716]},Lt=function(e,t,r,n,a){var i,o=e.height+e.depth+r+n;if(/fbox|color|angl/.test(t)){if(i=je.makeSpan(["stretchy",t],[],a),"fbox"===t){var s=a.color&&a.getColor();s&&(i.style.borderColor=s)}}else{var l=[];/^[bx]cancel$/.test(t)&&l.push(new H({x1:"0",y1:"0",x2:"100%",y2:"100%","stroke-width":"0.046em"})),/^x?cancel$/.test(t)&&l.push(new H({x1:"0",y1:"100%",x2:"100%",y2:"0","stroke-width":"0.046em"}));var h=new O(l,{width:"100%",height:o+"em"});i=je.makeSvgSpan([],[h],a)}return i.height=o,i.style.height=o+"em",i},Dt=function(e){var t=new Mt.MathNode("mo",[new Mt.TextNode(Et[e.replace(/^\\/,"")])]);return t.setAttribute("stretchy","true"),t},Pt=function(e,t){var r=function(){var r=4e5,n=e.label.substr(1);if(l.contains(["widehat","widecheck","widetilde","utilde"],n)){var a,i,o,s="ordgroup"===(d=e.base).type?d.body.length:1;if(s>5)"widehat"===n||"widecheck"===n?(a=420,r=2364,o=.42,i=n+"4"):(a=312,r=2340,o=.34,i="tilde4");else{var h=[1,1,2,2,3,3][s];"widehat"===n||"widecheck"===n?(r=[0,1062,2364,2364,2364][h],a=[0,239,300,360,420][h],o=[0,.24,.3,.3,.36,.42][h],i=n+h):(r=[0,600,1033,2339,2340][h],a=[0,260,286,306,312][h],o=[0,.26,.286,.3,.306,.34][h],i="tilde"+h)}var m=new E(i),c=new O([m],{width:"100%",height:o+"em",viewBox:"0 0 "+r+" "+a,preserveAspectRatio:"none"});return{span:je.makeSvgSpan([],[c],t),minWidth:0,height:o}}var u,p,d,f=[],g=Ht[n],v=g[0],b=g[1],y=g[2],x=y/1e3,w=v.length;if(1===w)u=["hide-tail"],p=[g[3]];else if(2===w)u=["halfarrow-left","halfarrow-right"],p=["xMinYMin","xMaxYMin"];else{if(3!==w)throw new Error("Correct katexImagesData or update code here to support\n "+w+" children.");u=["brace-left","brace-center","brace-right"],p=["xMinYMin","xMidYMin","xMaxYMin"]}for(var k=0;k0&&(n.style.minWidth=a+"em"),n};function Ft(e,t){if(!e||e.type!==t)throw new Error("Expected node of type "+t+", but got "+(e?"node of type "+e.type:String(e)));return e}function Vt(e){var t=Gt(e);if(!t)throw new Error("Expected node of symbol group type, but got "+(e?"node of type "+e.type:String(e)));return t}function Gt(e){return e&&("atom"===e.type||Y.hasOwnProperty(e.type))?e:null}var Ut=function(e,t){var r,n,a;e&&"supsub"===e.type?(r=(n=Ft(e.base,"accent")).base,e.base=r,a=function(e){if(e instanceof N)return e;throw new Error("Expected span but got "+String(e)+".")}(bt(e,t)),e.base=n):r=(n=Ft(e,"accent")).base;var i=bt(r,t.havingCrampedStyle()),o=0;if(n.isShifty&&l.isCharacterBox(r)){var s=l.getBaseElem(r);o=L(bt(s,t.havingCrampedStyle())).skew}var h,m="\\c"===n.label,c=m?i.height+i.depth:Math.min(i.height,t.fontMetrics().xHeight);if(n.isStretchy)h=Pt(n,t),h=je.makeVList({positionType:"firstBaseline",children:[{type:"elem",elem:i},{type:"elem",elem:h,wrapperClasses:["svg-align"],wrapperStyle:o>0?{width:"calc(100% - "+2*o+"em)",marginLeft:2*o+"em"}:void 0}]},t);else{var u,p;"\\vec"===n.label?(u=je.staticSvg("vec",t),p=je.svgData.vec[1]):((u=L(u=je.makeOrd({mode:n.mode,text:n.label},t,"textord"))).italic=0,p=u.width,m&&(c+=u.depth)),h=je.makeSpan(["accent-body"],[u]);var d="\\textcircled"===n.label;d&&(h.classes.push("accent-full"),c=i.height);var f=o;d||(f-=p/2),h.style.left=f+"em","\\textcircled"===n.label&&(h.style.top=".2em"),h=je.makeVList({positionType:"firstBaseline",children:[{type:"elem",elem:i},{type:"kern",size:-c},{type:"elem",elem:h}]},t)}var g=je.makeSpan(["mord","accent"],[h],t);return a?(a.children[0]=g,a.height=Math.max(g.height,a.height),a.classes[0]="mord",a):g},Yt=function(e,t){var r=e.isStretchy?Dt(e.label):new Mt.MathNode("mo",[zt(e.label,e.mode)]),n=new Mt.MathNode("mover",[qt(e.base,t),r]);return n.setAttribute("accent","true"),n},Wt=new RegExp(["\\acute","\\grave","\\ddot","\\tilde","\\bar","\\breve","\\check","\\hat","\\vec","\\dot","\\mathring"].map((function(e){return"\\"+e})).join("|"));nt({type:"accent",names:["\\acute","\\grave","\\ddot","\\tilde","\\bar","\\breve","\\check","\\hat","\\vec","\\dot","\\mathring","\\widecheck","\\widehat","\\widetilde","\\overrightarrow","\\overleftarrow","\\Overrightarrow","\\overleftrightarrow","\\overgroup","\\overlinesegment","\\overleftharpoon","\\overrightharpoon"],props:{numArgs:1},handler:function(e,t){var r=it(t[0]),n=!Wt.test(e.funcName),a=!n||"\\widehat"===e.funcName||"\\widetilde"===e.funcName||"\\widecheck"===e.funcName;return{type:"accent",mode:e.parser.mode,label:e.funcName,isStretchy:n,isShifty:a,base:r}},htmlBuilder:Ut,mathmlBuilder:Yt}),nt({type:"accent",names:["\\'","\\`","\\^","\\~","\\=","\\u","\\.",'\\"',"\\c","\\r","\\H","\\v","\\textcircled"],props:{numArgs:1,allowedInText:!0,allowedInMath:!0,argTypes:["primitive"]},handler:function(e,t){var r=t[0],n=e.parser.mode;return"math"===n&&(e.parser.settings.reportNonstrict("mathVsTextAccents","LaTeX's accent "+e.funcName+" works only in text mode"),n="text"),{type:"accent",mode:n,label:e.funcName,isStretchy:!1,isShifty:!0,base:r}},htmlBuilder:Ut,mathmlBuilder:Yt}),nt({type:"accentUnder",names:["\\underleftarrow","\\underrightarrow","\\underleftrightarrow","\\undergroup","\\underlinesegment","\\utilde"],props:{numArgs:1},handler:function(e,t){var r=e.parser,n=e.funcName,a=t[0];return{type:"accentUnder",mode:r.mode,label:n,base:a}},htmlBuilder:function(e,t){var r=bt(e.base,t),n=Pt(e,t),a="\\utilde"===e.label?.12:0,i=je.makeVList({positionType:"top",positionData:r.height,children:[{type:"elem",elem:n,wrapperClasses:["svg-align"]},{type:"kern",size:a},{type:"elem",elem:r}]},t);return je.makeSpan(["mord","accentunder"],[i],t)},mathmlBuilder:function(e,t){var r=Dt(e.label),n=new Mt.MathNode("munder",[qt(e.base,t),r]);return n.setAttribute("accentunder","true"),n}});var Xt=function(e){var t=new Mt.MathNode("mpadded",e?[e]:[]);return t.setAttribute("width","+0.6em"),t.setAttribute("lspace","0.3em"),t};nt({type:"xArrow",names:["\\xleftarrow","\\xrightarrow","\\xLeftarrow","\\xRightarrow","\\xleftrightarrow","\\xLeftrightarrow","\\xhookleftarrow","\\xhookrightarrow","\\xmapsto","\\xrightharpoondown","\\xrightharpoonup","\\xleftharpoondown","\\xleftharpoonup","\\xrightleftharpoons","\\xleftrightharpoons","\\xlongequal","\\xtwoheadrightarrow","\\xtwoheadleftarrow","\\xtofrom","\\xrightleftarrows","\\xrightequilibrium","\\xleftequilibrium","\\\\cdrightarrow","\\\\cdleftarrow","\\\\cdlongequal"],props:{numArgs:1,numOptionalArgs:1},handler:function(e,t,r){var n=e.parser,a=e.funcName;return{type:"xArrow",mode:n.mode,label:a,body:t[0],below:r[0]}},htmlBuilder:function(e,t){var r,n=t.style,a=t.havingStyle(n.sup()),i=je.wrapFragment(bt(e.body,a,t),t),o="\\x"===e.label.slice(0,2)?"x":"cd";i.classes.push(o+"-arrow-pad"),e.below&&(a=t.havingStyle(n.sub()),(r=je.wrapFragment(bt(e.below,a,t),t)).classes.push(o+"-arrow-pad"));var s,l=Pt(e,t),h=-t.fontMetrics().axisHeight+.5*l.height,m=-t.fontMetrics().axisHeight-.5*l.height-.111;if((i.depth>.25||"\\xleftequilibrium"===e.label)&&(m-=i.depth),r){var c=-t.fontMetrics().axisHeight+r.height+.5*l.height+.111;s=je.makeVList({positionType:"individualShift",children:[{type:"elem",elem:i,shift:m},{type:"elem",elem:l,shift:h},{type:"elem",elem:r,shift:c}]},t)}else s=je.makeVList({positionType:"individualShift",children:[{type:"elem",elem:i,shift:m},{type:"elem",elem:l,shift:h}]},t);return s.children[0].children[0].children[1].classes.push("svg-align"),je.makeSpan(["mrel","x-arrow"],[s],t)},mathmlBuilder:function(e,t){var r,n=Dt(e.label);if(n.setAttribute("minsize","x"===e.label.charAt(0)?"1.75em":"3.0em"),e.body){var a=Xt(qt(e.body,t));if(e.below){var i=Xt(qt(e.below,t));r=new Mt.MathNode("munderover",[n,i,a])}else r=new Mt.MathNode("mover",[n,a])}else if(e.below){var o=Xt(qt(e.below,t));r=new Mt.MathNode("munder",[n,o])}else r=Xt(),r=new Mt.MathNode("mover",[n,r]);return r}});var _t={">":"\\\\cdrightarrow","<":"\\\\cdleftarrow","=":"\\\\cdlongequal",A:"\\uparrow",V:"\\downarrow","|":"\\Vert",".":"no arrow"},jt=function(e){return"textord"===e.type&&"@"===e.text};function $t(e,t,r){var n=_t[e];switch(n){case"\\\\cdrightarrow":case"\\\\cdleftarrow":return r.callFunction(n,[t[0]],[t[1]]);case"\\uparrow":case"\\downarrow":var a={type:"atom",text:n,mode:"math",family:"rel"},i={type:"ordgroup",mode:"math",body:[r.callFunction("\\\\cdleft",[t[0]],[]),r.callFunction("\\Big",[a],[]),r.callFunction("\\\\cdright",[t[1]],[])]};return r.callFunction("\\\\cdparent",[i],[]);case"\\\\cdlongequal":return r.callFunction("\\\\cdlongequal",[],[]);case"\\Vert":return r.callFunction("\\Big",[{type:"textord",text:"\\Vert",mode:"math"}],[]);default:return{type:"textord",text:" ",mode:"math"}}}nt({type:"cdlabel",names:["\\\\cdleft","\\\\cdright"],props:{numArgs:1},handler:function(e,t){var r=e.parser,n=e.funcName;return{type:"cdlabel",mode:r.mode,side:n.slice(4),label:t[0]}},htmlBuilder:function(e,t){var r=t.havingStyle(t.style.sup()),n=je.wrapFragment(bt(e.label,r,t),t);return n.classes.push("cd-label-"+e.side),n.style.bottom=.8-n.depth+"em",n.height=0,n.depth=0,n},mathmlBuilder:function(e,t){var r=new Mt.MathNode("mrow",[qt(e.label,t)]);return(r=new Mt.MathNode("mpadded",[r])).setAttribute("width","0"),"left"===e.side&&r.setAttribute("lspace","-1width"),r.setAttribute("voffset","0.7em"),(r=new Mt.MathNode("mstyle",[r])).setAttribute("displaystyle","false"),r.setAttribute("scriptlevel","1"),r}}),nt({type:"cdlabelparent",names:["\\\\cdparent"],props:{numArgs:1},handler:function(e,t){return{type:"cdlabelparent",mode:e.parser.mode,fragment:t[0]}},htmlBuilder:function(e,t){var r=je.wrapFragment(bt(e.fragment,t),t);return r.classes.push("cd-vert-arrow"),r},mathmlBuilder:function(e,t){return new Mt.MathNode("mrow",[qt(e.fragment,t)])}}),nt({type:"textord",names:["\\@char"],props:{numArgs:1,allowedInText:!0},handler:function(e,t){for(var r=e.parser,a=Ft(t[0],"ordgroup").body,i="",o=0;o=1114111)throw new n("\\@char with invalid code point "+i);return l<=65535?s=String.fromCharCode(l):(l-=65536,s=String.fromCharCode(55296+(l>>10),56320+(1023&l))),{type:"textord",mode:r.mode,text:s}}});var Zt=function(e,t){var r=ut(e.body,t.withColor(e.color),!1);return je.makeFragment(r)},Kt=function(e,t){var r=Bt(e.body,t.withColor(e.color)),n=new Mt.MathNode("mstyle",r);return n.setAttribute("mathcolor",e.color),n};nt({type:"color",names:["\\textcolor"],props:{numArgs:2,allowedInText:!0,argTypes:["color","original"]},handler:function(e,t){var r=e.parser,n=Ft(t[0],"color-token").color,a=t[1];return{type:"color",mode:r.mode,color:n,body:ot(a)}},htmlBuilder:Zt,mathmlBuilder:Kt}),nt({type:"color",names:["\\color"],props:{numArgs:1,allowedInText:!0,argTypes:["color"]},handler:function(e,t){var r=e.parser,n=e.breakOnTokenText,a=Ft(t[0],"color-token").color;r.gullet.macros.set("\\current@color",a);var i=r.parseExpression(!0,n);return{type:"color",mode:r.mode,color:a,body:i}},htmlBuilder:Zt,mathmlBuilder:Kt}),nt({type:"cr",names:["\\\\"],props:{numArgs:0,numOptionalArgs:1,argTypes:["size"],allowedInText:!0},handler:function(e,t,r){var n=e.parser,a=r[0],i=!n.settings.displayMode||!n.settings.useStrictBehavior("newLineInDisplayMode","In LaTeX, \\\\ or \\newline does nothing in display mode");return{type:"cr",mode:n.mode,newLine:i,size:a&&Ft(a,"size").value}},htmlBuilder:function(e,t){var r=je.makeSpan(["mspace"],[],t);return e.newLine&&(r.classes.push("newline"),e.size&&(r.style.marginTop=Le(e.size,t)+"em")),r},mathmlBuilder:function(e,t){var r=new Mt.MathNode("mspace");return e.newLine&&(r.setAttribute("linebreak","newline"),e.size&&r.setAttribute("height",Le(e.size,t)+"em")),r}});var Jt={"\\global":"\\global","\\long":"\\\\globallong","\\\\globallong":"\\\\globallong","\\def":"\\gdef","\\gdef":"\\gdef","\\edef":"\\xdef","\\xdef":"\\xdef","\\let":"\\\\globallet","\\futurelet":"\\\\globalfuture"},Qt=function(e){var t=e.text;if(/^(?:[\\{}$&#^_]|EOF)$/.test(t))throw new n("Expected a control sequence",e);return t},er=function(e,t,r,n){var a=e.gullet.macros.get(r.text);null==a&&(r.noexpand=!0,a={tokens:[r],numArgs:0,unexpandable:!e.gullet.isExpandable(r.text)}),e.gullet.macros.set(t,a,n)};nt({type:"internal",names:["\\global","\\long","\\\\globallong"],props:{numArgs:0,allowedInText:!0},handler:function(e){var t=e.parser,r=e.funcName;t.consumeSpaces();var a=t.fetch();if(Jt[a.text])return"\\global"!==r&&"\\\\globallong"!==r||(a.text=Jt[a.text]),Ft(t.parseFunction(),"internal");throw new n("Invalid token after macro prefix",a)}}),nt({type:"internal",names:["\\def","\\gdef","\\edef","\\xdef"],props:{numArgs:0,allowedInText:!0,primitive:!0},handler:function(e){var t=e.parser,r=e.funcName,a=t.gullet.popToken(),i=a.text;if(/^(?:[\\{}$&#^_]|EOF)$/.test(i))throw new n("Expected a control sequence",a);for(var o,s=0,l=[[]];"{"!==t.gullet.future().text;)if("#"===(a=t.gullet.popToken()).text){if("{"===t.gullet.future().text){o=t.gullet.future(),l[s].push("{");break}if(a=t.gullet.popToken(),!/^[1-9]$/.test(a.text))throw new n('Invalid argument number "'+a.text+'"');if(parseInt(a.text)!==s+1)throw new n('Argument number "'+a.text+'" out of order');s++,l.push([])}else{if("EOF"===a.text)throw new n("Expected a macro definition");l[s].push(a.text)}var h=t.gullet.consumeArg().tokens;return o&&h.unshift(o),"\\edef"!==r&&"\\xdef"!==r||(h=t.gullet.expandTokens(h)).reverse(),t.gullet.macros.set(i,{tokens:h,numArgs:s,delimiters:l},r===Jt[r]),{type:"internal",mode:t.mode}}}),nt({type:"internal",names:["\\let","\\\\globallet"],props:{numArgs:0,allowedInText:!0,primitive:!0},handler:function(e){var t=e.parser,r=e.funcName,n=Qt(t.gullet.popToken());t.gullet.consumeSpaces();var a=function(e){var t=e.gullet.popToken();return"="===t.text&&" "===(t=e.gullet.popToken()).text&&(t=e.gullet.popToken()),t}(t);return er(t,n,a,"\\\\globallet"===r),{type:"internal",mode:t.mode}}}),nt({type:"internal",names:["\\futurelet","\\\\globalfuture"],props:{numArgs:0,allowedInText:!0,primitive:!0},handler:function(e){var t=e.parser,r=e.funcName,n=Qt(t.gullet.popToken()),a=t.gullet.popToken(),i=t.gullet.popToken();return er(t,n,i,"\\\\globalfuture"===r),t.gullet.pushToken(i),t.gullet.pushToken(a),{type:"internal",mode:t.mode}}});var tr=function(e,t,r){var n=V(X.math[e]&&X.math[e].replace||e,t,r);if(!n)throw new Error("Unsupported symbol "+e+" and font size "+t+".");return n},rr=function(e,t,r,n){var a=r.havingBaseStyle(t),i=je.makeSpan(n.concat(a.sizingClasses(r)),[e],r),o=a.sizeMultiplier/r.sizeMultiplier;return i.height*=o,i.depth*=o,i.maxFontSize=a.sizeMultiplier,i},nr=function(e,t,r){var n=t.havingBaseStyle(r),a=(1-t.sizeMultiplier/n.sizeMultiplier)*t.fontMetrics().axisHeight;e.classes.push("delimcenter"),e.style.top=a+"em",e.height-=a,e.depth+=a},ar=function(e,t,r,n,a,i){var o=function(e,t,r,n){return je.makeSymbol(e,"Size"+t+"-Regular",r,n)}(e,t,a,n),s=rr(je.makeSpan(["delimsizing","size"+t],[o],n),b.TEXT,n,i);return r&&nr(s,n,b.TEXT),s},ir=function(e,t,r){var n;return n="Size1-Regular"===t?"delim-size1":"delim-size4",{type:"elem",elem:je.makeSpan(["delimsizinginner",n],[je.makeSpan([],[je.makeSymbol(e,t,r)])])}},or=function(e,t,r){var n=D["Size4-Regular"][e.charCodeAt(0)]?D["Size4-Regular"][e.charCodeAt(0)][4].toFixed(3):D["Size1-Regular"][e.charCodeAt(0)][4].toFixed(3),a=new E("inner",function(e,t){switch(e){case"\u239c":return"M291 0 H417 V"+t+" H291z M291 0 H417 V"+t+" H291z";case"\u2223":return"M145 0 H188 V"+t+" H145z M145 0 H188 V"+t+" H145z";case"\u2225":return"M145 0 H188 V"+t+" H145z M145 0 H188 V"+t+" H145zM367 0 H410 V"+t+" H367z M367 0 H410 V"+t+" H367z";case"\u239f":return"M457 0 H583 V"+t+" H457z M457 0 H583 V"+t+" H457z";case"\u23a2":return"M319 0 H403 V"+t+" H319z M319 0 H403 V"+t+" H319z";case"\u23a5":return"M263 0 H347 V"+t+" H263z M263 0 H347 V"+t+" H263z";case"\u23aa":return"M384 0 H504 V"+t+" H384z M384 0 H504 V"+t+" H384z";case"\u23d0":return"M312 0 H355 V"+t+" H312z M312 0 H355 V"+t+" H312z";case"\u2016":return"M257 0 H300 V"+t+" H257z M257 0 H300 V"+t+" H257zM478 0 H521 V"+t+" H478z M478 0 H521 V"+t+" H478z";default:return""}}(e,Math.round(1e3*t))),i=new O([a],{width:n+"em",height:t+"em",style:"width:"+n+"em",viewBox:"0 0 "+1e3*n+" "+Math.round(1e3*t),preserveAspectRatio:"xMinYMin"}),o=je.makeSvgSpan([],[i],r);return o.height=t,o.style.height=t+"em",o.style.width=n+"em",{type:"elem",elem:o}},sr={type:"kern",size:-.008},lr=["|","\\lvert","\\rvert","\\vert"],hr=["\\|","\\lVert","\\rVert","\\Vert"],mr=function(e,t,r,n,a,i){var o,s,h,m;o=h=m=e,s=null;var c="Size1-Regular";"\\uparrow"===e?h=m="\u23d0":"\\Uparrow"===e?h=m="\u2016":"\\downarrow"===e?o=h="\u23d0":"\\Downarrow"===e?o=h="\u2016":"\\updownarrow"===e?(o="\\uparrow",h="\u23d0",m="\\downarrow"):"\\Updownarrow"===e?(o="\\Uparrow",h="\u2016",m="\\Downarrow"):l.contains(lr,e)?h="\u2223":l.contains(hr,e)?h="\u2225":"["===e||"\\lbrack"===e?(o="\u23a1",h="\u23a2",m="\u23a3",c="Size4-Regular"):"]"===e||"\\rbrack"===e?(o="\u23a4",h="\u23a5",m="\u23a6",c="Size4-Regular"):"\\lfloor"===e||"\u230a"===e?(h=o="\u23a2",m="\u23a3",c="Size4-Regular"):"\\lceil"===e||"\u2308"===e?(o="\u23a1",h=m="\u23a2",c="Size4-Regular"):"\\rfloor"===e||"\u230b"===e?(h=o="\u23a5",m="\u23a6",c="Size4-Regular"):"\\rceil"===e||"\u2309"===e?(o="\u23a4",h=m="\u23a5",c="Size4-Regular"):"("===e||"\\lparen"===e?(o="\u239b",h="\u239c",m="\u239d",c="Size4-Regular"):")"===e||"\\rparen"===e?(o="\u239e",h="\u239f",m="\u23a0",c="Size4-Regular"):"\\{"===e||"\\lbrace"===e?(o="\u23a7",s="\u23a8",m="\u23a9",h="\u23aa",c="Size4-Regular"):"\\}"===e||"\\rbrace"===e?(o="\u23ab",s="\u23ac",m="\u23ad",h="\u23aa",c="Size4-Regular"):"\\lgroup"===e||"\u27ee"===e?(o="\u23a7",m="\u23a9",h="\u23aa",c="Size4-Regular"):"\\rgroup"===e||"\u27ef"===e?(o="\u23ab",m="\u23ad",h="\u23aa",c="Size4-Regular"):"\\lmoustache"===e||"\u23b0"===e?(o="\u23a7",m="\u23ad",h="\u23aa",c="Size4-Regular"):"\\rmoustache"!==e&&"\u23b1"!==e||(o="\u23ab",m="\u23a9",h="\u23aa",c="Size4-Regular");var u=tr(o,c,a),p=u.height+u.depth,d=tr(h,c,a),f=d.height+d.depth,g=tr(m,c,a),v=g.height+g.depth,y=0,x=1;if(null!==s){var w=tr(s,c,a);y=w.height+w.depth,x=2}var k=p+v+y,S=k+Math.max(0,Math.ceil((t-k)/(x*f)))*x*f,M=n.fontMetrics().axisHeight;r&&(M*=n.sizeMultiplier);var z=S/2-M,A=[];if(A.push(ir(m,c,a)),A.push(sr),null===s){var T=S-p-v+.016;A.push(or(h,T,n))}else{var B=(S-p-v-y)/2+.016;A.push(or(h,B,n)),A.push(sr),A.push(ir(s,c,a)),A.push(sr),A.push(or(h,B,n))}A.push(sr),A.push(ir(o,c,a));var N=n.havingBaseStyle(b.TEXT),q=je.makeVList({positionType:"bottom",positionData:z,children:A},N);return rr(je.makeSpan(["delimsizing","mult"],[q],N),b.TEXT,n,i)},cr=.08,ur=function(e,t,r,n,a){var i=function(e,t,r){t*=1e3;var n="";switch(e){case"sqrtMain":n=function(e,t){return"M95,"+(622+e+t)+"\nc-2.7,0,-7.17,-2.7,-13.5,-8c-5.8,-5.3,-9.5,-10,-9.5,-14\nc0,-2,0.3,-3.3,1,-4c1.3,-2.7,23.83,-20.7,67.5,-54\nc44.2,-33.3,65.8,-50.3,66.5,-51c1.3,-1.3,3,-2,5,-2c4.7,0,8.7,3.3,12,10\ns173,378,173,378c0.7,0,35.3,-71,104,-213c68.7,-142,137.5,-285,206.5,-429\nc69,-144,104.5,-217.7,106.5,-221\nl"+e/2.075+" -"+e+"\nc5.3,-9.3,12,-14,20,-14\nH400000v"+(40+e)+"H845.2724\ns-225.272,467,-225.272,467s-235,486,-235,486c-2.7,4.7,-9,7,-19,7\nc-6,0,-10,-1,-12,-3s-194,-422,-194,-422s-65,47,-65,47z\nM"+(834+e)+" "+t+"h400000v"+(40+e)+"h-400000z"}(t,k);break;case"sqrtSize1":n=function(e,t){return"M263,"+(601+e+t)+"c0.7,0,18,39.7,52,119\nc34,79.3,68.167,158.7,102.5,238c34.3,79.3,51.8,119.3,52.5,120\nc340,-704.7,510.7,-1060.3,512,-1067\nl"+e/2.084+" -"+e+"\nc4.7,-7.3,11,-11,19,-11\nH40000v"+(40+e)+"H1012.3\ns-271.3,567,-271.3,567c-38.7,80.7,-84,175,-136,283c-52,108,-89.167,185.3,-111.5,232\nc-22.3,46.7,-33.8,70.3,-34.5,71c-4.7,4.7,-12.3,7,-23,7s-12,-1,-12,-1\ns-109,-253,-109,-253c-72.7,-168,-109.3,-252,-110,-252c-10.7,8,-22,16.7,-34,26\nc-22,17.3,-33.3,26,-34,26s-26,-26,-26,-26s76,-59,76,-59s76,-60,76,-60z\nM"+(1001+e)+" "+t+"h400000v"+(40+e)+"h-400000z"}(t,k);break;case"sqrtSize2":n=function(e,t){return"M983 "+(10+e+t)+"\nl"+e/3.13+" -"+e+"\nc4,-6.7,10,-10,18,-10 H400000v"+(40+e)+"\nH1013.1s-83.4,268,-264.1,840c-180.7,572,-277,876.3,-289,913c-4.7,4.7,-12.7,7,-24,7\ns-12,0,-12,0c-1.3,-3.3,-3.7,-11.7,-7,-25c-35.3,-125.3,-106.7,-373.3,-214,-744\nc-10,12,-21,25,-33,39s-32,39,-32,39c-6,-5.3,-15,-14,-27,-26s25,-30,25,-30\nc26.7,-32.7,52,-63,76,-91s52,-60,52,-60s208,722,208,722\nc56,-175.3,126.3,-397.3,211,-666c84.7,-268.7,153.8,-488.2,207.5,-658.5\nc53.7,-170.3,84.5,-266.8,92.5,-289.5z\nM"+(1001+e)+" "+t+"h400000v"+(40+e)+"h-400000z"}(t,k);break;case"sqrtSize3":n=function(e,t){return"M424,"+(2398+e+t)+"\nc-1.3,-0.7,-38.5,-172,-111.5,-514c-73,-342,-109.8,-513.3,-110.5,-514\nc0,-2,-10.7,14.3,-32,49c-4.7,7.3,-9.8,15.7,-15.5,25c-5.7,9.3,-9.8,16,-12.5,20\ns-5,7,-5,7c-4,-3.3,-8.3,-7.7,-13,-13s-13,-13,-13,-13s76,-122,76,-122s77,-121,77,-121\ns209,968,209,968c0,-2,84.7,-361.7,254,-1079c169.3,-717.3,254.7,-1077.7,256,-1081\nl"+e/4.223+" -"+e+"c4,-6.7,10,-10,18,-10 H400000\nv"+(40+e)+"H1014.6\ns-87.3,378.7,-272.6,1166c-185.3,787.3,-279.3,1182.3,-282,1185\nc-2,6,-10,9,-24,9\nc-8,0,-12,-0.7,-12,-2z M"+(1001+e)+" "+t+"\nh400000v"+(40+e)+"h-400000z"}(t,k);break;case"sqrtSize4":n=function(e,t){return"M473,"+(2713+e+t)+"\nc339.3,-1799.3,509.3,-2700,510,-2702 l"+e/5.298+" -"+e+"\nc3.3,-7.3,9.3,-11,18,-11 H400000v"+(40+e)+"H1017.7\ns-90.5,478,-276.2,1466c-185.7,988,-279.5,1483,-281.5,1485c-2,6,-10,9,-24,9\nc-8,0,-12,-0.7,-12,-2c0,-1.3,-5.3,-32,-16,-92c-50.7,-293.3,-119.7,-693.3,-207,-1200\nc0,-1.3,-5.3,8.7,-16,30c-10.7,21.3,-21.3,42.7,-32,64s-16,33,-16,33s-26,-26,-26,-26\ns76,-153,76,-153s77,-151,77,-151c0.7,0.7,35.7,202,105,604c67.3,400.7,102,602.7,104,\n606zM"+(1001+e)+" "+t+"h400000v"+(40+e)+"H1017.7z"}(t,k);break;case"sqrtTall":n=function(e,t,r){return"M702 "+(e+t)+"H400000"+(40+e)+"\nH742v"+(r-54-t-e)+"l-4 4-4 4c-.667.7 -2 1.5-4 2.5s-4.167 1.833-6.5 2.5-5.5 1-9.5 1\nh-12l-28-84c-16.667-52-96.667 -294.333-240-727l-212 -643 -85 170\nc-4-3.333-8.333-7.667-13 -13l-13-13l77-155 77-156c66 199.333 139 419.667\n219 661 l218 661zM702 "+t+"H400000v"+(40+e)+"H742z"}(t,k,r)}return n}(e,n,r),o=new E(e,i),s=new O([o],{width:"400em",height:t+"em",viewBox:"0 0 400000 "+r,preserveAspectRatio:"xMinYMin slice"});return je.makeSvgSpan(["hide-tail"],[s],a)},pr=["(","\\lparen",")","\\rparen","[","\\lbrack","]","\\rbrack","\\{","\\lbrace","\\}","\\rbrace","\\lfloor","\\rfloor","\u230a","\u230b","\\lceil","\\rceil","\u2308","\u2309","\\surd"],dr=["\\uparrow","\\downarrow","\\updownarrow","\\Uparrow","\\Downarrow","\\Updownarrow","|","\\|","\\vert","\\Vert","\\lvert","\\rvert","\\lVert","\\rVert","\\lgroup","\\rgroup","\u27ee","\u27ef","\\lmoustache","\\rmoustache","\u23b0","\u23b1"],fr=["<",">","\\langle","\\rangle","/","\\backslash","\\lt","\\gt"],gr=[0,1.2,1.8,2.4,3],vr=[{type:"small",style:b.SCRIPTSCRIPT},{type:"small",style:b.SCRIPT},{type:"small",style:b.TEXT},{type:"large",size:1},{type:"large",size:2},{type:"large",size:3},{type:"large",size:4}],br=[{type:"small",style:b.SCRIPTSCRIPT},{type:"small",style:b.SCRIPT},{type:"small",style:b.TEXT},{type:"stack"}],yr=[{type:"small",style:b.SCRIPTSCRIPT},{type:"small",style:b.SCRIPT},{type:"small",style:b.TEXT},{type:"large",size:1},{type:"large",size:2},{type:"large",size:3},{type:"large",size:4},{type:"stack"}],xr=function(e){if("small"===e.type)return"Main-Regular";if("large"===e.type)return"Size"+e.size+"-Regular";if("stack"===e.type)return"Size4-Regular";throw new Error("Add support for delim type '"+e.type+"' here.")},wr=function(e,t,r,n){for(var a=Math.min(2,3-n.style.size);at)return r[a]}return r[r.length-1]},kr=function(e,t,r,n,a,i){var o;"<"===e||"\\lt"===e||"\u27e8"===e?e="\\langle":">"!==e&&"\\gt"!==e&&"\u27e9"!==e||(e="\\rangle"),o=l.contains(fr,e)?vr:l.contains(pr,e)?yr:br;var s=wr(e,t,o,n);return"small"===s.type?function(e,t,r,n,a,i){var o=je.makeSymbol(e,"Main-Regular",a,n),s=rr(o,t,n,i);return r&&nr(s,n,t),s}(e,s.style,r,n,a,i):"large"===s.type?ar(e,s.size,r,n,a,i):mr(e,t,r,n,a,i)},Sr=function(e,t){var r,n,a=t.havingBaseSizing(),i=wr("\\surd",e*a.sizeMultiplier,yr,a),o=a.sizeMultiplier,s=Math.max(0,t.minRuleThickness-t.fontMetrics().sqrtRuleThickness),l=0,h=0,m=0;return"small"===i.type?(e<1?o=1:e<1.4&&(o=.7),h=(1+s)/o,(r=ur("sqrtMain",l=(1+s+cr)/o,m=1e3+1e3*s+80,s,t)).style.minWidth="0.853em",n=.833/o):"large"===i.type?(m=1080*gr[i.size],h=(gr[i.size]+s)/o,l=(gr[i.size]+s+cr)/o,(r=ur("sqrtSize"+i.size,l,m,s,t)).style.minWidth="1.02em",n=1/o):(l=e+s+cr,h=e+s,m=Math.floor(1e3*e+s)+80,(r=ur("sqrtTall",l,m,s,t)).style.minWidth="0.742em",n=1.056),r.height=h,r.style.height=l+"em",{span:r,advanceWidth:n,ruleWidth:(t.fontMetrics().sqrtRuleThickness+s)*o}},Mr=function(e,t,r,a,i){if("<"===e||"\\lt"===e||"\u27e8"===e?e="\\langle":">"!==e&&"\\gt"!==e&&"\u27e9"!==e||(e="\\rangle"),l.contains(pr,e)||l.contains(fr,e))return ar(e,t,!1,r,a,i);if(l.contains(dr,e))return mr(e,gr[t],!1,r,a,i);throw new n("Illegal delimiter: '"+e+"'")},zr=gr,Ar=kr,Tr=function(e,t,r,n,a,i){var o=n.fontMetrics().axisHeight*n.sizeMultiplier,s=5/n.fontMetrics().ptPerEm,l=Math.max(t-o,r+o),h=Math.max(l/500*901,2*l-s);return kr(e,h,!0,n,a,i)},Br={"\\bigl":{mclass:"mopen",size:1},"\\Bigl":{mclass:"mopen",size:2},"\\biggl":{mclass:"mopen",size:3},"\\Biggl":{mclass:"mopen",size:4},"\\bigr":{mclass:"mclose",size:1},"\\Bigr":{mclass:"mclose",size:2},"\\biggr":{mclass:"mclose",size:3},"\\Biggr":{mclass:"mclose",size:4},"\\bigm":{mclass:"mrel",size:1},"\\Bigm":{mclass:"mrel",size:2},"\\biggm":{mclass:"mrel",size:3},"\\Biggm":{mclass:"mrel",size:4},"\\big":{mclass:"mord",size:1},"\\Big":{mclass:"mord",size:2},"\\bigg":{mclass:"mord",size:3},"\\Bigg":{mclass:"mord",size:4}},Nr=["(","\\lparen",")","\\rparen","[","\\lbrack","]","\\rbrack","\\{","\\lbrace","\\}","\\rbrace","\\lfloor","\\rfloor","\u230a","\u230b","\\lceil","\\rceil","\u2308","\u2309","<",">","\\langle","\u27e8","\\rangle","\u27e9","\\lt","\\gt","\\lvert","\\rvert","\\lVert","\\rVert","\\lgroup","\\rgroup","\u27ee","\u27ef","\\lmoustache","\\rmoustache","\u23b0","\u23b1","/","\\backslash","|","\\vert","\\|","\\Vert","\\uparrow","\\Uparrow","\\downarrow","\\Downarrow","\\updownarrow","\\Updownarrow","."];function qr(e,t){var r=Gt(e);if(r&&l.contains(Nr,r.text))return r;throw new n(r?"Invalid delimiter '"+r.text+"' after '"+t.funcName+"'":"Invalid delimiter type '"+e.type+"'",e)}function Cr(e){if(!e.body)throw new Error("Bug: The leftright ParseNode wasn't fully parsed.")}nt({type:"delimsizing",names:["\\bigl","\\Bigl","\\biggl","\\Biggl","\\bigr","\\Bigr","\\biggr","\\Biggr","\\bigm","\\Bigm","\\biggm","\\Biggm","\\big","\\Big","\\bigg","\\Bigg"],props:{numArgs:1,argTypes:["primitive"]},handler:function(e,t){var r=qr(t[0],e);return{type:"delimsizing",mode:e.parser.mode,size:Br[e.funcName].size,mclass:Br[e.funcName].mclass,delim:r.text}},htmlBuilder:function(e,t){return"."===e.delim?je.makeSpan([e.mclass]):Mr(e.delim,e.size,t,e.mode,[e.mclass])},mathmlBuilder:function(e){var t=[];"."!==e.delim&&t.push(zt(e.delim,e.mode));var r=new Mt.MathNode("mo",t);return"mopen"===e.mclass||"mclose"===e.mclass?r.setAttribute("fence","true"):r.setAttribute("fence","false"),r.setAttribute("stretchy","true"),r.setAttribute("minsize",zr[e.size]+"em"),r.setAttribute("maxsize",zr[e.size]+"em"),r}}),nt({type:"leftright-right",names:["\\right"],props:{numArgs:1,primitive:!0},handler:function(e,t){var r=e.parser.gullet.macros.get("\\current@color");if(r&&"string"!=typeof r)throw new n("\\current@color set to non-string in \\right");return{type:"leftright-right",mode:e.parser.mode,delim:qr(t[0],e).text,color:r}}}),nt({type:"leftright",names:["\\left"],props:{numArgs:1,primitive:!0},handler:function(e,t){var r=qr(t[0],e),n=e.parser;++n.leftrightDepth;var a=n.parseExpression(!1);--n.leftrightDepth,n.expect("\\right",!1);var i=Ft(n.parseFunction(),"leftright-right");return{type:"leftright",mode:n.mode,body:a,left:r.text,right:i.delim,rightColor:i.color}},htmlBuilder:function(e,t){Cr(e);for(var r,n,a=ut(e.body,t,!0,["mopen","mclose"]),i=0,o=0,s=!1,l=0;l-1?"mpadded":"menclose",[qt(e.body,t)]);switch(e.label){case"\\cancel":n.setAttribute("notation","updiagonalstrike");break;case"\\bcancel":n.setAttribute("notation","downdiagonalstrike");break;case"\\phase":n.setAttribute("notation","phasorangle");break;case"\\sout":n.setAttribute("notation","horizontalstrike");break;case"\\fbox":n.setAttribute("notation","box");break;case"\\angl":n.setAttribute("notation","actuarial");break;case"\\fcolorbox":case"\\colorbox":if(r=t.fontMetrics().fboxsep*t.fontMetrics().ptPerEm,n.setAttribute("width","+"+2*r+"pt"),n.setAttribute("height","+"+2*r+"pt"),n.setAttribute("lspace",r+"pt"),n.setAttribute("voffset",r+"pt"),"\\fcolorbox"===e.label){var a=Math.max(t.fontMetrics().fboxrule,t.minRuleThickness);n.setAttribute("style","border: "+a+"em solid "+String(e.borderColor))}break;case"\\xcancel":n.setAttribute("notation","updiagonalstrike downdiagonalstrike")}return e.backgroundColor&&n.setAttribute("mathbackground",e.backgroundColor),n};nt({type:"enclose",names:["\\colorbox"],props:{numArgs:2,allowedInText:!0,argTypes:["color","text"]},handler:function(e,t,r){var n=e.parser,a=e.funcName,i=Ft(t[0],"color-token").color,o=t[1];return{type:"enclose",mode:n.mode,label:a,backgroundColor:i,body:o}},htmlBuilder:Ir,mathmlBuilder:Rr}),nt({type:"enclose",names:["\\fcolorbox"],props:{numArgs:3,allowedInText:!0,argTypes:["color","color","text"]},handler:function(e,t,r){var n=e.parser,a=e.funcName,i=Ft(t[0],"color-token").color,o=Ft(t[1],"color-token").color,s=t[2];return{type:"enclose",mode:n.mode,label:a,backgroundColor:o,borderColor:i,body:s}},htmlBuilder:Ir,mathmlBuilder:Rr}),nt({type:"enclose",names:["\\fbox"],props:{numArgs:1,argTypes:["hbox"],allowedInText:!0},handler:function(e,t){return{type:"enclose",mode:e.parser.mode,label:"\\fbox",body:t[0]}}}),nt({type:"enclose",names:["\\cancel","\\bcancel","\\xcancel","\\sout","\\phase"],props:{numArgs:1},handler:function(e,t){var r=e.parser,n=e.funcName,a=t[0];return{type:"enclose",mode:r.mode,label:n,body:a}},htmlBuilder:Ir,mathmlBuilder:Rr}),nt({type:"enclose",names:["\\angl"],props:{numArgs:1,argTypes:["hbox"],allowedInText:!1},handler:function(e,t){return{type:"enclose",mode:e.parser.mode,label:"\\angl",body:t[0]}}});var Or={};function Er(e){for(var t=e.type,r=e.names,n=e.props,a=e.handler,i=e.htmlBuilder,o=e.mathmlBuilder,s={type:t,numArgs:n.numArgs||0,allowedInText:!1,numOptionalArgs:0,handler:a},l=0;l1||!c)&&g.pop(),b.length0&&(x+=.25),m.push({pos:x,isDashed:e[t]})}for(w(o[0]),r=0;r0&&(M<(B+=y)&&(M=B),B=0),e.addJot&&(M+=f),z.height=S,z.depth=M,x+=S,z.pos=x,x+=M+B,h[r]=z,w(o[r+1])}var N,q,C=x/2+t.fontMetrics().axisHeight,I=e.cols||[],R=[],O=[];if(e.addEqnNum)for(r=0;r=s)){var G=void 0;(a>0||e.hskipBeforeAndAfter)&&0!==(G=l.deflt(D.pregap,p))&&((N=je.makeSpan(["arraycolsep"],[])).style.width=G+"em",R.push(N));var U=[];for(r=0;r0){for(var _=je.makeLineSpan("hline",t,c),j=je.makeLineSpan("hdashline",t,c),$=[{type:"elem",elem:h,shift:0}];m.length>0;){var Z=m.pop(),K=Z.pos-C;Z.isDashed?$.push({type:"elem",elem:j,shift:K}):$.push({type:"elem",elem:_,shift:K})}h=je.makeVList({positionType:"individualShift",children:$},t)}if(e.addEqnNum){var J=je.makeVList({positionType:"individualShift",children:O},t);return J=je.makeSpan(["tag"],[J],t),je.makeFragment([h,J])}return je.makeSpan(["mord"],[h],t)},Vr={c:"center ",l:"left ",r:"right "},Gr=function(e,t){for(var r=[],n=new Mt.MathNode("mtd",[],["mtr-glue"]),a=new Mt.MathNode("mtd",[],["mml-eqn-num"]),i=0;i0){var p=e.cols,d="",f=!1,g=0,v=p.length;"separator"===p[0].type&&(c+="top ",g=1),"separator"===p[p.length-1].type&&(c+="bottom ",v-=1);for(var b=g;b0?"left ":"",c+=S[S.length-1].length>0?"right ":"";for(var M=1;M-1?"alignat":"align",o=Dr(e.parser,{cols:a,addJot:!0,addEqnNum:"align"===e.envName||"alignat"===e.envName,emptySingleRow:!0,colSeparationType:i,maxNumCols:"split"===e.envName?2:void 0,leqno:e.parser.settings.leqno},"display"),s=0,l={type:"ordgroup",mode:e.mode,body:[]};if(t[0]&&"ordgroup"===t[0].type){for(var h="",m=0;m0&&c&&(d=1),a[u]={type:"align",align:p,pregap:d,postgap:0}}return o.colSeparationType=c?"align":"alignat",o};Er({type:"array",names:["array","darray"],props:{numArgs:1},handler:function(e,t){var r=(Gt(t[0])?[t[0]]:Ft(t[0],"ordgroup").body).map((function(e){var t=Vt(e).text;if(-1!=="lcr".indexOf(t))return{type:"align",align:t};if("|"===t)return{type:"separator",separator:"|"};if(":"===t)return{type:"separator",separator:":"};throw new n("Unknown column alignment: "+t,e)})),a={cols:r,hskipBeforeAndAfter:!0,maxNumCols:r.length};return Dr(e.parser,a,Pr(e.envName))},htmlBuilder:Fr,mathmlBuilder:Gr}),Er({type:"array",names:["matrix","pmatrix","bmatrix","Bmatrix","vmatrix","Vmatrix","matrix*","pmatrix*","bmatrix*","Bmatrix*","vmatrix*","Vmatrix*"],props:{numArgs:0},handler:function(e){var t={matrix:null,pmatrix:["(",")"],bmatrix:["[","]"],Bmatrix:["\\{","\\}"],vmatrix:["|","|"],Vmatrix:["\\Vert","\\Vert"]}[e.envName.replace("*","")],r="c",a={hskipBeforeAndAfter:!1,cols:[{type:"align",align:r}]};if("*"===e.envName.charAt(e.envName.length-1)){var i=e.parser;if(i.consumeSpaces(),"["===i.fetch().text){if(i.consume(),i.consumeSpaces(),r=i.fetch().text,-1==="lcr".indexOf(r))throw new n("Expected l or c or r",i.nextToken);i.consume(),i.consumeSpaces(),i.expect("]"),i.consume(),a.cols=[{type:"align",align:r}]}}var o=Dr(e.parser,a,Pr(e.envName)),s=Math.max.apply(Math,[0].concat(o.body.map((function(e){return e.length}))));return o.cols=new Array(s).fill({type:"align",align:r}),t?{type:"leftright",mode:e.mode,body:[o],left:t[0],right:t[1],rightColor:void 0}:o},htmlBuilder:Fr,mathmlBuilder:Gr}),Er({type:"array",names:["smallmatrix"],props:{numArgs:0},handler:function(e){var t=Dr(e.parser,{arraystretch:.5},"script");return t.colSeparationType="small",t},htmlBuilder:Fr,mathmlBuilder:Gr}),Er({type:"array",names:["subarray"],props:{numArgs:1},handler:function(e,t){var r=(Gt(t[0])?[t[0]]:Ft(t[0],"ordgroup").body).map((function(e){var t=Vt(e).text;if(-1!=="lc".indexOf(t))return{type:"align",align:t};throw new n("Unknown column alignment: "+t,e)}));if(r.length>1)throw new n("{subarray} can contain only one column");var a={cols:r,hskipBeforeAndAfter:!1,arraystretch:.5};if((a=Dr(e.parser,a,"script")).body.length>0&&a.body[0].length>1)throw new n("{subarray} can contain only one column");return a},htmlBuilder:Fr,mathmlBuilder:Gr}),Er({type:"array",names:["cases","dcases","rcases","drcases"],props:{numArgs:0},handler:function(e){var t=Dr(e.parser,{arraystretch:1.2,cols:[{type:"align",align:"l",pregap:0,postgap:1},{type:"align",align:"l",pregap:0,postgap:0}]},Pr(e.envName));return{type:"leftright",mode:e.mode,body:[t],left:e.envName.indexOf("r")>-1?".":"\\{",right:e.envName.indexOf("r")>-1?"\\}":".",rightColor:void 0}},htmlBuilder:Fr,mathmlBuilder:Gr}),Er({type:"array",names:["align","align*","aligned","split"],props:{numArgs:0},handler:Ur,htmlBuilder:Fr,mathmlBuilder:Gr}),Er({type:"array",names:["gathered","gather","gather*"],props:{numArgs:0},handler:function(e){l.contains(["gather","gather*"],e.envName)&&Lr(e);var t={cols:[{type:"align",align:"c"}],addJot:!0,colSeparationType:"gather",addEqnNum:"gather"===e.envName,emptySingleRow:!0,leqno:e.parser.settings.leqno};return Dr(e.parser,t,"display")},htmlBuilder:Fr,mathmlBuilder:Gr}),Er({type:"array",names:["alignat","alignat*","alignedat"],props:{numArgs:1},handler:Ur,htmlBuilder:Fr,mathmlBuilder:Gr}),Er({type:"array",names:["equation","equation*"],props:{numArgs:0},handler:function(e){Lr(e);var t={addEqnNum:"equation"===e.envName,emptySingleRow:!0,singleRow:!0,maxNumCols:1,leqno:e.parser.settings.leqno};return Dr(e.parser,t,"display")},htmlBuilder:Fr,mathmlBuilder:Gr}),Er({type:"array",names:["CD"],props:{numArgs:0},handler:function(e){return Lr(e),function(e){var t=[];for(e.gullet.beginGroup(),e.gullet.macros.set("\\cr","\\\\\\relax"),e.gullet.beginGroup();;){t.push(e.parseExpression(!1,"\\\\")),e.gullet.endGroup(),e.gullet.beginGroup();var r=e.fetch().text;if("&"!==r&&"\\\\"!==r){if("\\end"===r){0===t[t.length-1].length&&t.pop();break}throw new n("Expected \\\\ or \\cr or \\end",e.nextToken)}e.consume()}for(var a,i,o=[],s=[o],l=0;l-1);else{if(!("<>AV".indexOf(u)>-1))throw new n('Expected one of "<>AV=|." after @',h[c]);for(var d=0;d<2;d++){for(var f=!0,g=c+1;g=b.SCRIPT.id?r.text():b.DISPLAY:"text"===e&&r.size===b.DISPLAY.size?r=b.TEXT:"script"===e?r=b.SCRIPT:"scriptscript"===e&&(r=b.SCRIPTSCRIPT),r},Qr=function(e,t){var r,n=Jr(e.size,t.style),a=n.fracNum(),i=n.fracDen();r=t.havingStyle(a);var o=bt(e.numer,r,t);if(e.continued){var s=8.5/t.fontMetrics().ptPerEm,l=3.5/t.fontMetrics().ptPerEm;o.height=o.height0?3*c:7*c,d=t.fontMetrics().denom1):(m>0?(u=t.fontMetrics().num2,p=c):(u=t.fontMetrics().num3,p=3*c),d=t.fontMetrics().denom2),h){var w=t.fontMetrics().axisHeight;u-o.depth-(w+.5*m)0&&(t="."===(t=e)?null:t),t};nt({type:"genfrac",names:["\\genfrac"],props:{numArgs:6,allowedInArgument:!0,argTypes:["math","math","size","text","math","math"]},handler:function(e,t){var r,n=e.parser,a=t[4],i=t[5],o=it(t[0]),s="atom"===o.type&&"open"===o.family?rn(o.text):null,l=it(t[1]),h="atom"===l.type&&"close"===l.family?rn(l.text):null,m=Ft(t[2],"size"),c=null;r=!!m.isBlank||(c=m.value).number>0;var u="auto",p=t[3];if("ordgroup"===p.type){if(p.body.length>0){var d=Ft(p.body[0],"textord");u=tn[Number(d.text)]}}else p=Ft(p,"textord"),u=tn[Number(p.text)];return{type:"genfrac",mode:n.mode,numer:a,denom:i,continued:!1,hasBarLine:r,barSize:c,leftDelim:s,rightDelim:h,size:u}},htmlBuilder:Qr,mathmlBuilder:en}),nt({type:"infix",names:["\\above"],props:{numArgs:1,argTypes:["size"],infix:!0},handler:function(e,t){var r=e.parser,n=(e.funcName,e.token);return{type:"infix",mode:r.mode,replaceWith:"\\\\abovefrac",size:Ft(t[0],"size").value,token:n}}}),nt({type:"genfrac",names:["\\\\abovefrac"],props:{numArgs:3,argTypes:["math","size","math"]},handler:function(e,t){var r=e.parser,n=(e.funcName,t[0]),a=function(e){if(!e)throw new Error("Expected non-null, but got "+String(e));return e}(Ft(t[1],"infix").size),i=t[2],o=a.number>0;return{type:"genfrac",mode:r.mode,numer:n,denom:i,continued:!1,hasBarLine:o,barSize:a,leftDelim:null,rightDelim:null,size:"auto"}},htmlBuilder:Qr,mathmlBuilder:en});var nn=function(e,t){var r,n,a=t.style;"supsub"===e.type?(r=e.sup?bt(e.sup,t.havingStyle(a.sup()),t):bt(e.sub,t.havingStyle(a.sub()),t),n=Ft(e.base,"horizBrace")):n=Ft(e,"horizBrace");var i,o=bt(n.base,t.havingBaseStyle(b.DISPLAY)),s=Pt(n,t);if(n.isOver?(i=je.makeVList({positionType:"firstBaseline",children:[{type:"elem",elem:o},{type:"kern",size:.1},{type:"elem",elem:s}]},t)).children[0].children[0].children[1].classes.push("svg-align"):(i=je.makeVList({positionType:"bottom",positionData:o.depth+.1+s.height,children:[{type:"elem",elem:s},{type:"kern",size:.1},{type:"elem",elem:o}]},t)).children[0].children[0].children[0].classes.push("svg-align"),r){var l=je.makeSpan(["mord",n.isOver?"mover":"munder"],[i],t);i=n.isOver?je.makeVList({positionType:"firstBaseline",children:[{type:"elem",elem:l},{type:"kern",size:.2},{type:"elem",elem:r}]},t):je.makeVList({positionType:"bottom",positionData:l.depth+.2+r.height+r.depth,children:[{type:"elem",elem:r},{type:"kern",size:.2},{type:"elem",elem:l}]},t)}return je.makeSpan(["mord",n.isOver?"mover":"munder"],[i],t)};nt({type:"horizBrace",names:["\\overbrace","\\underbrace"],props:{numArgs:1},handler:function(e,t){var r=e.parser,n=e.funcName;return{type:"horizBrace",mode:r.mode,label:n,isOver:/^\\over/.test(n),base:t[0]}},htmlBuilder:nn,mathmlBuilder:function(e,t){var r=Dt(e.label);return new Mt.MathNode(e.isOver?"mover":"munder",[qt(e.base,t),r])}}),nt({type:"href",names:["\\href"],props:{numArgs:2,argTypes:["url","original"],allowedInText:!0},handler:function(e,t){var r=e.parser,n=t[1],a=Ft(t[0],"url").url;return r.settings.isTrusted({command:"\\href",url:a})?{type:"href",mode:r.mode,href:a,body:ot(n)}:r.formatUnsupportedCmd("\\href")},htmlBuilder:function(e,t){var r=ut(e.body,t,!1);return je.makeAnchor(e.href,[],r,t)},mathmlBuilder:function(e,t){var r=Nt(e.body,t);return r instanceof kt||(r=new kt("mrow",[r])),r.setAttribute("href",e.href),r}}),nt({type:"href",names:["\\url"],props:{numArgs:1,argTypes:["url"],allowedInText:!0},handler:function(e,t){var r=e.parser,n=Ft(t[0],"url").url;if(!r.settings.isTrusted({command:"\\url",url:n}))return r.formatUnsupportedCmd("\\url");for(var a=[],i=0;i0&&(n=Le(e.totalheight,t)-r,n=Number(n.toFixed(2)));var a=0;e.width.number>0&&(a=Le(e.width,t));var i={height:r+n+"em"};a>0&&(i.width=a+"em"),n>0&&(i.verticalAlign=-n+"em");var o=new C(e.src,e.alt,i);return o.height=r,o.depth=n,o},mathmlBuilder:function(e,t){var r=new Mt.MathNode("mglyph",[]);r.setAttribute("alt",e.alt);var n=Le(e.height,t),a=0;if(e.totalheight.number>0&&(a=(a=Le(e.totalheight,t)-n).toFixed(2),r.setAttribute("valign","-"+a+"em")),r.setAttribute("height",n+a+"em"),e.width.number>0){var i=Le(e.width,t);r.setAttribute("width",i+"em")}return r.setAttribute("src",e.src),r}}),nt({type:"kern",names:["\\kern","\\mkern","\\hskip","\\mskip"],props:{numArgs:1,argTypes:["size"],primitive:!0,allowedInText:!0},handler:function(e,t){var r=e.parser,n=e.funcName,a=Ft(t[0],"size");if(r.settings.strict){var i="m"===n[1],o="mu"===a.value.unit;i?(o||r.settings.reportNonstrict("mathVsTextUnits","LaTeX's "+n+" supports only mu units, not "+a.value.unit+" units"),"math"!==r.mode&&r.settings.reportNonstrict("mathVsTextUnits","LaTeX's "+n+" works only in math mode")):o&&r.settings.reportNonstrict("mathVsTextUnits","LaTeX's "+n+" doesn't support mu units")}return{type:"kern",mode:r.mode,dimension:a.value}},htmlBuilder:function(e,t){return je.makeGlue(e.dimension,t)},mathmlBuilder:function(e,t){var r=Le(e.dimension,t);return new Mt.SpaceNode(r)}}),nt({type:"lap",names:["\\mathllap","\\mathrlap","\\mathclap"],props:{numArgs:1,allowedInText:!0},handler:function(e,t){var r=e.parser,n=e.funcName,a=t[0];return{type:"lap",mode:r.mode,alignment:n.slice(5),body:a}},htmlBuilder:function(e,t){var r;"clap"===e.alignment?(r=je.makeSpan([],[bt(e.body,t)]),r=je.makeSpan(["inner"],[r],t)):r=je.makeSpan(["inner"],[bt(e.body,t)]);var n=je.makeSpan(["fix"],[]),a=je.makeSpan([e.alignment],[r,n],t),i=je.makeSpan(["strut"]);return i.style.height=a.height+a.depth+"em",i.style.verticalAlign=-a.depth+"em",a.children.unshift(i),a=je.makeSpan(["thinbox"],[a],t),je.makeSpan(["mord","vbox"],[a],t)},mathmlBuilder:function(e,t){var r=new Mt.MathNode("mpadded",[qt(e.body,t)]);if("rlap"!==e.alignment){var n="llap"===e.alignment?"-1":"-0.5";r.setAttribute("lspace",n+"width")}return r.setAttribute("width","0px"),r}}),nt({type:"styling",names:["\\(","$"],props:{numArgs:0,allowedInText:!0,allowedInMath:!1},handler:function(e,t){var r=e.funcName,n=e.parser,a=n.mode;n.switchMode("math");var i="\\("===r?"\\)":"$",o=n.parseExpression(!1,i);return n.expect(i),n.switchMode(a),{type:"styling",mode:n.mode,style:"text",body:o}}}),nt({type:"text",names:["\\)","\\]"],props:{numArgs:0,allowedInText:!0,allowedInMath:!1},handler:function(e,t){throw new n("Mismatched "+e.funcName)}});var on=function(e,t){switch(t.style.size){case b.DISPLAY.size:return e.display;case b.TEXT.size:return e.text;case b.SCRIPT.size:return e.script;case b.SCRIPTSCRIPT.size:return e.scriptscript;default:return e.text}};nt({type:"mathchoice",names:["\\mathchoice"],props:{numArgs:4,primitive:!0},handler:function(e,t){return{type:"mathchoice",mode:e.parser.mode,display:ot(t[0]),text:ot(t[1]),script:ot(t[2]),scriptscript:ot(t[3])}},htmlBuilder:function(e,t){var r=on(e,t),n=ut(r,t,!1);return je.makeFragment(n)},mathmlBuilder:function(e,t){var r=on(e,t);return Nt(r,t)}});var sn=function(e,t,r,n,a,i,o){e=je.makeSpan([],[e]);var s,h,m,c=r&&l.isCharacterBox(r);if(t){var u=bt(t,n.havingStyle(a.sup()),n);h={elem:u,kern:Math.max(n.fontMetrics().bigOpSpacing1,n.fontMetrics().bigOpSpacing3-u.depth)}}if(r){var p=bt(r,n.havingStyle(a.sub()),n);s={elem:p,kern:Math.max(n.fontMetrics().bigOpSpacing2,n.fontMetrics().bigOpSpacing4-p.height)}}if(h&&s){var d=n.fontMetrics().bigOpSpacing5+s.elem.height+s.elem.depth+s.kern+e.depth+o;m=je.makeVList({positionType:"bottom",positionData:d,children:[{type:"kern",size:n.fontMetrics().bigOpSpacing5},{type:"elem",elem:s.elem,marginLeft:-i+"em"},{type:"kern",size:s.kern},{type:"elem",elem:e},{type:"kern",size:h.kern},{type:"elem",elem:h.elem,marginLeft:i+"em"},{type:"kern",size:n.fontMetrics().bigOpSpacing5}]},n)}else if(s){var f=e.height-o;m=je.makeVList({positionType:"top",positionData:f,children:[{type:"kern",size:n.fontMetrics().bigOpSpacing5},{type:"elem",elem:s.elem,marginLeft:-i+"em"},{type:"kern",size:s.kern},{type:"elem",elem:e}]},n)}else{if(!h)return e;var g=e.depth+o;m=je.makeVList({positionType:"bottom",positionData:g,children:[{type:"elem",elem:e},{type:"kern",size:h.kern},{type:"elem",elem:h.elem,marginLeft:i+"em"},{type:"kern",size:n.fontMetrics().bigOpSpacing5}]},n)}var v=[m];if(s&&0!==i&&!c){var b=je.makeSpan(["mspace"],[],n);b.style.marginRight=i+"em",v.unshift(b)}return je.makeSpan(["mop","op-limits"],v,n)},ln=["\\smallint"],hn=function(e,t){var r,n,a,i=!1;"supsub"===e.type?(r=e.sup,n=e.sub,a=Ft(e.base,"op"),i=!0):a=Ft(e,"op");var o,s=t.style,h=!1;if(s.size===b.DISPLAY.size&&a.symbol&&!l.contains(ln,a.name)&&(h=!0),a.symbol){var m=h?"Size2-Regular":"Size1-Regular",c="";if("\\oiint"!==a.name&&"\\oiiint"!==a.name||(c=a.name.substr(1),a.name="oiint"===c?"\\iint":"\\iiint"),o=je.makeSymbol(a.name,m,"math",t,["mop","op-symbol",h?"large-op":"small-op"]),c.length>0){var u=o.italic,p=je.staticSvg(c+"Size"+(h?"2":"1"),t);o=je.makeVList({positionType:"individualShift",children:[{type:"elem",elem:o,shift:0},{type:"elem",elem:p,shift:h?.08:0}]},t),a.name="\\"+c,o.classes.unshift("mop"),o.italic=u}}else if(a.body){var d=ut(a.body,t,!0);1===d.length&&d[0]instanceof R?(o=d[0]).classes[0]="mop":o=je.makeSpan(["mop"],d,t)}else{for(var f=[],g=1;g0){for(var s=a.body.map((function(e){var t=e.text;return"string"==typeof t?{type:"textord",mode:e.mode,text:t}:e})),l=ut(s,t.withFont("mathrm"),!0),h=0;h=0?s.setAttribute("height","+"+a+"em"):(s.setAttribute("height",a+"em"),s.setAttribute("depth","+"+-a+"em")),s.setAttribute("voffset",a+"em"),s}});var vn=["\\tiny","\\sixptsize","\\scriptsize","\\footnotesize","\\small","\\normalsize","\\large","\\Large","\\LARGE","\\huge","\\Huge"];nt({type:"sizing",names:vn,props:{numArgs:0,allowedInText:!0},handler:function(e,t){var r=e.breakOnTokenText,n=e.funcName,a=e.parser,i=a.parseExpression(!1,r);return{type:"sizing",mode:a.mode,size:vn.indexOf(n)+1,body:i}},htmlBuilder:function(e,t){var r=t.havingSize(e.size);return gn(e.body,r,t)},mathmlBuilder:function(e,t){var r=t.havingSize(e.size),n=Bt(e.body,r),a=new Mt.MathNode("mstyle",n);return a.setAttribute("mathsize",r.sizeMultiplier+"em"),a}}),nt({type:"smash",names:["\\smash"],props:{numArgs:1,numOptionalArgs:1,allowedInText:!0},handler:function(e,t,r){var n=e.parser,a=!1,i=!1,o=r[0]&&Ft(r[0],"ordgroup");if(o)for(var s="",l=0;lr.height+r.depth+i&&(i=(i+c-r.height-r.depth)/2);var u=l.height-r.height-i-h;r.style.paddingLeft=m+"em";var p=je.makeVList({positionType:"firstBaseline",children:[{type:"elem",elem:r,wrapperClasses:["svg-align"]},{type:"kern",size:-(r.height+u)},{type:"elem",elem:l},{type:"kern",size:h}]},t);if(e.index){var d=t.havingStyle(b.SCRIPTSCRIPT),f=bt(e.index,d,t),g=.6*(p.height-p.depth),v=je.makeVList({positionType:"shift",positionData:-g,children:[{type:"elem",elem:f}]},t),y=je.makeSpan(["root"],[v]);return je.makeSpan(["mord","sqrt"],[y,p],t)}return je.makeSpan(["mord","sqrt"],[p],t)},mathmlBuilder:function(e,t){var r=e.body,n=e.index;return n?new Mt.MathNode("mroot",[qt(r,t),qt(n,t)]):new Mt.MathNode("msqrt",[qt(r,t)])}});var bn={display:b.DISPLAY,text:b.TEXT,script:b.SCRIPT,scriptscript:b.SCRIPTSCRIPT};nt({type:"styling",names:["\\displaystyle","\\textstyle","\\scriptstyle","\\scriptscriptstyle"],props:{numArgs:0,allowedInText:!0,primitive:!0},handler:function(e,t){var r=e.breakOnTokenText,n=e.funcName,a=e.parser,i=a.parseExpression(!0,r),o=n.slice(1,n.length-5);return{type:"styling",mode:a.mode,style:o,body:i}},htmlBuilder:function(e,t){var r=bn[e.style],n=t.havingStyle(r).withFont("");return gn(e.body,n,t)},mathmlBuilder:function(e,t){var r=bn[e.style],n=t.havingStyle(r),a=Bt(e.body,n),i=new Mt.MathNode("mstyle",a),o={display:["0","true"],text:["0","false"],script:["1","false"],scriptscript:["2","false"]}[e.style];return i.setAttribute("scriptlevel",o[0]),i.setAttribute("displaystyle",o[1]),i}});var yn=function(e,t){var r=e.base;return r?"op"===r.type?r.limits&&(t.style.size===b.DISPLAY.size||r.alwaysHandleSupSub)?hn:null:"operatorname"===r.type?r.alwaysHandleSupSub&&(t.style.size===b.DISPLAY.size||r.limits)?fn:null:"accent"===r.type?l.isCharacterBox(r.base)?Ut:null:"horizBrace"===r.type&&!e.sub===r.isOver?nn:null:null};at({type:"supsub",htmlBuilder:function(e,t){var r=yn(e,t);if(r)return r(e,t);var n,a,i,o=e.base,s=e.sup,h=e.sub,m=bt(o,t),c=t.fontMetrics(),u=0,p=0,d=o&&l.isCharacterBox(o);if(s){var f=t.havingStyle(t.style.sup());n=bt(s,f,t),d||(u=m.height-f.fontMetrics().supDrop*f.sizeMultiplier/t.sizeMultiplier)}if(h){var g=t.havingStyle(t.style.sub());a=bt(h,g,t),d||(p=m.depth+g.fontMetrics().subDrop*g.sizeMultiplier/t.sizeMultiplier)}i=t.style===b.DISPLAY?c.sup1:t.style.cramped?c.sup3:c.sup2;var v,y=t.sizeMultiplier,x=.5/c.ptPerEm/y+"em",w=null;if(a){var k=e.base&&"op"===e.base.type&&e.base.name&&("\\oiint"===e.base.name||"\\oiiint"===e.base.name);(m instanceof R||k)&&(w=-m.italic+"em")}if(n&&a){u=Math.max(u,i,n.depth+.25*c.xHeight),p=Math.max(p,c.sub2);var S=4*c.defaultRuleThickness;if(u-n.depth-(a.height-p)0&&(u+=M,p-=M)}var z=[{type:"elem",elem:a,shift:p,marginRight:x,marginLeft:w},{type:"elem",elem:n,shift:-u,marginRight:x}];v=je.makeVList({positionType:"individualShift",children:z},t)}else if(a){p=Math.max(p,c.sub1,a.height-.8*c.xHeight);var A=[{type:"elem",elem:a,marginLeft:w,marginRight:x}];v=je.makeVList({positionType:"shift",positionData:p,children:A},t)}else{if(!n)throw new Error("supsub must have either sup or sub.");u=Math.max(u,i,n.depth+.25*c.xHeight),v=je.makeVList({positionType:"shift",positionData:-u,children:[{type:"elem",elem:n,marginRight:x}]},t)}var T=gt(m,"right")||"mord";return je.makeSpan([T],[m,je.makeSpan(["msupsub"],[v])],t)},mathmlBuilder:function(e,t){var r,n=!1;e.base&&"horizBrace"===e.base.type&&!!e.sup===e.base.isOver&&(n=!0,r=e.base.isOver),!e.base||"op"!==e.base.type&&"operatorname"!==e.base.type||(e.base.parentIsSupSub=!0);var a,i=[qt(e.base,t)];if(e.sub&&i.push(qt(e.sub,t)),e.sup&&i.push(qt(e.sup,t)),n)a=r?"mover":"munder";else if(e.sub)if(e.sup){var o=e.base;a=o&&"op"===o.type&&o.limits&&t.style===b.DISPLAY||o&&"operatorname"===o.type&&o.alwaysHandleSupSub&&(t.style===b.DISPLAY||o.limits)?"munderover":"msubsup"}else{var s=e.base;a=s&&"op"===s.type&&s.limits&&(t.style===b.DISPLAY||s.alwaysHandleSupSub)||s&&"operatorname"===s.type&&s.alwaysHandleSupSub&&(s.limits||t.style===b.DISPLAY)?"munder":"msub"}else{var l=e.base;a=l&&"op"===l.type&&l.limits&&(t.style===b.DISPLAY||l.alwaysHandleSupSub)||l&&"operatorname"===l.type&&l.alwaysHandleSupSub&&(l.limits||t.style===b.DISPLAY)?"mover":"msup"}return new Mt.MathNode(a,i)}}),at({type:"atom",htmlBuilder:function(e,t){return je.mathsym(e.text,e.mode,t,["m"+e.family])},mathmlBuilder:function(e,t){var r=new Mt.MathNode("mo",[zt(e.text,e.mode)]);if("bin"===e.family){var n=Tt(e,t);"bold-italic"===n&&r.setAttribute("mathvariant",n)}else"punct"===e.family?r.setAttribute("separator","true"):"open"!==e.family&&"close"!==e.family||r.setAttribute("stretchy","false");return r}});var xn={mi:"italic",mn:"normal",mtext:"normal"};at({type:"mathord",htmlBuilder:function(e,t){return je.makeOrd(e,t,"mathord")},mathmlBuilder:function(e,t){var r=new Mt.MathNode("mi",[zt(e.text,e.mode,t)]),n=Tt(e,t)||"italic";return n!==xn[r.type]&&r.setAttribute("mathvariant",n),r}}),at({type:"textord",htmlBuilder:function(e,t){return je.makeOrd(e,t,"textord")},mathmlBuilder:function(e,t){var r,n=zt(e.text,e.mode,t),a=Tt(e,t)||"normal";return r="text"===e.mode?new Mt.MathNode("mtext",[n]):/[0-9]/.test(e.text)?new Mt.MathNode("mn",[n]):"\\prime"===e.text?new Mt.MathNode("mo",[n]):new Mt.MathNode("mi",[n]),a!==xn[r.type]&&r.setAttribute("mathvariant",a),r}});var wn={"\\nobreak":"nobreak","\\allowbreak":"allowbreak"},kn={" ":{},"\\ ":{},"~":{className:"nobreak"},"\\space":{},"\\nobreakspace":{className:"nobreak"}};at({type:"spacing",htmlBuilder:function(e,t){if(kn.hasOwnProperty(e.text)){var r=kn[e.text].className||"";if("text"===e.mode){var a=je.makeOrd(e,t,"textord");return a.classes.push(r),a}return je.makeSpan(["mspace",r],[je.mathsym(e.text,e.mode,t)],t)}if(wn.hasOwnProperty(e.text))return je.makeSpan(["mspace",wn[e.text]],[],t);throw new n('Unknown type of space "'+e.text+'"')},mathmlBuilder:function(e,t){if(!kn.hasOwnProperty(e.text)){if(wn.hasOwnProperty(e.text))return new Mt.MathNode("mspace");throw new n('Unknown type of space "'+e.text+'"')}return new Mt.MathNode("mtext",[new Mt.TextNode("\xa0")])}});var Sn=function(){var e=new Mt.MathNode("mtd",[]);return e.setAttribute("width","50%"),e};at({type:"tag",mathmlBuilder:function(e,t){var r=new Mt.MathNode("mtable",[new Mt.MathNode("mtr",[Sn(),new Mt.MathNode("mtd",[Nt(e.body,t)]),Sn(),new Mt.MathNode("mtd",[Nt(e.tag,t)])])]);return r.setAttribute("width","100%"),r}});var Mn={"\\text":void 0,"\\textrm":"textrm","\\textsf":"textsf","\\texttt":"texttt","\\textnormal":"textrm"},zn={"\\textbf":"textbf","\\textmd":"textmd"},An={"\\textit":"textit","\\textup":"textup"},Tn=function(e,t){var r=e.font;return r?Mn[r]?t.withTextFontFamily(Mn[r]):zn[r]?t.withTextFontWeight(zn[r]):t.withTextFontShape(An[r]):t};nt({type:"text",names:["\\text","\\textrm","\\textsf","\\texttt","\\textnormal","\\textbf","\\textmd","\\textit","\\textup"],props:{numArgs:1,argTypes:["text"],allowedInArgument:!0,allowedInText:!0},handler:function(e,t){var r=e.parser,n=e.funcName,a=t[0];return{type:"text",mode:r.mode,body:ot(a),font:n}},htmlBuilder:function(e,t){var r=Tn(e,t),n=ut(e.body,r,!0);return je.makeSpan(["mord","text"],n,r)},mathmlBuilder:function(e,t){var r=Tn(e,t);return Nt(e.body,r)}}),nt({type:"underline",names:["\\underline"],props:{numArgs:1,allowedInText:!0},handler:function(e,t){return{type:"underline",mode:e.parser.mode,body:t[0]}},htmlBuilder:function(e,t){var r=bt(e.body,t),n=je.makeLineSpan("underline-line",t),a=t.fontMetrics().defaultRuleThickness,i=je.makeVList({positionType:"top",positionData:r.height,children:[{type:"kern",size:a},{type:"elem",elem:n},{type:"kern",size:3*a},{type:"elem",elem:r}]},t);return je.makeSpan(["mord","underline"],[i],t)},mathmlBuilder:function(e,t){var r=new Mt.MathNode("mo",[new Mt.TextNode("\u203e")]);r.setAttribute("stretchy","true");var n=new Mt.MathNode("munder",[qt(e.body,t),r]);return n.setAttribute("accentunder","true"),n}}),nt({type:"vcenter",names:["\\vcenter"],props:{numArgs:1,argTypes:["original"],allowedInText:!1},handler:function(e,t){return{type:"vcenter",mode:e.parser.mode,body:t[0]}},htmlBuilder:function(e,t){var r=bt(e.body,t),n=t.fontMetrics().axisHeight,a=.5*(r.height-n-(r.depth+n));return je.makeVList({positionType:"shift",positionData:a,children:[{type:"elem",elem:r}]},t)},mathmlBuilder:function(e,t){return new Mt.MathNode("mpadded",[qt(e.body,t)],["vcenter"])}}),nt({type:"verb",names:["\\verb"],props:{numArgs:0,allowedInText:!0},handler:function(e,t,r){throw new n("\\verb ended by end of line instead of matching delimiter")},htmlBuilder:function(e,t){for(var r=Bn(e),n=[],a=t.havingStyle(t.style.text()),i=0;i0;)this.endGroup()},t.has=function(e){return this.current.hasOwnProperty(e)||this.builtins.hasOwnProperty(e)},t.get=function(e){return this.current.hasOwnProperty(e)?this.current[e]:this.builtins[e]},t.set=function(e,t,r){if(void 0===r&&(r=!1),r){for(var n=0;n0&&(this.undefStack[this.undefStack.length-1][e]=t)}else{var a=this.undefStack[this.undefStack.length-1];a&&!a.hasOwnProperty(e)&&(a[e]=this.current[e])}this.current[e]=t},e}(),En=pn;dn("\\noexpand",(function(e){var t=e.popToken();return e.isExpandable(t.text)&&(t.noexpand=!0,t.treatAsRelax=!0),{tokens:[t],numArgs:0}})),dn("\\expandafter",(function(e){var t=e.popToken();return e.expandOnce(!0),{tokens:[t],numArgs:0}})),dn("\\@firstoftwo",(function(e){return{tokens:e.consumeArgs(2)[0],numArgs:0}})),dn("\\@secondoftwo",(function(e){return{tokens:e.consumeArgs(2)[1],numArgs:0}})),dn("\\@ifnextchar",(function(e){var t=e.consumeArgs(3);e.consumeSpaces();var r=e.future();return 1===t[0].length&&t[0][0].text===r.text?{tokens:t[1],numArgs:0}:{tokens:t[2],numArgs:0}})),dn("\\@ifstar","\\@ifnextchar *{\\@firstoftwo{#1}}"),dn("\\TextOrMath",(function(e){var t=e.consumeArgs(2);return"text"===e.mode?{tokens:t[0],numArgs:0}:{tokens:t[1],numArgs:0}}));var Hn={0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,a:10,A:10,b:11,B:11,c:12,C:12,d:13,D:13,e:14,E:14,f:15,F:15};dn("\\char",(function(e){var t,r=e.popToken(),a="";if("'"===r.text)t=8,r=e.popToken();else if('"'===r.text)t=16,r=e.popToken();else if("`"===r.text)if("\\"===(r=e.popToken()).text[0])a=r.text.charCodeAt(1);else{if("EOF"===r.text)throw new n("\\char` missing argument");a=r.text.charCodeAt(0)}else t=10;if(t){if(null==(a=Hn[r.text])||a>=t)throw new n("Invalid base-"+t+" digit "+r.text);for(var i;null!=(i=Hn[e.future().text])&&i":"\\dotsb","-":"\\dotsb","*":"\\dotsb",":":"\\dotsb","\\DOTSB":"\\dotsb","\\coprod":"\\dotsb","\\bigvee":"\\dotsb","\\bigwedge":"\\dotsb","\\biguplus":"\\dotsb","\\bigcap":"\\dotsb","\\bigcup":"\\dotsb","\\prod":"\\dotsb","\\sum":"\\dotsb","\\bigotimes":"\\dotsb","\\bigoplus":"\\dotsb","\\bigodot":"\\dotsb","\\bigsqcup":"\\dotsb","\\And":"\\dotsb","\\longrightarrow":"\\dotsb","\\Longrightarrow":"\\dotsb","\\longleftarrow":"\\dotsb","\\Longleftarrow":"\\dotsb","\\longleftrightarrow":"\\dotsb","\\Longleftrightarrow":"\\dotsb","\\mapsto":"\\dotsb","\\longmapsto":"\\dotsb","\\hookrightarrow":"\\dotsb","\\doteq":"\\dotsb","\\mathbin":"\\dotsb","\\mathrel":"\\dotsb","\\relbar":"\\dotsb","\\Relbar":"\\dotsb","\\xrightarrow":"\\dotsb","\\xleftarrow":"\\dotsb","\\DOTSI":"\\dotsi","\\int":"\\dotsi","\\oint":"\\dotsi","\\iint":"\\dotsi","\\iiint":"\\dotsi","\\iiiint":"\\dotsi","\\idotsint":"\\dotsi","\\DOTSX":"\\dotsx"};dn("\\dots",(function(e){var t="\\dotso",r=e.expandAfterFuture().text;return r in Dn?t=Dn[r]:("\\not"===r.substr(0,4)||r in X.math&&l.contains(["bin","rel"],X.math[r].group))&&(t="\\dotsb"),t}));var Pn={")":!0,"]":!0,"\\rbrack":!0,"\\}":!0,"\\rbrace":!0,"\\rangle":!0,"\\rceil":!0,"\\rfloor":!0,"\\rgroup":!0,"\\rmoustache":!0,"\\right":!0,"\\bigr":!0,"\\biggr":!0,"\\Bigr":!0,"\\Biggr":!0,$:!0,";":!0,".":!0,",":!0};dn("\\dotso",(function(e){return e.future().text in Pn?"\\ldots\\,":"\\ldots"})),dn("\\dotsc",(function(e){var t=e.future().text;return t in Pn&&","!==t?"\\ldots\\,":"\\ldots"})),dn("\\cdots",(function(e){return e.future().text in Pn?"\\@cdots\\,":"\\@cdots"})),dn("\\dotsb","\\cdots"),dn("\\dotsm","\\cdots"),dn("\\dotsi","\\!\\cdots"),dn("\\dotsx","\\ldots\\,"),dn("\\DOTSI","\\relax"),dn("\\DOTSB","\\relax"),dn("\\DOTSX","\\relax"),dn("\\tmspace","\\TextOrMath{\\kern#1#3}{\\mskip#1#2}\\relax"),dn("\\,","\\tmspace+{3mu}{.1667em}"),dn("\\thinspace","\\,"),dn("\\>","\\mskip{4mu}"),dn("\\:","\\tmspace+{4mu}{.2222em}"),dn("\\medspace","\\:"),dn("\\;","\\tmspace+{5mu}{.2777em}"),dn("\\thickspace","\\;"),dn("\\!","\\tmspace-{3mu}{.1667em}"),dn("\\negthinspace","\\!"),dn("\\negmedspace","\\tmspace-{4mu}{.2222em}"),dn("\\negthickspace","\\tmspace-{5mu}{.277em}"),dn("\\enspace","\\kern.5em "),dn("\\enskip","\\hskip.5em\\relax"),dn("\\quad","\\hskip1em\\relax"),dn("\\qquad","\\hskip2em\\relax"),dn("\\tag","\\@ifstar\\tag@literal\\tag@paren"),dn("\\tag@paren","\\tag@literal{({#1})}"),dn("\\tag@literal",(function(e){if(e.macros.get("\\df@tag"))throw new n("Multiple \\tag");return"\\gdef\\df@tag{\\text{#1}}"})),dn("\\bmod","\\mathchoice{\\mskip1mu}{\\mskip1mu}{\\mskip5mu}{\\mskip5mu}\\mathbin{\\rm mod}\\mathchoice{\\mskip1mu}{\\mskip1mu}{\\mskip5mu}{\\mskip5mu}"),dn("\\pod","\\allowbreak\\mathchoice{\\mkern18mu}{\\mkern8mu}{\\mkern8mu}{\\mkern8mu}(#1)"),dn("\\pmod","\\pod{{\\rm mod}\\mkern6mu#1}"),dn("\\mod","\\allowbreak\\mathchoice{\\mkern18mu}{\\mkern12mu}{\\mkern12mu}{\\mkern12mu}{\\rm mod}\\,\\,#1"),dn("\\pmb","\\html@mathml{\\@binrel{#1}{\\mathrlap{#1}\\kern0.5px#1}}{\\mathbf{#1}}"),dn("\\newline","\\\\\\relax"),dn("\\TeX","\\textrm{\\html@mathml{T\\kern-.1667em\\raisebox{-.5ex}{E}\\kern-.125emX}{TeX}}");var Fn=D["Main-Regular"]["T".charCodeAt(0)][1]-.7*D["Main-Regular"]["A".charCodeAt(0)][1]+"em";dn("\\LaTeX","\\textrm{\\html@mathml{L\\kern-.36em\\raisebox{"+Fn+"}{\\scriptstyle A}\\kern-.15em\\TeX}{LaTeX}}"),dn("\\KaTeX","\\textrm{\\html@mathml{K\\kern-.17em\\raisebox{"+Fn+"}{\\scriptstyle A}\\kern-.15em\\TeX}{KaTeX}}"),dn("\\hspace","\\@ifstar\\@hspacer\\@hspace"),dn("\\@hspace","\\hskip #1\\relax"),dn("\\@hspacer","\\rule{0pt}{0pt}\\hskip #1\\relax"),dn("\\ordinarycolon",":"),dn("\\vcentcolon","\\mathrel{\\mathop\\ordinarycolon}"),dn("\\dblcolon",'\\html@mathml{\\mathrel{\\vcentcolon\\mathrel{\\mkern-.9mu}\\vcentcolon}}{\\mathop{\\char"2237}}'),dn("\\coloneqq",'\\html@mathml{\\mathrel{\\vcentcolon\\mathrel{\\mkern-1.2mu}=}}{\\mathop{\\char"2254}}'),dn("\\Coloneqq",'\\html@mathml{\\mathrel{\\dblcolon\\mathrel{\\mkern-1.2mu}=}}{\\mathop{\\char"2237\\char"3d}}'),dn("\\coloneq",'\\html@mathml{\\mathrel{\\vcentcolon\\mathrel{\\mkern-1.2mu}\\mathrel{-}}}{\\mathop{\\char"3a\\char"2212}}'),dn("\\Coloneq",'\\html@mathml{\\mathrel{\\dblcolon\\mathrel{\\mkern-1.2mu}\\mathrel{-}}}{\\mathop{\\char"2237\\char"2212}}'),dn("\\eqqcolon",'\\html@mathml{\\mathrel{=\\mathrel{\\mkern-1.2mu}\\vcentcolon}}{\\mathop{\\char"2255}}'),dn("\\Eqqcolon",'\\html@mathml{\\mathrel{=\\mathrel{\\mkern-1.2mu}\\dblcolon}}{\\mathop{\\char"3d\\char"2237}}'),dn("\\eqcolon",'\\html@mathml{\\mathrel{\\mathrel{-}\\mathrel{\\mkern-1.2mu}\\vcentcolon}}{\\mathop{\\char"2239}}'),dn("\\Eqcolon",'\\html@mathml{\\mathrel{\\mathrel{-}\\mathrel{\\mkern-1.2mu}\\dblcolon}}{\\mathop{\\char"2212\\char"2237}}'),dn("\\colonapprox",'\\html@mathml{\\mathrel{\\vcentcolon\\mathrel{\\mkern-1.2mu}\\approx}}{\\mathop{\\char"3a\\char"2248}}'),dn("\\Colonapprox",'\\html@mathml{\\mathrel{\\dblcolon\\mathrel{\\mkern-1.2mu}\\approx}}{\\mathop{\\char"2237\\char"2248}}'),dn("\\colonsim",'\\html@mathml{\\mathrel{\\vcentcolon\\mathrel{\\mkern-1.2mu}\\sim}}{\\mathop{\\char"3a\\char"223c}}'),dn("\\Colonsim",'\\html@mathml{\\mathrel{\\dblcolon\\mathrel{\\mkern-1.2mu}\\sim}}{\\mathop{\\char"2237\\char"223c}}'),dn("\u2237","\\dblcolon"),dn("\u2239","\\eqcolon"),dn("\u2254","\\coloneqq"),dn("\u2255","\\eqqcolon"),dn("\u2a74","\\Coloneqq"),dn("\\ratio","\\vcentcolon"),dn("\\coloncolon","\\dblcolon"),dn("\\colonequals","\\coloneqq"),dn("\\coloncolonequals","\\Coloneqq"),dn("\\equalscolon","\\eqqcolon"),dn("\\equalscoloncolon","\\Eqqcolon"),dn("\\colonminus","\\coloneq"),dn("\\coloncolonminus","\\Coloneq"),dn("\\minuscolon","\\eqcolon"),dn("\\minuscoloncolon","\\Eqcolon"),dn("\\coloncolonapprox","\\Colonapprox"),dn("\\coloncolonsim","\\Colonsim"),dn("\\simcolon","\\mathrel{\\sim\\mathrel{\\mkern-1.2mu}\\vcentcolon}"),dn("\\simcoloncolon","\\mathrel{\\sim\\mathrel{\\mkern-1.2mu}\\dblcolon}"),dn("\\approxcolon","\\mathrel{\\approx\\mathrel{\\mkern-1.2mu}\\vcentcolon}"),dn("\\approxcoloncolon","\\mathrel{\\approx\\mathrel{\\mkern-1.2mu}\\dblcolon}"),dn("\\notni","\\html@mathml{\\not\\ni}{\\mathrel{\\char`\u220c}}"),dn("\\limsup","\\DOTSB\\operatorname*{lim\\,sup}"),dn("\\liminf","\\DOTSB\\operatorname*{lim\\,inf}"),dn("\\injlim","\\DOTSB\\operatorname*{inj\\,lim}"),dn("\\projlim","\\DOTSB\\operatorname*{proj\\,lim}"),dn("\\varlimsup","\\DOTSB\\operatorname*{\\overline{lim}}"),dn("\\varliminf","\\DOTSB\\operatorname*{\\underline{lim}}"),dn("\\varinjlim","\\DOTSB\\operatorname*{\\underrightarrow{lim}}"),dn("\\varprojlim","\\DOTSB\\operatorname*{\\underleftarrow{lim}}"),dn("\\gvertneqq","\\html@mathml{\\@gvertneqq}{\u2269}"),dn("\\lvertneqq","\\html@mathml{\\@lvertneqq}{\u2268}"),dn("\\ngeqq","\\html@mathml{\\@ngeqq}{\u2271}"),dn("\\ngeqslant","\\html@mathml{\\@ngeqslant}{\u2271}"),dn("\\nleqq","\\html@mathml{\\@nleqq}{\u2270}"),dn("\\nleqslant","\\html@mathml{\\@nleqslant}{\u2270}"),dn("\\nshortmid","\\html@mathml{\\@nshortmid}{\u2224}"),dn("\\nshortparallel","\\html@mathml{\\@nshortparallel}{\u2226}"),dn("\\nsubseteqq","\\html@mathml{\\@nsubseteqq}{\u2288}"),dn("\\nsupseteqq","\\html@mathml{\\@nsupseteqq}{\u2289}"),dn("\\varsubsetneq","\\html@mathml{\\@varsubsetneq}{\u228a}"),dn("\\varsubsetneqq","\\html@mathml{\\@varsubsetneqq}{\u2acb}"),dn("\\varsupsetneq","\\html@mathml{\\@varsupsetneq}{\u228b}"),dn("\\varsupsetneqq","\\html@mathml{\\@varsupsetneqq}{\u2acc}"),dn("\\imath","\\html@mathml{\\@imath}{\u0131}"),dn("\\jmath","\\html@mathml{\\@jmath}{\u0237}"),dn("\\llbracket","\\html@mathml{\\mathopen{[\\mkern-3.2mu[}}{\\mathopen{\\char`\u27e6}}"),dn("\\rrbracket","\\html@mathml{\\mathclose{]\\mkern-3.2mu]}}{\\mathclose{\\char`\u27e7}}"),dn("\u27e6","\\llbracket"),dn("\u27e7","\\rrbracket"),dn("\\lBrace","\\html@mathml{\\mathopen{\\{\\mkern-3.2mu[}}{\\mathopen{\\char`\u2983}}"),dn("\\rBrace","\\html@mathml{\\mathclose{]\\mkern-3.2mu\\}}}{\\mathclose{\\char`\u2984}}"),dn("\u2983","\\lBrace"),dn("\u2984","\\rBrace"),dn("\\minuso","\\mathbin{\\html@mathml{{\\mathrlap{\\mathchoice{\\kern{0.145em}}{\\kern{0.145em}}{\\kern{0.1015em}}{\\kern{0.0725em}}\\circ}{-}}}{\\char`\u29b5}}"),dn("\u29b5","\\minuso"),dn("\\darr","\\downarrow"),dn("\\dArr","\\Downarrow"),dn("\\Darr","\\Downarrow"),dn("\\lang","\\langle"),dn("\\rang","\\rangle"),dn("\\uarr","\\uparrow"),dn("\\uArr","\\Uparrow"),dn("\\Uarr","\\Uparrow"),dn("\\N","\\mathbb{N}"),dn("\\R","\\mathbb{R}"),dn("\\Z","\\mathbb{Z}"),dn("\\alef","\\aleph"),dn("\\alefsym","\\aleph"),dn("\\Alpha","\\mathrm{A}"),dn("\\Beta","\\mathrm{B}"),dn("\\bull","\\bullet"),dn("\\Chi","\\mathrm{X}"),dn("\\clubs","\\clubsuit"),dn("\\cnums","\\mathbb{C}"),dn("\\Complex","\\mathbb{C}"),dn("\\Dagger","\\ddagger"),dn("\\diamonds","\\diamondsuit"),dn("\\empty","\\emptyset"),dn("\\Epsilon","\\mathrm{E}"),dn("\\Eta","\\mathrm{H}"),dn("\\exist","\\exists"),dn("\\harr","\\leftrightarrow"),dn("\\hArr","\\Leftrightarrow"),dn("\\Harr","\\Leftrightarrow"),dn("\\hearts","\\heartsuit"),dn("\\image","\\Im"),dn("\\infin","\\infty"),dn("\\Iota","\\mathrm{I}"),dn("\\isin","\\in"),dn("\\Kappa","\\mathrm{K}"),dn("\\larr","\\leftarrow"),dn("\\lArr","\\Leftarrow"),dn("\\Larr","\\Leftarrow"),dn("\\lrarr","\\leftrightarrow"),dn("\\lrArr","\\Leftrightarrow"),dn("\\Lrarr","\\Leftrightarrow"),dn("\\Mu","\\mathrm{M}"),dn("\\natnums","\\mathbb{N}"),dn("\\Nu","\\mathrm{N}"),dn("\\Omicron","\\mathrm{O}"),dn("\\plusmn","\\pm"),dn("\\rarr","\\rightarrow"),dn("\\rArr","\\Rightarrow"),dn("\\Rarr","\\Rightarrow"),dn("\\real","\\Re"),dn("\\reals","\\mathbb{R}"),dn("\\Reals","\\mathbb{R}"),dn("\\Rho","\\mathrm{P}"),dn("\\sdot","\\cdot"),dn("\\sect","\\S"),dn("\\spades","\\spadesuit"),dn("\\sub","\\subset"),dn("\\sube","\\subseteq"),dn("\\supe","\\supseteq"),dn("\\Tau","\\mathrm{T}"),dn("\\thetasym","\\vartheta"),dn("\\weierp","\\wp"),dn("\\Zeta","\\mathrm{Z}"),dn("\\argmin","\\DOTSB\\operatorname*{arg\\,min}"),dn("\\argmax","\\DOTSB\\operatorname*{arg\\,max}"),dn("\\plim","\\DOTSB\\mathop{\\operatorname{plim}}\\limits"),dn("\\bra","\\mathinner{\\langle{#1}|}"),dn("\\ket","\\mathinner{|{#1}\\rangle}"),dn("\\braket","\\mathinner{\\langle{#1}\\rangle}"),dn("\\Bra","\\left\\langle#1\\right|"),dn("\\Ket","\\left|#1\\right\\rangle"),dn("\\angln","{\\angl n}"),dn("\\blue","\\textcolor{##6495ed}{#1}"),dn("\\orange","\\textcolor{##ffa500}{#1}"),dn("\\pink","\\textcolor{##ff00af}{#1}"),dn("\\red","\\textcolor{##df0030}{#1}"),dn("\\green","\\textcolor{##28ae7b}{#1}"),dn("\\gray","\\textcolor{gray}{#1}"),dn("\\purple","\\textcolor{##9d38bd}{#1}"),dn("\\blueA","\\textcolor{##ccfaff}{#1}"),dn("\\blueB","\\textcolor{##80f6ff}{#1}"),dn("\\blueC","\\textcolor{##63d9ea}{#1}"),dn("\\blueD","\\textcolor{##11accd}{#1}"),dn("\\blueE","\\textcolor{##0c7f99}{#1}"),dn("\\tealA","\\textcolor{##94fff5}{#1}"),dn("\\tealB","\\textcolor{##26edd5}{#1}"),dn("\\tealC","\\textcolor{##01d1c1}{#1}"),dn("\\tealD","\\textcolor{##01a995}{#1}"),dn("\\tealE","\\textcolor{##208170}{#1}"),dn("\\greenA","\\textcolor{##b6ffb0}{#1}"),dn("\\greenB","\\textcolor{##8af281}{#1}"),dn("\\greenC","\\textcolor{##74cf70}{#1}"),dn("\\greenD","\\textcolor{##1fab54}{#1}"),dn("\\greenE","\\textcolor{##0d923f}{#1}"),dn("\\goldA","\\textcolor{##ffd0a9}{#1}"),dn("\\goldB","\\textcolor{##ffbb71}{#1}"),dn("\\goldC","\\textcolor{##ff9c39}{#1}"),dn("\\goldD","\\textcolor{##e07d10}{#1}"),dn("\\goldE","\\textcolor{##a75a05}{#1}"),dn("\\redA","\\textcolor{##fca9a9}{#1}"),dn("\\redB","\\textcolor{##ff8482}{#1}"),dn("\\redC","\\textcolor{##f9685d}{#1}"),dn("\\redD","\\textcolor{##e84d39}{#1}"),dn("\\redE","\\textcolor{##bc2612}{#1}"),dn("\\maroonA","\\textcolor{##ffbde0}{#1}"),dn("\\maroonB","\\textcolor{##ff92c6}{#1}"),dn("\\maroonC","\\textcolor{##ed5fa6}{#1}"),dn("\\maroonD","\\textcolor{##ca337c}{#1}"),dn("\\maroonE","\\textcolor{##9e034e}{#1}"),dn("\\purpleA","\\textcolor{##ddd7ff}{#1}"),dn("\\purpleB","\\textcolor{##c6b9fc}{#1}"),dn("\\purpleC","\\textcolor{##aa87ff}{#1}"),dn("\\purpleD","\\textcolor{##7854ab}{#1}"),dn("\\purpleE","\\textcolor{##543b78}{#1}"),dn("\\mintA","\\textcolor{##f5f9e8}{#1}"),dn("\\mintB","\\textcolor{##edf2df}{#1}"),dn("\\mintC","\\textcolor{##e0e5cc}{#1}"),dn("\\grayA","\\textcolor{##f6f7f7}{#1}"),dn("\\grayB","\\textcolor{##f0f1f2}{#1}"),dn("\\grayC","\\textcolor{##e3e5e6}{#1}"),dn("\\grayD","\\textcolor{##d6d8da}{#1}"),dn("\\grayE","\\textcolor{##babec2}{#1}"),dn("\\grayF","\\textcolor{##888d93}{#1}"),dn("\\grayG","\\textcolor{##626569}{#1}"),dn("\\grayH","\\textcolor{##3b3e40}{#1}"),dn("\\grayI","\\textcolor{##21242c}{#1}"),dn("\\kaBlue","\\textcolor{##314453}{#1}"),dn("\\kaGreen","\\textcolor{##71B307}{#1}");var Vn={"\\relax":!0,"^":!0,_:!0,"\\limits":!0,"\\nolimits":!0},Gn=function(){function e(e,t,r){this.settings=void 0,this.expansionCount=void 0,this.lexer=void 0,this.macros=void 0,this.stack=void 0,this.mode=void 0,this.settings=t,this.expansionCount=0,this.feed(e),this.macros=new On(En,t.macros),this.mode=r,this.stack=[]}var t=e.prototype;return t.feed=function(e){this.lexer=new Rn(e,this.settings)},t.switchMode=function(e){this.mode=e},t.beginGroup=function(){this.macros.beginGroup()},t.endGroup=function(){this.macros.endGroup()},t.endGroups=function(){this.macros.endGroups()},t.future=function(){return 0===this.stack.length&&this.pushToken(this.lexer.lex()),this.stack[this.stack.length-1]},t.popToken=function(){return this.future(),this.stack.pop()},t.pushToken=function(e){this.stack.push(e)},t.pushTokens=function(e){var t;(t=this.stack).push.apply(t,e)},t.scanArgument=function(e){var t,r,n;if(e){if(this.consumeSpaces(),"["!==this.future().text)return null;t=this.popToken();var a=this.consumeArg(["]"]);n=a.tokens,r=a.end}else{var i=this.consumeArg();n=i.tokens,t=i.start,r=i.end}return this.pushToken(new Cn("EOF",r.loc)),this.pushTokens(n),t.range(r,"")},t.consumeSpaces=function(){for(;;){if(" "!==this.future().text)break;this.stack.pop()}},t.consumeArg=function(e){var t=[],r=e&&e.length>0;r||this.consumeSpaces();var a,i=this.future(),o=0,s=0;do{if(a=this.popToken(),t.push(a),"{"===a.text)++o;else if("}"===a.text){if(-1===--o)throw new n("Extra }",a)}else if("EOF"===a.text)throw new n("Unexpected end of input in a macro argument, expected '"+(e&&r?e[s]:"}")+"'",a);if(e&&r)if((0===o||1===o&&"{"===e[s])&&a.text===e[s]){if(++s===e.length){t.splice(-s,s);break}}else s=0}while(0!==o||r);return"{"===i.text&&"}"===t[t.length-1].text&&(t.pop(),t.shift()),t.reverse(),{tokens:t,start:i,end:a}},t.consumeArgs=function(e,t){if(t){if(t.length!==e+1)throw new n("The length of delimiters doesn't match the number of args!");for(var r=t[0],a=0;athis.settings.maxExpand)throw new n("Too many expansions: infinite loop or need to increase maxExpand setting");var i=a.tokens,o=this.consumeArgs(a.numArgs,a.delimiters);if(a.numArgs)for(var s=(i=i.slice()).length-1;s>=0;--s){var l=i[s];if("#"===l.text){if(0===s)throw new n("Incomplete placeholder at end of macro body",l);if("#"===(l=i[--s]).text)i.splice(s+1,1);else{if(!/^[1-9]$/.test(l.text))throw new n("Not a valid argument number",l);var h;(h=i).splice.apply(h,[s,2].concat(o[+l.text-1]))}}}return this.pushTokens(i),i},t.expandAfterFuture=function(){return this.expandOnce(),this.future()},t.expandNextToken=function(){for(;;){var e=this.expandOnce();if(e instanceof Cn){if("\\relax"!==e.text&&!e.treatAsRelax)return this.stack.pop();this.stack.pop()}}throw new Error},t.expandMacro=function(e){return this.macros.has(e)?this.expandTokens([new Cn(e)]):void 0},t.expandTokens=function(e){var t=[],r=this.stack.length;for(this.pushTokens(e);this.stack.length>r;){var n=this.expandOnce(!0);n instanceof Cn&&(n.treatAsRelax&&(n.noexpand=!1,n.treatAsRelax=!1),t.push(this.stack.pop()))}return t},t.expandMacroAsText=function(e){var t=this.expandMacro(e);return t?t.map((function(e){return e.text})).join(""):t},t._getExpansion=function(e){var t=this.macros.get(e);if(null==t)return t;if(1===e.length){var r=this.lexer.catcodes[e];if(null!=r&&13!==r)return}var n="function"==typeof t?t(this):t;if("string"==typeof n){var a=0;if(-1!==n.indexOf("#"))for(var i=n.replace(/##/g,"");-1!==i.indexOf("#"+(a+1));)++a;for(var o=new Rn(n,this.settings),s=[],l=o.lex();"EOF"!==l.text;)s.push(l),l=o.lex();return s.reverse(),{tokens:s,numArgs:a}}return n},t.isDefined=function(e){return this.macros.has(e)||Nn.hasOwnProperty(e)||X.math.hasOwnProperty(e)||X.text.hasOwnProperty(e)||Vn.hasOwnProperty(e)},t.isExpandable=function(e){var t=this.macros.get(e);return null!=t?"string"==typeof t||"function"==typeof t||!t.unexpandable:Nn.hasOwnProperty(e)&&!Nn[e].primitive},e}(),Un={"\u0301":{text:"\\'",math:"\\acute"},"\u0300":{text:"\\`",math:"\\grave"},"\u0308":{text:'\\"',math:"\\ddot"},"\u0303":{text:"\\~",math:"\\tilde"},"\u0304":{text:"\\=",math:"\\bar"},"\u0306":{text:"\\u",math:"\\breve"},"\u030c":{text:"\\v",math:"\\check"},"\u0302":{text:"\\^",math:"\\hat"},"\u0307":{text:"\\.",math:"\\dot"},"\u030a":{text:"\\r",math:"\\mathring"},"\u030b":{text:"\\H"},"\u0327":{text:"\\c"}},Yn={"\xe1":"a\u0301","\xe0":"a\u0300","\xe4":"a\u0308","\u01df":"a\u0308\u0304","\xe3":"a\u0303","\u0101":"a\u0304","\u0103":"a\u0306","\u1eaf":"a\u0306\u0301","\u1eb1":"a\u0306\u0300","\u1eb5":"a\u0306\u0303","\u01ce":"a\u030c","\xe2":"a\u0302","\u1ea5":"a\u0302\u0301","\u1ea7":"a\u0302\u0300","\u1eab":"a\u0302\u0303","\u0227":"a\u0307","\u01e1":"a\u0307\u0304","\xe5":"a\u030a","\u01fb":"a\u030a\u0301","\u1e03":"b\u0307","\u0107":"c\u0301","\u1e09":"c\u0327\u0301","\u010d":"c\u030c","\u0109":"c\u0302","\u010b":"c\u0307","\xe7":"c\u0327","\u010f":"d\u030c","\u1e0b":"d\u0307","\u1e11":"d\u0327","\xe9":"e\u0301","\xe8":"e\u0300","\xeb":"e\u0308","\u1ebd":"e\u0303","\u0113":"e\u0304","\u1e17":"e\u0304\u0301","\u1e15":"e\u0304\u0300","\u0115":"e\u0306","\u1e1d":"e\u0327\u0306","\u011b":"e\u030c","\xea":"e\u0302","\u1ebf":"e\u0302\u0301","\u1ec1":"e\u0302\u0300","\u1ec5":"e\u0302\u0303","\u0117":"e\u0307","\u0229":"e\u0327","\u1e1f":"f\u0307","\u01f5":"g\u0301","\u1e21":"g\u0304","\u011f":"g\u0306","\u01e7":"g\u030c","\u011d":"g\u0302","\u0121":"g\u0307","\u0123":"g\u0327","\u1e27":"h\u0308","\u021f":"h\u030c","\u0125":"h\u0302","\u1e23":"h\u0307","\u1e29":"h\u0327","\xed":"i\u0301","\xec":"i\u0300","\xef":"i\u0308","\u1e2f":"i\u0308\u0301","\u0129":"i\u0303","\u012b":"i\u0304","\u012d":"i\u0306","\u01d0":"i\u030c","\xee":"i\u0302","\u01f0":"j\u030c","\u0135":"j\u0302","\u1e31":"k\u0301","\u01e9":"k\u030c","\u0137":"k\u0327","\u013a":"l\u0301","\u013e":"l\u030c","\u013c":"l\u0327","\u1e3f":"m\u0301","\u1e41":"m\u0307","\u0144":"n\u0301","\u01f9":"n\u0300","\xf1":"n\u0303","\u0148":"n\u030c","\u1e45":"n\u0307","\u0146":"n\u0327","\xf3":"o\u0301","\xf2":"o\u0300","\xf6":"o\u0308","\u022b":"o\u0308\u0304","\xf5":"o\u0303","\u1e4d":"o\u0303\u0301","\u1e4f":"o\u0303\u0308","\u022d":"o\u0303\u0304","\u014d":"o\u0304","\u1e53":"o\u0304\u0301","\u1e51":"o\u0304\u0300","\u014f":"o\u0306","\u01d2":"o\u030c","\xf4":"o\u0302","\u1ed1":"o\u0302\u0301","\u1ed3":"o\u0302\u0300","\u1ed7":"o\u0302\u0303","\u022f":"o\u0307","\u0231":"o\u0307\u0304","\u0151":"o\u030b","\u1e55":"p\u0301","\u1e57":"p\u0307","\u0155":"r\u0301","\u0159":"r\u030c","\u1e59":"r\u0307","\u0157":"r\u0327","\u015b":"s\u0301","\u1e65":"s\u0301\u0307","\u0161":"s\u030c","\u1e67":"s\u030c\u0307","\u015d":"s\u0302","\u1e61":"s\u0307","\u015f":"s\u0327","\u1e97":"t\u0308","\u0165":"t\u030c","\u1e6b":"t\u0307","\u0163":"t\u0327","\xfa":"u\u0301","\xf9":"u\u0300","\xfc":"u\u0308","\u01d8":"u\u0308\u0301","\u01dc":"u\u0308\u0300","\u01d6":"u\u0308\u0304","\u01da":"u\u0308\u030c","\u0169":"u\u0303","\u1e79":"u\u0303\u0301","\u016b":"u\u0304","\u1e7b":"u\u0304\u0308","\u016d":"u\u0306","\u01d4":"u\u030c","\xfb":"u\u0302","\u016f":"u\u030a","\u0171":"u\u030b","\u1e7d":"v\u0303","\u1e83":"w\u0301","\u1e81":"w\u0300","\u1e85":"w\u0308","\u0175":"w\u0302","\u1e87":"w\u0307","\u1e98":"w\u030a","\u1e8d":"x\u0308","\u1e8b":"x\u0307","\xfd":"y\u0301","\u1ef3":"y\u0300","\xff":"y\u0308","\u1ef9":"y\u0303","\u0233":"y\u0304","\u0177":"y\u0302","\u1e8f":"y\u0307","\u1e99":"y\u030a","\u017a":"z\u0301","\u017e":"z\u030c","\u1e91":"z\u0302","\u017c":"z\u0307","\xc1":"A\u0301","\xc0":"A\u0300","\xc4":"A\u0308","\u01de":"A\u0308\u0304","\xc3":"A\u0303","\u0100":"A\u0304","\u0102":"A\u0306","\u1eae":"A\u0306\u0301","\u1eb0":"A\u0306\u0300","\u1eb4":"A\u0306\u0303","\u01cd":"A\u030c","\xc2":"A\u0302","\u1ea4":"A\u0302\u0301","\u1ea6":"A\u0302\u0300","\u1eaa":"A\u0302\u0303","\u0226":"A\u0307","\u01e0":"A\u0307\u0304","\xc5":"A\u030a","\u01fa":"A\u030a\u0301","\u1e02":"B\u0307","\u0106":"C\u0301","\u1e08":"C\u0327\u0301","\u010c":"C\u030c","\u0108":"C\u0302","\u010a":"C\u0307","\xc7":"C\u0327","\u010e":"D\u030c","\u1e0a":"D\u0307","\u1e10":"D\u0327","\xc9":"E\u0301","\xc8":"E\u0300","\xcb":"E\u0308","\u1ebc":"E\u0303","\u0112":"E\u0304","\u1e16":"E\u0304\u0301","\u1e14":"E\u0304\u0300","\u0114":"E\u0306","\u1e1c":"E\u0327\u0306","\u011a":"E\u030c","\xca":"E\u0302","\u1ebe":"E\u0302\u0301","\u1ec0":"E\u0302\u0300","\u1ec4":"E\u0302\u0303","\u0116":"E\u0307","\u0228":"E\u0327","\u1e1e":"F\u0307","\u01f4":"G\u0301","\u1e20":"G\u0304","\u011e":"G\u0306","\u01e6":"G\u030c","\u011c":"G\u0302","\u0120":"G\u0307","\u0122":"G\u0327","\u1e26":"H\u0308","\u021e":"H\u030c","\u0124":"H\u0302","\u1e22":"H\u0307","\u1e28":"H\u0327","\xcd":"I\u0301","\xcc":"I\u0300","\xcf":"I\u0308","\u1e2e":"I\u0308\u0301","\u0128":"I\u0303","\u012a":"I\u0304","\u012c":"I\u0306","\u01cf":"I\u030c","\xce":"I\u0302","\u0130":"I\u0307","\u0134":"J\u0302","\u1e30":"K\u0301","\u01e8":"K\u030c","\u0136":"K\u0327","\u0139":"L\u0301","\u013d":"L\u030c","\u013b":"L\u0327","\u1e3e":"M\u0301","\u1e40":"M\u0307","\u0143":"N\u0301","\u01f8":"N\u0300","\xd1":"N\u0303","\u0147":"N\u030c","\u1e44":"N\u0307","\u0145":"N\u0327","\xd3":"O\u0301","\xd2":"O\u0300","\xd6":"O\u0308","\u022a":"O\u0308\u0304","\xd5":"O\u0303","\u1e4c":"O\u0303\u0301","\u1e4e":"O\u0303\u0308","\u022c":"O\u0303\u0304","\u014c":"O\u0304","\u1e52":"O\u0304\u0301","\u1e50":"O\u0304\u0300","\u014e":"O\u0306","\u01d1":"O\u030c","\xd4":"O\u0302","\u1ed0":"O\u0302\u0301","\u1ed2":"O\u0302\u0300","\u1ed6":"O\u0302\u0303","\u022e":"O\u0307","\u0230":"O\u0307\u0304","\u0150":"O\u030b","\u1e54":"P\u0301","\u1e56":"P\u0307","\u0154":"R\u0301","\u0158":"R\u030c","\u1e58":"R\u0307","\u0156":"R\u0327","\u015a":"S\u0301","\u1e64":"S\u0301\u0307","\u0160":"S\u030c","\u1e66":"S\u030c\u0307","\u015c":"S\u0302","\u1e60":"S\u0307","\u015e":"S\u0327","\u0164":"T\u030c","\u1e6a":"T\u0307","\u0162":"T\u0327","\xda":"U\u0301","\xd9":"U\u0300","\xdc":"U\u0308","\u01d7":"U\u0308\u0301","\u01db":"U\u0308\u0300","\u01d5":"U\u0308\u0304","\u01d9":"U\u0308\u030c","\u0168":"U\u0303","\u1e78":"U\u0303\u0301","\u016a":"U\u0304","\u1e7a":"U\u0304\u0308","\u016c":"U\u0306","\u01d3":"U\u030c","\xdb":"U\u0302","\u016e":"U\u030a","\u0170":"U\u030b","\u1e7c":"V\u0303","\u1e82":"W\u0301","\u1e80":"W\u0300","\u1e84":"W\u0308","\u0174":"W\u0302","\u1e86":"W\u0307","\u1e8c":"X\u0308","\u1e8a":"X\u0307","\xdd":"Y\u0301","\u1ef2":"Y\u0300","\u0178":"Y\u0308","\u1ef8":"Y\u0303","\u0232":"Y\u0304","\u0176":"Y\u0302","\u1e8e":"Y\u0307","\u0179":"Z\u0301","\u017d":"Z\u030c","\u1e90":"Z\u0302","\u017b":"Z\u0307","\u03ac":"\u03b1\u0301","\u1f70":"\u03b1\u0300","\u1fb1":"\u03b1\u0304","\u1fb0":"\u03b1\u0306","\u03ad":"\u03b5\u0301","\u1f72":"\u03b5\u0300","\u03ae":"\u03b7\u0301","\u1f74":"\u03b7\u0300","\u03af":"\u03b9\u0301","\u1f76":"\u03b9\u0300","\u03ca":"\u03b9\u0308","\u0390":"\u03b9\u0308\u0301","\u1fd2":"\u03b9\u0308\u0300","\u1fd1":"\u03b9\u0304","\u1fd0":"\u03b9\u0306","\u03cc":"\u03bf\u0301","\u1f78":"\u03bf\u0300","\u03cd":"\u03c5\u0301","\u1f7a":"\u03c5\u0300","\u03cb":"\u03c5\u0308","\u03b0":"\u03c5\u0308\u0301","\u1fe2":"\u03c5\u0308\u0300","\u1fe1":"\u03c5\u0304","\u1fe0":"\u03c5\u0306","\u03ce":"\u03c9\u0301","\u1f7c":"\u03c9\u0300","\u038e":"\u03a5\u0301","\u1fea":"\u03a5\u0300","\u03ab":"\u03a5\u0308","\u1fe9":"\u03a5\u0304","\u1fe8":"\u03a5\u0306","\u038f":"\u03a9\u0301","\u1ffa":"\u03a9\u0300"},Wn=function(){function e(e,t){this.mode=void 0,this.gullet=void 0,this.settings=void 0,this.leftrightDepth=void 0,this.nextToken=void 0,this.mode="math",this.gullet=new Gn(e,t,this.mode),this.settings=t,this.leftrightDepth=0}var t=e.prototype;return t.expect=function(e,t){if(void 0===t&&(t=!0),this.fetch().text!==e)throw new n("Expected '"+e+"', got '"+this.fetch().text+"'",this.fetch());t&&this.consume()},t.consume=function(){this.nextToken=null},t.fetch=function(){return null==this.nextToken&&(this.nextToken=this.gullet.expandNextToken()),this.nextToken},t.switchMode=function(e){this.mode=e,this.gullet.switchMode(e)},t.parse=function(){this.settings.globalGroup||this.gullet.beginGroup(),this.settings.colorIsTextColor&&this.gullet.macros.set("\\color","\\textcolor");try{var e=this.parseExpression(!1);return this.expect("EOF"),this.settings.globalGroup||this.gullet.endGroup(),e}finally{this.gullet.endGroups()}},t.parseExpression=function(t,r){for(var n=[];;){"math"===this.mode&&this.consumeSpaces();var a=this.fetch();if(-1!==e.endOfExpression.indexOf(a.text))break;if(r&&a.text===r)break;if(t&&Nn[a.text]&&Nn[a.text].infix)break;var i=this.parseAtom(r);if(!i)break;"internal"!==i.type&&n.push(i)}return"text"===this.mode&&this.formLigatures(n),this.handleInfixNodes(n)},t.handleInfixNodes=function(e){for(var t,r=-1,a=0;a=0&&this.settings.reportNonstrict("unicodeTextInMathMode",'Latin-1/Unicode text character "'+t[0]+'" used in math mode',e);var s,l=X[this.mode][t].group,h=qn.range(e);if(U.hasOwnProperty(l)){var m=l;s={type:"atom",mode:this.mode,family:m,loc:h,text:t}}else s={type:l,mode:this.mode,loc:h,text:t};i=s}else{if(!(t.charCodeAt(0)>=128))return null;this.settings.strict&&(w(t.charCodeAt(0))?"math"===this.mode&&this.settings.reportNonstrict("unicodeTextInMathMode",'Unicode text character "'+t[0]+'" used in math mode',e):this.settings.reportNonstrict("unknownSymbol",'Unrecognized Unicode character "'+t[0]+'" ('+t.charCodeAt(0)+")",e)),i={type:"textord",mode:"text",loc:qn.range(e),text:t}}if(this.consume(),o)for(var c=0;c0&&(a.push({type:"text",data:e.slice(0,n)}),e=e.slice(n));var l=t.findIndex((function(t){return e.startsWith(t.left)}));if(-1===(n=r(t[l].right,e,t[l].left.length)))break;var d=e.slice(0,n+t[l].right.length),s=i.test(d)?d:e.slice(t[l].left.length,n);a.push({type:"math",data:s,rawData:d,display:t[l].display}),e=e.slice(n+t[l].right.length)}return""!==e&&a.push({type:"text",data:e}),a},l=function(e,r){var n=o(e,r.delimiters);if(1===n.length&&"text"===n[0].type)return null;for(var a=document.createDocumentFragment(),i=0;i":"is greater than","<":"is less than","\\leftarrow":"left arrow","\\Leftarrow":"left arrow","\\rightarrow":"right arrow","\\Rightarrow":"right arrow",":":"colon"},d={"\\underleftarrow":"left arrow","\\underrightarrow":"right arrow","\\underleftrightarrow":"left-right arrow","\\undergroup":"group","\\underlinesegment":"line segment","\\utilde":"tilde"},b=function(e,r,a){var t;e&&(/^\d+$/.test(t="open"===r?e in l?l[e]:s[e]||e:"close"===r?e in c?c[e]:s[e]||e:"bin"===r?u[e]||e:"rel"===r?p[e]||e:s[e]||e)&&a.length>0&&/^\d+$/.test(a[a.length-1])?a[a.length-1]+=t:t&&a.push(t))},h=function(e,r){var a=[];e.push(a),r(a)},m=function(e,r,a){switch(e.type){case"accent":h(r,(function(r){f(e.base,r,a),r.push("with"),b(e.label,"normal",r),r.push("on top")}));break;case"accentUnder":h(r,(function(r){f(e.base,r,a),r.push("with"),b(d[e.label],"normal",r),r.push("underneath")}));break;case"accent-token":break;case"atom":var t=e.text;switch(e.family){case"bin":b(t,"bin",r);break;case"close":b(t,"close",r);break;case"inner":b(e.text,"inner",r);break;case"open":b(t,"open",r);break;case"punct":b(t,"punct",r);break;case"rel":b(t,"rel",r);break;default:throw e.family,new Error('"'+e.family+'" is not a valid atom type')}break;case"color":var o=e.color.replace(/katex-/,"");h(r,(function(r){r.push("start color "+o),f(e.body,r,a),r.push("end color "+o)}));break;case"color-token":break;case"delimsizing":e.delim&&"."!==e.delim&&b(e.delim,"normal",r);break;case"genfrac":h(r,(function(r){var t=e.leftDelim,o=e.rightDelim;e.hasBarLine?(r.push("start fraction"),t&&b(t,"open",r),f(e.numer,r,a),r.push("divided by"),f(e.denom,r,a),o&&b(o,"close",r),r.push("end fraction")):(r.push("start binomial"),t&&b(t,"open",r),f(e.numer,r,a),r.push("over"),f(e.denom,r,a),o&&b(o,"close",r),r.push("end binomial"))}));break;case"hbox":f(e.body,r,a);break;case"kern":break;case"leftright":h(r,(function(r){b(e.left,"open",r),f(e.body,r,a),b(e.right,"close",r)}));break;case"leftright-right":break;case"lap":f(e.body,r,a);break;case"mathord":b(e.text,"normal",r);break;case"op":var n=e.body,s=e.name;n?f(n,r,a):s&&b(s,"normal",r);break;case"op-token":b(e.text,a,r);break;case"ordgroup":f(e.body,r,a);break;case"overline":h(r,(function(r){r.push("start overline"),f(e.body,r,a),r.push("end overline")}));break;case"phantom":r.push("empty space");break;case"raisebox":f(e.body,r,a);break;case"rule":r.push("rectangle");break;case"sizing":f(e.body,r,a);break;case"spacing":r.push("space");break;case"styling":f(e.body,r,a);break;case"sqrt":h(r,(function(r){var t=e.body,o=e.index;if(o)return"3"===y(f(o,[],a)).join(",")?(r.push("cube root of"),f(t,r,a),void r.push("end cube root")):(r.push("root"),r.push("start index"),f(o,r,a),void r.push("end index"));r.push("square root of"),f(t,r,a),r.push("end square root")}));break;case"supsub":var l=e.base,c=e.sub,u=e.sup,p=!1;if(l&&(f(l,r,a),p="op"===l.type&&"\\log"===l.name),c){var m=p?"base":"subscript";h(r,(function(e){e.push("start "+m),f(c,e,a),e.push("end "+m)}))}u&&h(r,(function(e){var r=y(f(u,[],a)).join(",");r in i?e.push(i[r]):(e.push("start superscript"),f(u,e,a),e.push("end superscript"))}));break;case"text":if("\\textbf"===e.font){h(r,(function(r){r.push("start bold text"),f(e.body,r,a),r.push("end bold text")}));break}h(r,(function(r){r.push("start text"),f(e.body,r,a),r.push("end text")}));break;case"textord":b(e.text,a,r);break;case"smash":f(e.body,r,a);break;case"enclose":if(/cancel/.test(e.label)){h(r,(function(r){r.push("start cancel"),f(e.body,r,a),r.push("end cancel")}));break}if(/box/.test(e.label)){h(r,(function(r){r.push("start box"),f(e.body,r,a),r.push("end box")}));break}if(/sout/.test(e.label)){h(r,(function(r){r.push("start strikeout"),f(e.body,r,a),r.push("end strikeout")}));break}if(/phase/.test(e.label)){h(r,(function(r){r.push("start phase angle"),f(e.body,r,a),r.push("end phase angle")}));break}throw new Error("KaTeX-a11y: enclose node with "+e.label+" not supported yet");case"vcenter":f(e.body,r,a);break;case"vphantom":throw new Error("KaTeX-a11y: vphantom not implemented yet");case"hphantom":throw new Error("KaTeX-a11y: hphantom not implemented yet");case"operatorname":f(e.body,r,a);break;case"array":throw new Error("KaTeX-a11y: array not implemented yet");case"raw":throw new Error("KaTeX-a11y: raw not implemented yet");case"size":break;case"url":throw new Error("KaTeX-a11y: url not implemented yet");case"tag":throw new Error("KaTeX-a11y: tag not implemented yet");case"verb":b("start verbatim","normal",r),b(e.body,"normal",r),b("end verbatim","normal",r);break;case"environment":throw new Error("KaTeX-a11y: environment not implemented yet");case"horizBrace":b("start "+e.label.slice(1),"normal",r),f(e.base,r,a),b("end "+e.label.slice(1),"normal",r);break;case"infix":break;case"includegraphics":throw new Error("KaTeX-a11y: includegraphics not implemented yet");case"font":f(e.body,r,a);break;case"href":throw new Error("KaTeX-a11y: href not implemented yet");case"cr":throw new Error("KaTeX-a11y: cr not implemented yet");case"underline":h(r,(function(r){r.push("start underline"),f(e.body,r,a),r.push("end underline")}));break;case"xArrow":throw new Error("KaTeX-a11y: xArrow not implemented yet");case"cdlabel":throw new Error("KaTeX-a11y: cdlabel not implemented yet");case"cdlabelparent":throw new Error("KaTeX-a11y: cdlabelparent not implemented yet");case"mclass":var w=e.mclass.slice(1);f(e.body,r,w);break;case"mathchoice":f(e.text,r,a);break;case"htmlmathml":f(e.mathml,r,a);break;case"middle":b(e.delim,a,r);break;case"internal":break;case"html":f(e.body,r,a);break;default:throw e.type,new Error("KaTeX a11y un-recognized type: "+e.type)}},f=function e(r,a,t){if(void 0===a&&(a=[]),r instanceof Array)for(var o=0;o { + const htmlDiv = document.getElementById("html-output"); + renderMathInElement(htmlDiv, { + delimiters: [ + {left: "$$", right: "$$", display: true}, + {left: "$", right: "$", display: false} + ], + throwError: false, + }); +} + +const download = async (filename, mime, text) => { + let ele = document.createElement("a"); + ele.setAttribute("href", "data:" + mime + ";charset=utf-8," + encodeURIComponent(text)); + ele.setAttribute("download", filename); + ele.style.display = 'none'; + document.body.appendChild(ele); + ele.click(); + document.body.removeChild(ele); +}; + +const downloadMd = async () => { + const md = document.getElementById("markdown-input"); + download("markdown.md", "text/plain", md.value); +}; + +const getRequiredCss = async () => { + let css = ""; + const css1 = document.getElementById("main-stylesheet").sheet; + const css2 = document.getElementById("math-stylesheet").sheet; + css += Array.from(css1.cssRules).map(rule => rule.cssText).join(' '); + css += Array.from(css2.cssRules).map(rule => rule.cssText).join(' '); + return css +}; + +const downloadHTML = async () => { + const css = await getRequiredCss(); + const htmlEle = document.getElementById("html-output"); + let html = ""; + html += htmlEle.innerHTML; + html += ""; + download("output.html", "text/html", html); +}; + +const setStatus = async (msg) => { + const statusBox = document.getElementById("status"); + statusBox.innerText = msg; +}; + +const saveHTML = async () => { + setStatus("Saving HTML..."); + const html = document.getElementById("html-output"); + localStorage.setItem(SAVE_HTML_KEY, html.innerHTML); + setStatus("Saved"); +}; + +const saveMd = async () => { + setStatus("Saving markdown..."); + const md = document.getElementById("markdown-input"); + localStorage.setItem(SAVE_MD_KEY, md.value); + setStatus("Saved"); +}; + +const renderHTML = async () => { + setStatus("Rendering..."); + const md = new remarkable.Remarkable({ + html: true, + }); + const markdownInput = document.getElementById("markdown-input"); + const htmlOutput = document.getElementById("html-output"); + const newHtml = md.render(markdownInput.value); + htmlOutput.innerHTML = newHtml; + updateRender(); + saveMd(); + saveHTML(); + setStatus("Done"); +} + +document.addEventListener("DOMContentLoaded", async () => { + console.log("loaded"); + + // preload save + if (localStorage.getItem(SAVE_HTML_KEY) !== null){ + const html = document.getElementById("html-output"); + html.innerHTML = localStorage.getItem(SAVE_HTML_KEY); + } + if (localStorage.getItem(SAVE_MD_KEY) !== null){ + const md = document.getElementById("markdown-input"); + md.value = localStorage.getItem(SAVE_MD_KEY); + } + + const updateBtn = document.getElementById("update-html"); + updateBtn.addEventListener("click", renderHTML); + + const saveMdBtn = document.getElementById("save-markdown"); + saveMdBtn.addEventListener("click", saveMd); + + const saveHTMLBtn = document.getElementById("save-html"); + saveHTMLBtn.addEventListener("click", saveHTML); + + const downloadMdBtn = document.getElementById("download-markdown"); + downloadMdBtn.addEventListener("click", downloadMd); + const downloadHTMLBtn = document.getElementById("download-html"); + downloadHTMLBtn.addEventListener("click", downloadHTML); +}); diff --git a/_site/assets/js/md/remarkable.js b/_site/assets/js/md/remarkable.js new file mode 100644 index 0000000..cebb38f --- /dev/null +++ b/_site/assets/js/md/remarkable.js @@ -0,0 +1 @@ +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).remarkable={})}(this,function(e){"use strict";var t={Aacute:"Á",aacute:"á",Abreve:"Ă",abreve:"ă",ac:"∾",acd:"∿",acE:"∾̳",Acirc:"Â",acirc:"â",acute:"´",Acy:"А",acy:"а",AElig:"Æ",aelig:"æ",af:"⁡",Afr:"𝔄",afr:"𝔞",Agrave:"À",agrave:"à",alefsym:"ℵ",aleph:"ℵ",Alpha:"Α",alpha:"α",Amacr:"Ā",amacr:"ā",amalg:"⨿",AMP:"&",amp:"&",And:"⩓",and:"∧",andand:"⩕",andd:"⩜",andslope:"⩘",andv:"⩚",ang:"∠",ange:"⦤",angle:"∠",angmsd:"∡",angmsdaa:"⦨",angmsdab:"⦩",angmsdac:"⦪",angmsdad:"⦫",angmsdae:"⦬",angmsdaf:"⦭",angmsdag:"⦮",angmsdah:"⦯",angrt:"∟",angrtvb:"⊾",angrtvbd:"⦝",angsph:"∢",angst:"Å",angzarr:"⍼",Aogon:"Ą",aogon:"ą",Aopf:"𝔸",aopf:"𝕒",ap:"≈",apacir:"⩯",apE:"⩰",ape:"≊",apid:"≋",apos:"'",ApplyFunction:"⁡",approx:"≈",approxeq:"≊",Aring:"Å",aring:"å",Ascr:"𝒜",ascr:"𝒶",Assign:"≔",ast:"*",asymp:"≈",asympeq:"≍",Atilde:"Ã",atilde:"ã",Auml:"Ä",auml:"ä",awconint:"∳",awint:"⨑",backcong:"≌",backepsilon:"϶",backprime:"‵",backsim:"∽",backsimeq:"⋍",Backslash:"∖",Barv:"⫧",barvee:"⊽",Barwed:"⌆",barwed:"⌅",barwedge:"⌅",bbrk:"⎵",bbrktbrk:"⎶",bcong:"≌",Bcy:"Б",bcy:"б",bdquo:"„",becaus:"∵",Because:"∵",because:"∵",bemptyv:"⦰",bepsi:"϶",bernou:"ℬ",Bernoullis:"ℬ",Beta:"Β",beta:"β",beth:"ℶ",between:"≬",Bfr:"𝔅",bfr:"𝔟",bigcap:"⋂",bigcirc:"◯",bigcup:"⋃",bigodot:"⨀",bigoplus:"⨁",bigotimes:"⨂",bigsqcup:"⨆",bigstar:"★",bigtriangledown:"▽",bigtriangleup:"△",biguplus:"⨄",bigvee:"⋁",bigwedge:"⋀",bkarow:"⤍",blacklozenge:"⧫",blacksquare:"▪",blacktriangle:"▴",blacktriangledown:"▾",blacktriangleleft:"◂",blacktriangleright:"▸",blank:"␣",blk12:"▒",blk14:"░",blk34:"▓",block:"█",bne:"=⃥",bnequiv:"≡⃥",bNot:"⫭",bnot:"⌐",Bopf:"𝔹",bopf:"𝕓",bot:"⊥",bottom:"⊥",bowtie:"⋈",boxbox:"⧉",boxDL:"╗",boxDl:"╖",boxdL:"╕",boxdl:"┐",boxDR:"╔",boxDr:"╓",boxdR:"╒",boxdr:"┌",boxH:"═",boxh:"─",boxHD:"╦",boxHd:"╤",boxhD:"╥",boxhd:"┬",boxHU:"╩",boxHu:"╧",boxhU:"╨",boxhu:"┴",boxminus:"⊟",boxplus:"⊞",boxtimes:"⊠",boxUL:"╝",boxUl:"╜",boxuL:"╛",boxul:"┘",boxUR:"╚",boxUr:"╙",boxuR:"╘",boxur:"└",boxV:"║",boxv:"│",boxVH:"╬",boxVh:"╫",boxvH:"╪",boxvh:"┼",boxVL:"╣",boxVl:"╢",boxvL:"╡",boxvl:"┤",boxVR:"╠",boxVr:"╟",boxvR:"╞",boxvr:"├",bprime:"‵",Breve:"˘",breve:"˘",brvbar:"¦",Bscr:"ℬ",bscr:"𝒷",bsemi:"⁏",bsim:"∽",bsime:"⋍",bsol:"\\",bsolb:"⧅",bsolhsub:"⟈",bull:"•",bullet:"•",bump:"≎",bumpE:"⪮",bumpe:"≏",Bumpeq:"≎",bumpeq:"≏",Cacute:"Ć",cacute:"ć",Cap:"⋒",cap:"∩",capand:"⩄",capbrcup:"⩉",capcap:"⩋",capcup:"⩇",capdot:"⩀",CapitalDifferentialD:"ⅅ",caps:"∩︀",caret:"⁁",caron:"ˇ",Cayleys:"ℭ",ccaps:"⩍",Ccaron:"Č",ccaron:"č",Ccedil:"Ç",ccedil:"ç",Ccirc:"Ĉ",ccirc:"ĉ",Cconint:"∰",ccups:"⩌",ccupssm:"⩐",Cdot:"Ċ",cdot:"ċ",cedil:"¸",Cedilla:"¸",cemptyv:"⦲",cent:"¢",CenterDot:"·",centerdot:"·",Cfr:"ℭ",cfr:"𝔠",CHcy:"Ч",chcy:"ч",check:"✓",checkmark:"✓",Chi:"Χ",chi:"χ",cir:"○",circ:"ˆ",circeq:"≗",circlearrowleft:"↺",circlearrowright:"↻",circledast:"⊛",circledcirc:"⊚",circleddash:"⊝",CircleDot:"⊙",circledR:"®",circledS:"Ⓢ",CircleMinus:"⊖",CirclePlus:"⊕",CircleTimes:"⊗",cirE:"⧃",cire:"≗",cirfnint:"⨐",cirmid:"⫯",cirscir:"⧂",ClockwiseContourIntegral:"∲",CloseCurlyDoubleQuote:"”",CloseCurlyQuote:"’",clubs:"♣",clubsuit:"♣",Colon:"∷",colon:":",Colone:"⩴",colone:"≔",coloneq:"≔",comma:",",commat:"@",comp:"∁",compfn:"∘",complement:"∁",complexes:"ℂ",cong:"≅",congdot:"⩭",Congruent:"≡",Conint:"∯",conint:"∮",ContourIntegral:"∮",Copf:"ℂ",copf:"𝕔",coprod:"∐",Coproduct:"∐",COPY:"©",copy:"©",copysr:"℗",CounterClockwiseContourIntegral:"∳",crarr:"↵",Cross:"⨯",cross:"✗",Cscr:"𝒞",cscr:"𝒸",csub:"⫏",csube:"⫑",csup:"⫐",csupe:"⫒",ctdot:"⋯",cudarrl:"⤸",cudarrr:"⤵",cuepr:"⋞",cuesc:"⋟",cularr:"↶",cularrp:"⤽",Cup:"⋓",cup:"∪",cupbrcap:"⩈",CupCap:"≍",cupcap:"⩆",cupcup:"⩊",cupdot:"⊍",cupor:"⩅",cups:"∪︀",curarr:"↷",curarrm:"⤼",curlyeqprec:"⋞",curlyeqsucc:"⋟",curlyvee:"⋎",curlywedge:"⋏",curren:"¤",curvearrowleft:"↶",curvearrowright:"↷",cuvee:"⋎",cuwed:"⋏",cwconint:"∲",cwint:"∱",cylcty:"⌭",Dagger:"‡",dagger:"†",daleth:"ℸ",Darr:"↡",dArr:"⇓",darr:"↓",dash:"‐",Dashv:"⫤",dashv:"⊣",dbkarow:"⤏",dblac:"˝",Dcaron:"Ď",dcaron:"ď",Dcy:"Д",dcy:"д",DD:"ⅅ",dd:"ⅆ",ddagger:"‡",ddarr:"⇊",DDotrahd:"⤑",ddotseq:"⩷",deg:"°",Del:"∇",Delta:"Δ",delta:"δ",demptyv:"⦱",dfisht:"⥿",Dfr:"𝔇",dfr:"𝔡",dHar:"⥥",dharl:"⇃",dharr:"⇂",DiacriticalAcute:"´",DiacriticalDot:"˙",DiacriticalDoubleAcute:"˝",DiacriticalGrave:"`",DiacriticalTilde:"˜",diam:"⋄",Diamond:"⋄",diamond:"⋄",diamondsuit:"♦",diams:"♦",die:"¨",DifferentialD:"ⅆ",digamma:"ϝ",disin:"⋲",div:"÷",divide:"÷",divideontimes:"⋇",divonx:"⋇",DJcy:"Ђ",djcy:"ђ",dlcorn:"⌞",dlcrop:"⌍",dollar:"$",Dopf:"𝔻",dopf:"𝕕",Dot:"¨",dot:"˙",DotDot:"⃜",doteq:"≐",doteqdot:"≑",DotEqual:"≐",dotminus:"∸",dotplus:"∔",dotsquare:"⊡",doublebarwedge:"⌆",DoubleContourIntegral:"∯",DoubleDot:"¨",DoubleDownArrow:"⇓",DoubleLeftArrow:"⇐",DoubleLeftRightArrow:"⇔",DoubleLeftTee:"⫤",DoubleLongLeftArrow:"⟸",DoubleLongLeftRightArrow:"⟺",DoubleLongRightArrow:"⟹",DoubleRightArrow:"⇒",DoubleRightTee:"⊨",DoubleUpArrow:"⇑",DoubleUpDownArrow:"⇕",DoubleVerticalBar:"∥",DownArrow:"↓",Downarrow:"⇓",downarrow:"↓",DownArrowBar:"⤓",DownArrowUpArrow:"⇵",DownBreve:"̑",downdownarrows:"⇊",downharpoonleft:"⇃",downharpoonright:"⇂",DownLeftRightVector:"⥐",DownLeftTeeVector:"⥞",DownLeftVector:"↽",DownLeftVectorBar:"⥖",DownRightTeeVector:"⥟",DownRightVector:"⇁",DownRightVectorBar:"⥗",DownTee:"⊤",DownTeeArrow:"↧",drbkarow:"⤐",drcorn:"⌟",drcrop:"⌌",Dscr:"𝒟",dscr:"𝒹",DScy:"Ѕ",dscy:"ѕ",dsol:"⧶",Dstrok:"Đ",dstrok:"đ",dtdot:"⋱",dtri:"▿",dtrif:"▾",duarr:"⇵",duhar:"⥯",dwangle:"⦦",DZcy:"Џ",dzcy:"џ",dzigrarr:"⟿",Eacute:"É",eacute:"é",easter:"⩮",Ecaron:"Ě",ecaron:"ě",ecir:"≖",Ecirc:"Ê",ecirc:"ê",ecolon:"≕",Ecy:"Э",ecy:"э",eDDot:"⩷",Edot:"Ė",eDot:"≑",edot:"ė",ee:"ⅇ",efDot:"≒",Efr:"𝔈",efr:"𝔢",eg:"⪚",Egrave:"È",egrave:"è",egs:"⪖",egsdot:"⪘",el:"⪙",Element:"∈",elinters:"⏧",ell:"ℓ",els:"⪕",elsdot:"⪗",Emacr:"Ē",emacr:"ē",empty:"∅",emptyset:"∅",EmptySmallSquare:"◻",emptyv:"∅",EmptyVerySmallSquare:"▫",emsp:" ",emsp13:" ",emsp14:" ",ENG:"Ŋ",eng:"ŋ",ensp:" ",Eogon:"Ę",eogon:"ę",Eopf:"𝔼",eopf:"𝕖",epar:"⋕",eparsl:"⧣",eplus:"⩱",epsi:"ε",Epsilon:"Ε",epsilon:"ε",epsiv:"ϵ",eqcirc:"≖",eqcolon:"≕",eqsim:"≂",eqslantgtr:"⪖",eqslantless:"⪕",Equal:"⩵",equals:"=",EqualTilde:"≂",equest:"≟",Equilibrium:"⇌",equiv:"≡",equivDD:"⩸",eqvparsl:"⧥",erarr:"⥱",erDot:"≓",Escr:"ℰ",escr:"ℯ",esdot:"≐",Esim:"⩳",esim:"≂",Eta:"Η",eta:"η",ETH:"Ð",eth:"ð",Euml:"Ë",euml:"ë",euro:"€",excl:"!",exist:"∃",Exists:"∃",expectation:"ℰ",ExponentialE:"ⅇ",exponentiale:"ⅇ",fallingdotseq:"≒",Fcy:"Ф",fcy:"ф",female:"♀",ffilig:"ffi",fflig:"ff",ffllig:"ffl",Ffr:"𝔉",ffr:"𝔣",filig:"fi",FilledSmallSquare:"◼",FilledVerySmallSquare:"▪",fjlig:"fj",flat:"♭",fllig:"fl",fltns:"▱",fnof:"ƒ",Fopf:"𝔽",fopf:"𝕗",ForAll:"∀",forall:"∀",fork:"⋔",forkv:"⫙",Fouriertrf:"ℱ",fpartint:"⨍",frac12:"½",frac13:"⅓",frac14:"¼",frac15:"⅕",frac16:"⅙",frac18:"⅛",frac23:"⅔",frac25:"⅖",frac34:"¾",frac35:"⅗",frac38:"⅜",frac45:"⅘",frac56:"⅚",frac58:"⅝",frac78:"⅞",frasl:"⁄",frown:"⌢",Fscr:"ℱ",fscr:"𝒻",gacute:"ǵ",Gamma:"Γ",gamma:"γ",Gammad:"Ϝ",gammad:"ϝ",gap:"⪆",Gbreve:"Ğ",gbreve:"ğ",Gcedil:"Ģ",Gcirc:"Ĝ",gcirc:"ĝ",Gcy:"Г",gcy:"г",Gdot:"Ġ",gdot:"ġ",gE:"≧",ge:"≥",gEl:"⪌",gel:"⋛",geq:"≥",geqq:"≧",geqslant:"⩾",ges:"⩾",gescc:"⪩",gesdot:"⪀",gesdoto:"⪂",gesdotol:"⪄",gesl:"⋛︀",gesles:"⪔",Gfr:"𝔊",gfr:"𝔤",Gg:"⋙",gg:"≫",ggg:"⋙",gimel:"ℷ",GJcy:"Ѓ",gjcy:"ѓ",gl:"≷",gla:"⪥",glE:"⪒",glj:"⪤",gnap:"⪊",gnapprox:"⪊",gnE:"≩",gne:"⪈",gneq:"⪈",gneqq:"≩",gnsim:"⋧",Gopf:"𝔾",gopf:"𝕘",grave:"`",GreaterEqual:"≥",GreaterEqualLess:"⋛",GreaterFullEqual:"≧",GreaterGreater:"⪢",GreaterLess:"≷",GreaterSlantEqual:"⩾",GreaterTilde:"≳",Gscr:"𝒢",gscr:"ℊ",gsim:"≳",gsime:"⪎",gsiml:"⪐",GT:">",Gt:"≫",gt:">",gtcc:"⪧",gtcir:"⩺",gtdot:"⋗",gtlPar:"⦕",gtquest:"⩼",gtrapprox:"⪆",gtrarr:"⥸",gtrdot:"⋗",gtreqless:"⋛",gtreqqless:"⪌",gtrless:"≷",gtrsim:"≳",gvertneqq:"≩︀",gvnE:"≩︀",Hacek:"ˇ",hairsp:" ",half:"½",hamilt:"ℋ",HARDcy:"Ъ",hardcy:"ъ",hArr:"⇔",harr:"↔",harrcir:"⥈",harrw:"↭",Hat:"^",hbar:"ℏ",Hcirc:"Ĥ",hcirc:"ĥ",hearts:"♥",heartsuit:"♥",hellip:"…",hercon:"⊹",Hfr:"ℌ",hfr:"𝔥",HilbertSpace:"ℋ",hksearow:"⤥",hkswarow:"⤦",hoarr:"⇿",homtht:"∻",hookleftarrow:"↩",hookrightarrow:"↪",Hopf:"ℍ",hopf:"𝕙",horbar:"―",HorizontalLine:"─",Hscr:"ℋ",hscr:"𝒽",hslash:"ℏ",Hstrok:"Ħ",hstrok:"ħ",HumpDownHump:"≎",HumpEqual:"≏",hybull:"⁃",hyphen:"‐",Iacute:"Í",iacute:"í",ic:"⁣",Icirc:"Î",icirc:"î",Icy:"И",icy:"и",Idot:"İ",IEcy:"Е",iecy:"е",iexcl:"¡",iff:"⇔",Ifr:"ℑ",ifr:"𝔦",Igrave:"Ì",igrave:"ì",ii:"ⅈ",iiiint:"⨌",iiint:"∭",iinfin:"⧜",iiota:"℩",IJlig:"IJ",ijlig:"ij",Im:"ℑ",Imacr:"Ī",imacr:"ī",image:"ℑ",ImaginaryI:"ⅈ",imagline:"ℐ",imagpart:"ℑ",imath:"ı",imof:"⊷",imped:"Ƶ",Implies:"⇒",in:"∈",incare:"℅",infin:"∞",infintie:"⧝",inodot:"ı",Int:"∬",int:"∫",intcal:"⊺",integers:"ℤ",Integral:"∫",intercal:"⊺",Intersection:"⋂",intlarhk:"⨗",intprod:"⨼",InvisibleComma:"⁣",InvisibleTimes:"⁢",IOcy:"Ё",iocy:"ё",Iogon:"Į",iogon:"į",Iopf:"𝕀",iopf:"𝕚",Iota:"Ι",iota:"ι",iprod:"⨼",iquest:"¿",Iscr:"ℐ",iscr:"𝒾",isin:"∈",isindot:"⋵",isinE:"⋹",isins:"⋴",isinsv:"⋳",isinv:"∈",it:"⁢",Itilde:"Ĩ",itilde:"ĩ",Iukcy:"І",iukcy:"і",Iuml:"Ï",iuml:"ï",Jcirc:"Ĵ",jcirc:"ĵ",Jcy:"Й",jcy:"й",Jfr:"𝔍",jfr:"𝔧",jmath:"ȷ",Jopf:"𝕁",jopf:"𝕛",Jscr:"𝒥",jscr:"𝒿",Jsercy:"Ј",jsercy:"ј",Jukcy:"Є",jukcy:"є",Kappa:"Κ",kappa:"κ",kappav:"ϰ",Kcedil:"Ķ",kcedil:"ķ",Kcy:"К",kcy:"к",Kfr:"𝔎",kfr:"𝔨",kgreen:"ĸ",KHcy:"Х",khcy:"х",KJcy:"Ќ",kjcy:"ќ",Kopf:"𝕂",kopf:"𝕜",Kscr:"𝒦",kscr:"𝓀",lAarr:"⇚",Lacute:"Ĺ",lacute:"ĺ",laemptyv:"⦴",lagran:"ℒ",Lambda:"Λ",lambda:"λ",Lang:"⟪",lang:"⟨",langd:"⦑",langle:"⟨",lap:"⪅",Laplacetrf:"ℒ",laquo:"«",Larr:"↞",lArr:"⇐",larr:"←",larrb:"⇤",larrbfs:"⤟",larrfs:"⤝",larrhk:"↩",larrlp:"↫",larrpl:"⤹",larrsim:"⥳",larrtl:"↢",lat:"⪫",lAtail:"⤛",latail:"⤙",late:"⪭",lates:"⪭︀",lBarr:"⤎",lbarr:"⤌",lbbrk:"❲",lbrace:"{",lbrack:"[",lbrke:"⦋",lbrksld:"⦏",lbrkslu:"⦍",Lcaron:"Ľ",lcaron:"ľ",Lcedil:"Ļ",lcedil:"ļ",lceil:"⌈",lcub:"{",Lcy:"Л",lcy:"л",ldca:"⤶",ldquo:"“",ldquor:"„",ldrdhar:"⥧",ldrushar:"⥋",ldsh:"↲",lE:"≦",le:"≤",LeftAngleBracket:"⟨",LeftArrow:"←",Leftarrow:"⇐",leftarrow:"←",LeftArrowBar:"⇤",LeftArrowRightArrow:"⇆",leftarrowtail:"↢",LeftCeiling:"⌈",LeftDoubleBracket:"⟦",LeftDownTeeVector:"⥡",LeftDownVector:"⇃",LeftDownVectorBar:"⥙",LeftFloor:"⌊",leftharpoondown:"↽",leftharpoonup:"↼",leftleftarrows:"⇇",LeftRightArrow:"↔",Leftrightarrow:"⇔",leftrightarrow:"↔",leftrightarrows:"⇆",leftrightharpoons:"⇋",leftrightsquigarrow:"↭",LeftRightVector:"⥎",LeftTee:"⊣",LeftTeeArrow:"↤",LeftTeeVector:"⥚",leftthreetimes:"⋋",LeftTriangle:"⊲",LeftTriangleBar:"⧏",LeftTriangleEqual:"⊴",LeftUpDownVector:"⥑",LeftUpTeeVector:"⥠",LeftUpVector:"↿",LeftUpVectorBar:"⥘",LeftVector:"↼",LeftVectorBar:"⥒",lEg:"⪋",leg:"⋚",leq:"≤",leqq:"≦",leqslant:"⩽",les:"⩽",lescc:"⪨",lesdot:"⩿",lesdoto:"⪁",lesdotor:"⪃",lesg:"⋚︀",lesges:"⪓",lessapprox:"⪅",lessdot:"⋖",lesseqgtr:"⋚",lesseqqgtr:"⪋",LessEqualGreater:"⋚",LessFullEqual:"≦",LessGreater:"≶",lessgtr:"≶",LessLess:"⪡",lesssim:"≲",LessSlantEqual:"⩽",LessTilde:"≲",lfisht:"⥼",lfloor:"⌊",Lfr:"𝔏",lfr:"𝔩",lg:"≶",lgE:"⪑",lHar:"⥢",lhard:"↽",lharu:"↼",lharul:"⥪",lhblk:"▄",LJcy:"Љ",ljcy:"љ",Ll:"⋘",ll:"≪",llarr:"⇇",llcorner:"⌞",Lleftarrow:"⇚",llhard:"⥫",lltri:"◺",Lmidot:"Ŀ",lmidot:"ŀ",lmoust:"⎰",lmoustache:"⎰",lnap:"⪉",lnapprox:"⪉",lnE:"≨",lne:"⪇",lneq:"⪇",lneqq:"≨",lnsim:"⋦",loang:"⟬",loarr:"⇽",lobrk:"⟦",LongLeftArrow:"⟵",Longleftarrow:"⟸",longleftarrow:"⟵",LongLeftRightArrow:"⟷",Longleftrightarrow:"⟺",longleftrightarrow:"⟷",longmapsto:"⟼",LongRightArrow:"⟶",Longrightarrow:"⟹",longrightarrow:"⟶",looparrowleft:"↫",looparrowright:"↬",lopar:"⦅",Lopf:"𝕃",lopf:"𝕝",loplus:"⨭",lotimes:"⨴",lowast:"∗",lowbar:"_",LowerLeftArrow:"↙",LowerRightArrow:"↘",loz:"◊",lozenge:"◊",lozf:"⧫",lpar:"(",lparlt:"⦓",lrarr:"⇆",lrcorner:"⌟",lrhar:"⇋",lrhard:"⥭",lrm:"‎",lrtri:"⊿",lsaquo:"‹",Lscr:"ℒ",lscr:"𝓁",Lsh:"↰",lsh:"↰",lsim:"≲",lsime:"⪍",lsimg:"⪏",lsqb:"[",lsquo:"‘",lsquor:"‚",Lstrok:"Ł",lstrok:"ł",LT:"<",Lt:"≪",lt:"<",ltcc:"⪦",ltcir:"⩹",ltdot:"⋖",lthree:"⋋",ltimes:"⋉",ltlarr:"⥶",ltquest:"⩻",ltri:"◃",ltrie:"⊴",ltrif:"◂",ltrPar:"⦖",lurdshar:"⥊",luruhar:"⥦",lvertneqq:"≨︀",lvnE:"≨︀",macr:"¯",male:"♂",malt:"✠",maltese:"✠",Map:"⤅",map:"↦",mapsto:"↦",mapstodown:"↧",mapstoleft:"↤",mapstoup:"↥",marker:"▮",mcomma:"⨩",Mcy:"М",mcy:"м",mdash:"—",mDDot:"∺",measuredangle:"∡",MediumSpace:" ",Mellintrf:"ℳ",Mfr:"𝔐",mfr:"𝔪",mho:"℧",micro:"µ",mid:"∣",midast:"*",midcir:"⫰",middot:"·",minus:"−",minusb:"⊟",minusd:"∸",minusdu:"⨪",MinusPlus:"∓",mlcp:"⫛",mldr:"…",mnplus:"∓",models:"⊧",Mopf:"𝕄",mopf:"𝕞",mp:"∓",Mscr:"ℳ",mscr:"𝓂",mstpos:"∾",Mu:"Μ",mu:"μ",multimap:"⊸",mumap:"⊸",nabla:"∇",Nacute:"Ń",nacute:"ń",nang:"∠⃒",nap:"≉",napE:"⩰̸",napid:"≋̸",napos:"ʼn",napprox:"≉",natur:"♮",natural:"♮",naturals:"ℕ",nbsp:" ",nbump:"≎̸",nbumpe:"≏̸",ncap:"⩃",Ncaron:"Ň",ncaron:"ň",Ncedil:"Ņ",ncedil:"ņ",ncong:"≇",ncongdot:"⩭̸",ncup:"⩂",Ncy:"Н",ncy:"н",ndash:"–",ne:"≠",nearhk:"⤤",neArr:"⇗",nearr:"↗",nearrow:"↗",nedot:"≐̸",NegativeMediumSpace:"​",NegativeThickSpace:"​",NegativeThinSpace:"​",NegativeVeryThinSpace:"​",nequiv:"≢",nesear:"⤨",nesim:"≂̸",NestedGreaterGreater:"≫",NestedLessLess:"≪",NewLine:"\n",nexist:"∄",nexists:"∄",Nfr:"𝔑",nfr:"𝔫",ngE:"≧̸",nge:"≱",ngeq:"≱",ngeqq:"≧̸",ngeqslant:"⩾̸",nges:"⩾̸",nGg:"⋙̸",ngsim:"≵",nGt:"≫⃒",ngt:"≯",ngtr:"≯",nGtv:"≫̸",nhArr:"⇎",nharr:"↮",nhpar:"⫲",ni:"∋",nis:"⋼",nisd:"⋺",niv:"∋",NJcy:"Њ",njcy:"њ",nlArr:"⇍",nlarr:"↚",nldr:"‥",nlE:"≦̸",nle:"≰",nLeftarrow:"⇍",nleftarrow:"↚",nLeftrightarrow:"⇎",nleftrightarrow:"↮",nleq:"≰",nleqq:"≦̸",nleqslant:"⩽̸",nles:"⩽̸",nless:"≮",nLl:"⋘̸",nlsim:"≴",nLt:"≪⃒",nlt:"≮",nltri:"⋪",nltrie:"⋬",nLtv:"≪̸",nmid:"∤",NoBreak:"⁠",NonBreakingSpace:" ",Nopf:"ℕ",nopf:"𝕟",Not:"⫬",not:"¬",NotCongruent:"≢",NotCupCap:"≭",NotDoubleVerticalBar:"∦",NotElement:"∉",NotEqual:"≠",NotEqualTilde:"≂̸",NotExists:"∄",NotGreater:"≯",NotGreaterEqual:"≱",NotGreaterFullEqual:"≧̸",NotGreaterGreater:"≫̸",NotGreaterLess:"≹",NotGreaterSlantEqual:"⩾̸",NotGreaterTilde:"≵",NotHumpDownHump:"≎̸",NotHumpEqual:"≏̸",notin:"∉",notindot:"⋵̸",notinE:"⋹̸",notinva:"∉",notinvb:"⋷",notinvc:"⋶",NotLeftTriangle:"⋪",NotLeftTriangleBar:"⧏̸",NotLeftTriangleEqual:"⋬",NotLess:"≮",NotLessEqual:"≰",NotLessGreater:"≸",NotLessLess:"≪̸",NotLessSlantEqual:"⩽̸",NotLessTilde:"≴",NotNestedGreaterGreater:"⪢̸",NotNestedLessLess:"⪡̸",notni:"∌",notniva:"∌",notnivb:"⋾",notnivc:"⋽",NotPrecedes:"⊀",NotPrecedesEqual:"⪯̸",NotPrecedesSlantEqual:"⋠",NotReverseElement:"∌",NotRightTriangle:"⋫",NotRightTriangleBar:"⧐̸",NotRightTriangleEqual:"⋭",NotSquareSubset:"⊏̸",NotSquareSubsetEqual:"⋢",NotSquareSuperset:"⊐̸",NotSquareSupersetEqual:"⋣",NotSubset:"⊂⃒",NotSubsetEqual:"⊈",NotSucceeds:"⊁",NotSucceedsEqual:"⪰̸",NotSucceedsSlantEqual:"⋡",NotSucceedsTilde:"≿̸",NotSuperset:"⊃⃒",NotSupersetEqual:"⊉",NotTilde:"≁",NotTildeEqual:"≄",NotTildeFullEqual:"≇",NotTildeTilde:"≉",NotVerticalBar:"∤",npar:"∦",nparallel:"∦",nparsl:"⫽⃥",npart:"∂̸",npolint:"⨔",npr:"⊀",nprcue:"⋠",npre:"⪯̸",nprec:"⊀",npreceq:"⪯̸",nrArr:"⇏",nrarr:"↛",nrarrc:"⤳̸",nrarrw:"↝̸",nRightarrow:"⇏",nrightarrow:"↛",nrtri:"⋫",nrtrie:"⋭",nsc:"⊁",nsccue:"⋡",nsce:"⪰̸",Nscr:"𝒩",nscr:"𝓃",nshortmid:"∤",nshortparallel:"∦",nsim:"≁",nsime:"≄",nsimeq:"≄",nsmid:"∤",nspar:"∦",nsqsube:"⋢",nsqsupe:"⋣",nsub:"⊄",nsubE:"⫅̸",nsube:"⊈",nsubset:"⊂⃒",nsubseteq:"⊈",nsubseteqq:"⫅̸",nsucc:"⊁",nsucceq:"⪰̸",nsup:"⊅",nsupE:"⫆̸",nsupe:"⊉",nsupset:"⊃⃒",nsupseteq:"⊉",nsupseteqq:"⫆̸",ntgl:"≹",Ntilde:"Ñ",ntilde:"ñ",ntlg:"≸",ntriangleleft:"⋪",ntrianglelefteq:"⋬",ntriangleright:"⋫",ntrianglerighteq:"⋭",Nu:"Ν",nu:"ν",num:"#",numero:"№",numsp:" ",nvap:"≍⃒",nVDash:"⊯",nVdash:"⊮",nvDash:"⊭",nvdash:"⊬",nvge:"≥⃒",nvgt:">⃒",nvHarr:"⤄",nvinfin:"⧞",nvlArr:"⤂",nvle:"≤⃒",nvlt:"<⃒",nvltrie:"⊴⃒",nvrArr:"⤃",nvrtrie:"⊵⃒",nvsim:"∼⃒",nwarhk:"⤣",nwArr:"⇖",nwarr:"↖",nwarrow:"↖",nwnear:"⤧",Oacute:"Ó",oacute:"ó",oast:"⊛",ocir:"⊚",Ocirc:"Ô",ocirc:"ô",Ocy:"О",ocy:"о",odash:"⊝",Odblac:"Ő",odblac:"ő",odiv:"⨸",odot:"⊙",odsold:"⦼",OElig:"Œ",oelig:"œ",ofcir:"⦿",Ofr:"𝔒",ofr:"𝔬",ogon:"˛",Ograve:"Ò",ograve:"ò",ogt:"⧁",ohbar:"⦵",ohm:"Ω",oint:"∮",olarr:"↺",olcir:"⦾",olcross:"⦻",oline:"‾",olt:"⧀",Omacr:"Ō",omacr:"ō",Omega:"Ω",omega:"ω",Omicron:"Ο",omicron:"ο",omid:"⦶",ominus:"⊖",Oopf:"𝕆",oopf:"𝕠",opar:"⦷",OpenCurlyDoubleQuote:"“",OpenCurlyQuote:"‘",operp:"⦹",oplus:"⊕",Or:"⩔",or:"∨",orarr:"↻",ord:"⩝",order:"ℴ",orderof:"ℴ",ordf:"ª",ordm:"º",origof:"⊶",oror:"⩖",orslope:"⩗",orv:"⩛",oS:"Ⓢ",Oscr:"𝒪",oscr:"ℴ",Oslash:"Ø",oslash:"ø",osol:"⊘",Otilde:"Õ",otilde:"õ",Otimes:"⨷",otimes:"⊗",otimesas:"⨶",Ouml:"Ö",ouml:"ö",ovbar:"⌽",OverBar:"‾",OverBrace:"⏞",OverBracket:"⎴",OverParenthesis:"⏜",par:"∥",para:"¶",parallel:"∥",parsim:"⫳",parsl:"⫽",part:"∂",PartialD:"∂",Pcy:"П",pcy:"п",percnt:"%",period:".",permil:"‰",perp:"⊥",pertenk:"‱",Pfr:"𝔓",pfr:"𝔭",Phi:"Φ",phi:"φ",phiv:"ϕ",phmmat:"ℳ",phone:"☎",Pi:"Π",pi:"π",pitchfork:"⋔",piv:"ϖ",planck:"ℏ",planckh:"ℎ",plankv:"ℏ",plus:"+",plusacir:"⨣",plusb:"⊞",pluscir:"⨢",plusdo:"∔",plusdu:"⨥",pluse:"⩲",PlusMinus:"±",plusmn:"±",plussim:"⨦",plustwo:"⨧",pm:"±",Poincareplane:"ℌ",pointint:"⨕",Popf:"ℙ",popf:"𝕡",pound:"£",Pr:"⪻",pr:"≺",prap:"⪷",prcue:"≼",prE:"⪳",pre:"⪯",prec:"≺",precapprox:"⪷",preccurlyeq:"≼",Precedes:"≺",PrecedesEqual:"⪯",PrecedesSlantEqual:"≼",PrecedesTilde:"≾",preceq:"⪯",precnapprox:"⪹",precneqq:"⪵",precnsim:"⋨",precsim:"≾",Prime:"″",prime:"′",primes:"ℙ",prnap:"⪹",prnE:"⪵",prnsim:"⋨",prod:"∏",Product:"∏",profalar:"⌮",profline:"⌒",profsurf:"⌓",prop:"∝",Proportion:"∷",Proportional:"∝",propto:"∝",prsim:"≾",prurel:"⊰",Pscr:"𝒫",pscr:"𝓅",Psi:"Ψ",psi:"ψ",puncsp:" ",Qfr:"𝔔",qfr:"𝔮",qint:"⨌",Qopf:"ℚ",qopf:"𝕢",qprime:"⁗",Qscr:"𝒬",qscr:"𝓆",quaternions:"ℍ",quatint:"⨖",quest:"?",questeq:"≟",QUOT:'"',quot:'"',rAarr:"⇛",race:"∽̱",Racute:"Ŕ",racute:"ŕ",radic:"√",raemptyv:"⦳",Rang:"⟫",rang:"⟩",rangd:"⦒",range:"⦥",rangle:"⟩",raquo:"»",Rarr:"↠",rArr:"⇒",rarr:"→",rarrap:"⥵",rarrb:"⇥",rarrbfs:"⤠",rarrc:"⤳",rarrfs:"⤞",rarrhk:"↪",rarrlp:"↬",rarrpl:"⥅",rarrsim:"⥴",Rarrtl:"⤖",rarrtl:"↣",rarrw:"↝",rAtail:"⤜",ratail:"⤚",ratio:"∶",rationals:"ℚ",RBarr:"⤐",rBarr:"⤏",rbarr:"⤍",rbbrk:"❳",rbrace:"}",rbrack:"]",rbrke:"⦌",rbrksld:"⦎",rbrkslu:"⦐",Rcaron:"Ř",rcaron:"ř",Rcedil:"Ŗ",rcedil:"ŗ",rceil:"⌉",rcub:"}",Rcy:"Р",rcy:"р",rdca:"⤷",rdldhar:"⥩",rdquo:"”",rdquor:"”",rdsh:"↳",Re:"ℜ",real:"ℜ",realine:"ℛ",realpart:"ℜ",reals:"ℝ",rect:"▭",REG:"®",reg:"®",ReverseElement:"∋",ReverseEquilibrium:"⇋",ReverseUpEquilibrium:"⥯",rfisht:"⥽",rfloor:"⌋",Rfr:"ℜ",rfr:"𝔯",rHar:"⥤",rhard:"⇁",rharu:"⇀",rharul:"⥬",Rho:"Ρ",rho:"ρ",rhov:"ϱ",RightAngleBracket:"⟩",RightArrow:"→",Rightarrow:"⇒",rightarrow:"→",RightArrowBar:"⇥",RightArrowLeftArrow:"⇄",rightarrowtail:"↣",RightCeiling:"⌉",RightDoubleBracket:"⟧",RightDownTeeVector:"⥝",RightDownVector:"⇂",RightDownVectorBar:"⥕",RightFloor:"⌋",rightharpoondown:"⇁",rightharpoonup:"⇀",rightleftarrows:"⇄",rightleftharpoons:"⇌",rightrightarrows:"⇉",rightsquigarrow:"↝",RightTee:"⊢",RightTeeArrow:"↦",RightTeeVector:"⥛",rightthreetimes:"⋌",RightTriangle:"⊳",RightTriangleBar:"⧐",RightTriangleEqual:"⊵",RightUpDownVector:"⥏",RightUpTeeVector:"⥜",RightUpVector:"↾",RightUpVectorBar:"⥔",RightVector:"⇀",RightVectorBar:"⥓",ring:"˚",risingdotseq:"≓",rlarr:"⇄",rlhar:"⇌",rlm:"‏",rmoust:"⎱",rmoustache:"⎱",rnmid:"⫮",roang:"⟭",roarr:"⇾",robrk:"⟧",ropar:"⦆",Ropf:"ℝ",ropf:"𝕣",roplus:"⨮",rotimes:"⨵",RoundImplies:"⥰",rpar:")",rpargt:"⦔",rppolint:"⨒",rrarr:"⇉",Rrightarrow:"⇛",rsaquo:"›",Rscr:"ℛ",rscr:"𝓇",Rsh:"↱",rsh:"↱",rsqb:"]",rsquo:"’",rsquor:"’",rthree:"⋌",rtimes:"⋊",rtri:"▹",rtrie:"⊵",rtrif:"▸",rtriltri:"⧎",RuleDelayed:"⧴",ruluhar:"⥨",rx:"℞",Sacute:"Ś",sacute:"ś",sbquo:"‚",Sc:"⪼",sc:"≻",scap:"⪸",Scaron:"Š",scaron:"š",sccue:"≽",scE:"⪴",sce:"⪰",Scedil:"Ş",scedil:"ş",Scirc:"Ŝ",scirc:"ŝ",scnap:"⪺",scnE:"⪶",scnsim:"⋩",scpolint:"⨓",scsim:"≿",Scy:"С",scy:"с",sdot:"⋅",sdotb:"⊡",sdote:"⩦",searhk:"⤥",seArr:"⇘",searr:"↘",searrow:"↘",sect:"§",semi:";",seswar:"⤩",setminus:"∖",setmn:"∖",sext:"✶",Sfr:"𝔖",sfr:"𝔰",sfrown:"⌢",sharp:"♯",SHCHcy:"Щ",shchcy:"щ",SHcy:"Ш",shcy:"ш",ShortDownArrow:"↓",ShortLeftArrow:"←",shortmid:"∣",shortparallel:"∥",ShortRightArrow:"→",ShortUpArrow:"↑",shy:"­",Sigma:"Σ",sigma:"σ",sigmaf:"ς",sigmav:"ς",sim:"∼",simdot:"⩪",sime:"≃",simeq:"≃",simg:"⪞",simgE:"⪠",siml:"⪝",simlE:"⪟",simne:"≆",simplus:"⨤",simrarr:"⥲",slarr:"←",SmallCircle:"∘",smallsetminus:"∖",smashp:"⨳",smeparsl:"⧤",smid:"∣",smile:"⌣",smt:"⪪",smte:"⪬",smtes:"⪬︀",SOFTcy:"Ь",softcy:"ь",sol:"/",solb:"⧄",solbar:"⌿",Sopf:"𝕊",sopf:"𝕤",spades:"♠",spadesuit:"♠",spar:"∥",sqcap:"⊓",sqcaps:"⊓︀",sqcup:"⊔",sqcups:"⊔︀",Sqrt:"√",sqsub:"⊏",sqsube:"⊑",sqsubset:"⊏",sqsubseteq:"⊑",sqsup:"⊐",sqsupe:"⊒",sqsupset:"⊐",sqsupseteq:"⊒",squ:"□",Square:"□",square:"□",SquareIntersection:"⊓",SquareSubset:"⊏",SquareSubsetEqual:"⊑",SquareSuperset:"⊐",SquareSupersetEqual:"⊒",SquareUnion:"⊔",squarf:"▪",squf:"▪",srarr:"→",Sscr:"𝒮",sscr:"𝓈",ssetmn:"∖",ssmile:"⌣",sstarf:"⋆",Star:"⋆",star:"☆",starf:"★",straightepsilon:"ϵ",straightphi:"ϕ",strns:"¯",Sub:"⋐",sub:"⊂",subdot:"⪽",subE:"⫅",sube:"⊆",subedot:"⫃",submult:"⫁",subnE:"⫋",subne:"⊊",subplus:"⪿",subrarr:"⥹",Subset:"⋐",subset:"⊂",subseteq:"⊆",subseteqq:"⫅",SubsetEqual:"⊆",subsetneq:"⊊",subsetneqq:"⫋",subsim:"⫇",subsub:"⫕",subsup:"⫓",succ:"≻",succapprox:"⪸",succcurlyeq:"≽",Succeeds:"≻",SucceedsEqual:"⪰",SucceedsSlantEqual:"≽",SucceedsTilde:"≿",succeq:"⪰",succnapprox:"⪺",succneqq:"⪶",succnsim:"⋩",succsim:"≿",SuchThat:"∋",Sum:"∑",sum:"∑",sung:"♪",Sup:"⋑",sup:"⊃",sup1:"¹",sup2:"²",sup3:"³",supdot:"⪾",supdsub:"⫘",supE:"⫆",supe:"⊇",supedot:"⫄",Superset:"⊃",SupersetEqual:"⊇",suphsol:"⟉",suphsub:"⫗",suplarr:"⥻",supmult:"⫂",supnE:"⫌",supne:"⊋",supplus:"⫀",Supset:"⋑",supset:"⊃",supseteq:"⊇",supseteqq:"⫆",supsetneq:"⊋",supsetneqq:"⫌",supsim:"⫈",supsub:"⫔",supsup:"⫖",swarhk:"⤦",swArr:"⇙",swarr:"↙",swarrow:"↙",swnwar:"⤪",szlig:"ß",Tab:"\t",target:"⌖",Tau:"Τ",tau:"τ",tbrk:"⎴",Tcaron:"Ť",tcaron:"ť",Tcedil:"Ţ",tcedil:"ţ",Tcy:"Т",tcy:"т",tdot:"⃛",telrec:"⌕",Tfr:"𝔗",tfr:"𝔱",there4:"∴",Therefore:"∴",therefore:"∴",Theta:"Θ",theta:"θ",thetasym:"ϑ",thetav:"ϑ",thickapprox:"≈",thicksim:"∼",ThickSpace:"  ",thinsp:" ",ThinSpace:" ",thkap:"≈",thksim:"∼",THORN:"Þ",thorn:"þ",Tilde:"∼",tilde:"˜",TildeEqual:"≃",TildeFullEqual:"≅",TildeTilde:"≈",times:"×",timesb:"⊠",timesbar:"⨱",timesd:"⨰",tint:"∭",toea:"⤨",top:"⊤",topbot:"⌶",topcir:"⫱",Topf:"𝕋",topf:"𝕥",topfork:"⫚",tosa:"⤩",tprime:"‴",TRADE:"™",trade:"™",triangle:"▵",triangledown:"▿",triangleleft:"◃",trianglelefteq:"⊴",triangleq:"≜",triangleright:"▹",trianglerighteq:"⊵",tridot:"◬",trie:"≜",triminus:"⨺",TripleDot:"⃛",triplus:"⨹",trisb:"⧍",tritime:"⨻",trpezium:"⏢",Tscr:"𝒯",tscr:"𝓉",TScy:"Ц",tscy:"ц",TSHcy:"Ћ",tshcy:"ћ",Tstrok:"Ŧ",tstrok:"ŧ",twixt:"≬",twoheadleftarrow:"↞",twoheadrightarrow:"↠",Uacute:"Ú",uacute:"ú",Uarr:"↟",uArr:"⇑",uarr:"↑",Uarrocir:"⥉",Ubrcy:"Ў",ubrcy:"ў",Ubreve:"Ŭ",ubreve:"ŭ",Ucirc:"Û",ucirc:"û",Ucy:"У",ucy:"у",udarr:"⇅",Udblac:"Ű",udblac:"ű",udhar:"⥮",ufisht:"⥾",Ufr:"𝔘",ufr:"𝔲",Ugrave:"Ù",ugrave:"ù",uHar:"⥣",uharl:"↿",uharr:"↾",uhblk:"▀",ulcorn:"⌜",ulcorner:"⌜",ulcrop:"⌏",ultri:"◸",Umacr:"Ū",umacr:"ū",uml:"¨",UnderBar:"_",UnderBrace:"⏟",UnderBracket:"⎵",UnderParenthesis:"⏝",Union:"⋃",UnionPlus:"⊎",Uogon:"Ų",uogon:"ų",Uopf:"𝕌",uopf:"𝕦",UpArrow:"↑",Uparrow:"⇑",uparrow:"↑",UpArrowBar:"⤒",UpArrowDownArrow:"⇅",UpDownArrow:"↕",Updownarrow:"⇕",updownarrow:"↕",UpEquilibrium:"⥮",upharpoonleft:"↿",upharpoonright:"↾",uplus:"⊎",UpperLeftArrow:"↖",UpperRightArrow:"↗",Upsi:"ϒ",upsi:"υ",upsih:"ϒ",Upsilon:"Υ",upsilon:"υ",UpTee:"⊥",UpTeeArrow:"↥",upuparrows:"⇈",urcorn:"⌝",urcorner:"⌝",urcrop:"⌎",Uring:"Ů",uring:"ů",urtri:"◹",Uscr:"𝒰",uscr:"𝓊",utdot:"⋰",Utilde:"Ũ",utilde:"ũ",utri:"▵",utrif:"▴",uuarr:"⇈",Uuml:"Ü",uuml:"ü",uwangle:"⦧",vangrt:"⦜",varepsilon:"ϵ",varkappa:"ϰ",varnothing:"∅",varphi:"ϕ",varpi:"ϖ",varpropto:"∝",vArr:"⇕",varr:"↕",varrho:"ϱ",varsigma:"ς",varsubsetneq:"⊊︀",varsubsetneqq:"⫋︀",varsupsetneq:"⊋︀",varsupsetneqq:"⫌︀",vartheta:"ϑ",vartriangleleft:"⊲",vartriangleright:"⊳",Vbar:"⫫",vBar:"⫨",vBarv:"⫩",Vcy:"В",vcy:"в",VDash:"⊫",Vdash:"⊩",vDash:"⊨",vdash:"⊢",Vdashl:"⫦",Vee:"⋁",vee:"∨",veebar:"⊻",veeeq:"≚",vellip:"⋮",Verbar:"‖",verbar:"|",Vert:"‖",vert:"|",VerticalBar:"∣",VerticalLine:"|",VerticalSeparator:"❘",VerticalTilde:"≀",VeryThinSpace:" ",Vfr:"𝔙",vfr:"𝔳",vltri:"⊲",vnsub:"⊂⃒",vnsup:"⊃⃒",Vopf:"𝕍",vopf:"𝕧",vprop:"∝",vrtri:"⊳",Vscr:"𝒱",vscr:"𝓋",vsubnE:"⫋︀",vsubne:"⊊︀",vsupnE:"⫌︀",vsupne:"⊋︀",Vvdash:"⊪",vzigzag:"⦚",Wcirc:"Ŵ",wcirc:"ŵ",wedbar:"⩟",Wedge:"⋀",wedge:"∧",wedgeq:"≙",weierp:"℘",Wfr:"𝔚",wfr:"𝔴",Wopf:"𝕎",wopf:"𝕨",wp:"℘",wr:"≀",wreath:"≀",Wscr:"𝒲",wscr:"𝓌",xcap:"⋂",xcirc:"◯",xcup:"⋃",xdtri:"▽",Xfr:"𝔛",xfr:"𝔵",xhArr:"⟺",xharr:"⟷",Xi:"Ξ",xi:"ξ",xlArr:"⟸",xlarr:"⟵",xmap:"⟼",xnis:"⋻",xodot:"⨀",Xopf:"𝕏",xopf:"𝕩",xoplus:"⨁",xotime:"⨂",xrArr:"⟹",xrarr:"⟶",Xscr:"𝒳",xscr:"𝓍",xsqcup:"⨆",xuplus:"⨄",xutri:"△",xvee:"⋁",xwedge:"⋀",Yacute:"Ý",yacute:"ý",YAcy:"Я",yacy:"я",Ycirc:"Ŷ",ycirc:"ŷ",Ycy:"Ы",ycy:"ы",yen:"¥",Yfr:"𝔜",yfr:"𝔶",YIcy:"Ї",yicy:"ї",Yopf:"𝕐",yopf:"𝕪",Yscr:"𝒴",yscr:"𝓎",YUcy:"Ю",yucy:"ю",Yuml:"Ÿ",yuml:"ÿ",Zacute:"Ź",zacute:"ź",Zcaron:"Ž",zcaron:"ž",Zcy:"З",zcy:"з",Zdot:"Ż",zdot:"ż",zeetrf:"ℨ",ZeroWidthSpace:"​",Zeta:"Ζ",zeta:"ζ",Zfr:"ℨ",zfr:"𝔷",ZHcy:"Ж",zhcy:"ж",zigrarr:"⇝",Zopf:"ℤ",zopf:"𝕫",Zscr:"𝒵",zscr:"𝓏",zwj:"‍",zwnj:"‌"},r=Object.prototype.hasOwnProperty;function n(e){return o=e,(n=t)&&r.call(n,o)?t[e]:e;var n,o}var o=Object.prototype.hasOwnProperty;function s(e,t){return!!e&&o.call(e,t)}function i(e){return[].slice.call(arguments,1).forEach(function(t){if(t){if("object"!=typeof t)throw new TypeError(t+"must be object");Object.keys(t).forEach(function(r){e[r]=t[r]})}}),e}var a=/\\([\\!"#$%&'()*+,.\/:;<=>?@[\]^_`{|}~-])/g;function u(e){return e.indexOf("\\")<0?e:e.replace(a,"$1")}function l(e){return!(e>=55296&&e<=57343)&&(!(e>=64976&&e<=65007)&&(65535!=(65535&e)&&65534!=(65535&e)&&(!(e>=0&&e<=8)&&(11!==e&&(!(e>=14&&e<=31)&&(!(e>=127&&e<=159)&&!(e>1114111)))))))}function c(e){if(e>65535){var t=55296+((e-=65536)>>10),r=56320+(1023&e);return String.fromCharCode(t,r)}return String.fromCharCode(e)}var p=/&([a-z#][a-z0-9]{1,31});/gi,h=/^#((?:x[a-f0-9]{1,8}|[0-9]{1,8}))/i;function f(e,t){var r=0,o=n(t);return t!==o?o:35===t.charCodeAt(0)&&h.test(t)&&l(r="x"===t[1].toLowerCase()?parseInt(t.slice(2),16):parseInt(t.slice(1),10))?c(r):e}function g(e){return e.indexOf("&")<0?e:e.replace(p,f)}var d=/[&<>"]/,m=/[&<>"]/g,b={"&":"&","<":"<",">":">",'"':"""};function v(e){return b[e]}function k(e){return d.test(e)?e.replace(m,v):e}var A=Object.freeze({isString:function(e){return"[object String]"===function(e){return Object.prototype.toString.call(e)}(e)},has:s,assign:i,unescapeMd:u,isValidEntityCode:l,fromCodePoint:c,replaceEntities:g,escapeHtml:k}),y={};y.blockquote_open=function(){return"
\n"},y.blockquote_close=function(e,t){return"
"+x(e,t)},y.code=function(e,t){return e[t].block?"
"+k(e[t].content)+"
"+x(e,t):""+k(e[t].content)+""},y.fence=function(e,t,r,n,o){var i,a,l=e[t],c="",p=r.langPrefix;if(l.params){if(a=(i=l.params.split(/\s+/g)).join(" "),s(o.rules.fence_custom,i[0]))return o.rules.fence_custom[i[0]](e,t,r,n,o);c=' class="'+p+k(g(u(a)))+'"'}return"
"+(r.highlight&&r.highlight.apply(r.highlight,[l.content].concat(i))||k(l.content))+"
"+x(e,t)},y.fence_custom={},y.heading_open=function(e,t){return""},y.heading_close=function(e,t){return"\n"},y.hr=function(e,t,r){return(r.xhtmlOut?"
":"
")+x(e,t)},y.bullet_list_open=function(){return"
    \n"},y.bullet_list_close=function(e,t){return"
"+x(e,t)},y.list_item_open=function(){return"
  • "},y.list_item_close=function(){return"
  • \n"},y.ordered_list_open=function(e,t){var r=e[t];return"1?' start="'+r.order+'"':"")+">\n"},y.ordered_list_close=function(e,t){return""+x(e,t)},y.paragraph_open=function(e,t){return e[t].tight?"":"

    "},y.paragraph_close=function(e,t){var r=!(e[t].tight&&t&&"inline"===e[t-1].type&&!e[t-1].content);return(e[t].tight?"":"

    ")+(r?x(e,t):"")},y.link_open=function(e,t,r){var n=e[t].title?' title="'+k(g(e[t].title))+'"':"",o=r.linkTarget?' target="'+r.linkTarget+'"':"";return'"},y.link_close=function(){return""},y.image=function(e,t,r){var n=' src="'+k(e[t].src)+'"',o=e[t].title?' title="'+k(g(e[t].title))+'"':"";return""},y.table_open=function(){return"\n"},y.table_close=function(){return"
    \n"},y.thead_open=function(){return"\n"},y.thead_close=function(){return"\n"},y.tbody_open=function(){return"\n"},y.tbody_close=function(){return"\n"},y.tr_open=function(){return""},y.tr_close=function(){return"\n"},y.th_open=function(e,t){var r=e[t];return""},y.th_close=function(){return""},y.td_open=function(e,t){var r=e[t];return""},y.td_close=function(){return""},y.strong_open=function(){return""},y.strong_close=function(){return""},y.em_open=function(){return""},y.em_close=function(){return""},y.del_open=function(){return""},y.del_close=function(){return""},y.ins_open=function(){return""},y.ins_close=function(){return""},y.mark_open=function(){return""},y.mark_close=function(){return""},y.sub=function(e,t){return""+k(e[t].content)+""},y.sup=function(e,t){return""+k(e[t].content)+""},y.hardbreak=function(e,t,r){return r.xhtmlOut?"
    \n":"
    \n"},y.softbreak=function(e,t,r){return r.breaks?r.xhtmlOut?"
    \n":"
    \n":"\n"},y.text=function(e,t){return k(e[t].content)},y.htmlblock=function(e,t){return e[t].content},y.htmltag=function(e,t){return e[t].content},y.abbr_open=function(e,t){return''},y.abbr_close=function(){return""},y.footnote_ref=function(e,t){var r=Number(e[t].id+1).toString(),n="fnref"+r;return e[t].subId>0&&(n+=":"+e[t].subId),'['+r+"]"},y.footnote_block_open=function(e,t,r){return(r.xhtmlOut?'
    \n':'
    \n')+'
    \n
      \n'},y.footnote_block_close=function(){return"
    \n
    \n"},y.footnote_open=function(e,t){return'
  • '},y.footnote_close=function(){return"
  • \n"},y.footnote_anchor=function(e,t){var r="fnref"+Number(e[t].id+1).toString();return e[t].subId>0&&(r+=":"+e[t].subId),' '},y.dl_open=function(){return"
    \n"},y.dt_open=function(){return"
    "},y.dd_open=function(){return"
    "},y.dl_close=function(){return"
    \n"},y.dt_close=function(){return"\n"},y.dd_close=function(){return"\n"};var x=y.getBreak=function(e,t){return(t=function e(t,r){return++r>=t.length-2?r:"paragraph_open"===t[r].type&&t[r].tight&&"inline"===t[r+1].type&&0===t[r+1].content.length&&"paragraph_close"===t[r+2].type&&t[r+2].tight?e(t,r+2):r}(e,t))1)break;if(41===r&&--n<0)break;t++}return s!==t&&(o=u(e.src.slice(s,t)),!!e.parser.validateLink(o)&&(e.linkContent=o,e.pos=t,!0))}function F(e,t){var r,n=t,o=e.posMax,s=e.src.charCodeAt(t);if(34!==s&&39!==s&&40!==s)return!1;for(t++,40===s&&(s=41);t=e.length)&&!I.test(e[t])}function H(e,t,r){return e.substr(0,t)+r+e.substr(t+1)}var V=[["block",function(e){e.inlineMode?e.tokens.push({type:"inline",content:e.src.replace(/\n/g," ").trim(),level:0,lines:[0,1],children:[]}):e.block.parse(e.src,e.options,e.env,e.tokens)}],["abbr",function(e){var t,r,n,o,s=e.tokens;if(!e.inlineMode)for(t=1,r=s.length-1;t0?i[t].count:1,n=0;n=0;t--)if("text"===(s=o[t]).type){for(u=0,i=s.content,c.lastIndex=0,l=s.level,a=[];p=c.exec(i);)c.lastIndex>u&&a.push({type:"text",content:i.slice(u,p.index+p[1].length),level:l}),a.push({type:"abbr_open",title:e.env.abbreviations[":"+p[2]],level:l++}),a.push({type:"text",content:p[2],level:l}),a.push({type:"abbr_close",level:--l}),u=c.lastIndex-p[3].length;a.length&&(u=0;s--)if("inline"===e.tokens[s].type)for(t=(o=e.tokens[s].children).length-1;t>=0;t--)"text"===(r=o[t]).type&&(n=r.content,n=(i=n).indexOf("(")<0?i:i.replace(N,function(e,t){return z[t.toLowerCase()]}),R.test(n)&&(n=n.replace(/\+-/g,"±").replace(/\.{2,}/g,"…").replace(/([?!])…/g,"$1..").replace(/([?!]){4,}/g,"$1$1$1").replace(/,{2,}/g,",").replace(/(^|[^-])---([^-]|$)/gm,"$1—$2").replace(/(^|\s)--(\s|$)/gm,"$1–$2").replace(/(^|[^-\s])--([^-\s]|$)/gm,"$1–$2")),r.content=n)}],["smartquotes",function(e){var t,r,n,o,s,i,a,u,l,c,p,h,f,g,d,m,b;if(e.options.typographer)for(b=[],d=e.tokens.length-1;d>=0;d--)if("inline"===e.tokens[d].type)for(m=e.tokens[d].children,b.length=0,t=0;t=0&&!(b[f].level<=a);f--);b.length=f+1,s=0,i=(n=r.content).length;e:for(;s=0&&(c=b[f],!(b[f].level=(o=e.eMarks[t])?-1:42!==(r=e.src.charCodeAt(n++))&&45!==r&&43!==r?-1:n=o)return-1;if((r=e.src.charCodeAt(n++))<48||r>57)return-1;for(;;){if(n>=o)return-1;if(!((r=e.src.charCodeAt(n++))>=48&&r<=57)){if(41===r||46===r)break;return-1}}return n=this.eMarks[e]},$.prototype.skipEmptyLines=function(e){for(var t=this.lineMax;er;)if(t!==this.src.charCodeAt(--e))return e+1;return e},$.prototype.getLines=function(e,t,r,n){var o,s,i,a,u,l=e;if(e>=t)return"";if(l+1===t)return s=this.bMarks[l]+Math.min(this.tShift[l],r),i=n?this.eMarks[l]+1:this.eMarks[l],this.src.slice(s,i);for(a=new Array(t-e),o=0;lr&&(u=r),u<0&&(u=0),s=this.bMarks[l]+u,i=l+1]/,K=/^<\/([a-zA-Z]{1,15})[\s>]/;function Q(e,t){var r=e.bMarks[t]+e.blkIndent,n=e.eMarks[t];return e.src.substr(r,n-r)}function X(e,t){var r,n,o=e.bMarks[t]+e.tShift[t],s=e.eMarks[t];return o>=s?-1:126!==(n=e.src.charCodeAt(o++))&&58!==n?-1:o===(r=e.skipSpaces(o))?-1:r>=s?-1:r}var ee=[["code",function(e,t,r){var n,o;if(e.tShift[t]-e.blkIndent<4)return!1;for(o=n=t+1;n=4))break;o=++n}return e.line=n,e.tokens.push({type:"code",content:e.getLines(t,o,4+e.blkIndent,!0),block:!0,lines:[t,e.line],level:e.level}),!0}],["fences",function(e,t,r,n){var o,s,i,a,u,l=!1,c=e.bMarks[t]+e.tShift[t],p=e.eMarks[t];if(c+3>p)return!1;if(126!==(o=e.src.charCodeAt(c))&&96!==o)return!1;if(u=c,(s=(c=e.skipChars(c,o))-u)<3)return!1;if((i=e.src.slice(c,p).trim()).indexOf("`")>=0)return!1;if(n)return!0;for(a=t;!(++a>=r||(c=u=e.bMarks[a]+e.tShift[a])<(p=e.eMarks[a])&&e.tShift[a]=4||(c=e.skipChars(c,o))-um)return!1;if(62!==e.src.charCodeAt(d++))return!1;if(e.level>=e.options.maxNesting)return!1;if(n)return!0;for(32===e.src.charCodeAt(d)&&d++,u=e.blkIndent,e.blkIndent=0,a=[e.bMarks[t]],e.bMarks[t]=d,s=(d=d=m,i=[e.tShift[t]],e.tShift[t]=d-e.bMarks[t],p=e.parser.ruler.getRules("blockquote"),o=t+1;o=(m=e.eMarks[o]));o++)if(62!==e.src.charCodeAt(d++)){if(s)break;for(g=!1,h=0,f=p.length;h=m,i.push(e.tShift[o]),e.tShift[o]=d-e.bMarks[o];for(l=e.parentType,e.parentType="blockquote",e.tokens.push({type:"blockquote_open",lines:c=[t,0],level:e.level++}),e.parser.tokenize(e,t,o),e.tokens.push({type:"blockquote_close",level:--e.level}),e.parentType=l,c[1]=e.line,h=0;hu)return!1;if(42!==(o=e.src.charCodeAt(a++))&&45!==o&&95!==o)return!1;for(s=1;a=0)d=!0;else{if(!((p=Z(e,t))>=0))return!1;d=!1}if(e.level>=e.options.maxNesting)return!1;if(g=e.src.charCodeAt(p-1),n)return!0;for(b=e.tokens.length,d?(c=e.bMarks[t]+e.tShift[t],f=Number(e.src.substr(c,p-c-1)),e.tokens.push({type:"ordered_list_open",order:f,lines:k=[t,0],level:e.level++})):e.tokens.push({type:"bullet_list_open",lines:k=[t,0],level:e.level++}),o=t,v=!1,y=e.parser.ruler.getRules("list");!(!(o=e.eMarks[o]?1:m-p)>4&&(h=1),h<1&&(h=1),s=p-e.bMarks[o]+h,e.tokens.push({type:"list_item_open",lines:A=[t,0],level:e.level++}),a=e.blkIndent,u=e.tight,i=e.tShift[t],l=e.parentType,e.tShift[t]=m-e.bMarks[t],e.blkIndent=s,e.tight=!0,e.parentType="list",e.parser.tokenize(e,t,r,!0),e.tight&&!v||(E=!1),v=e.line-t>1&&e.isEmpty(e.line-1),e.blkIndent=a,e.tShift[t]=i,e.tight=u,e.parentType=l,e.tokens.push({type:"list_item_close",level:--e.level}),o=t=e.line,A[1]=o,m=e.bMarks[t],o>=r)||e.isEmpty(o)||e.tShift[o]c)return!1;if(91!==e.src.charCodeAt(l))return!1;if(94!==e.src.charCodeAt(l+1))return!1;if(e.level>=e.options.maxNesting)return!1;for(a=l+2;a=c||58!==e.src.charCodeAt(++a)||!n&&(a++,e.env.footnotes||(e.env.footnotes={}),e.env.footnotes.refs||(e.env.footnotes.refs={}),u=e.src.slice(l+2,a-2),e.env.footnotes.refs[":"+u]=-1,e.tokens.push({type:"footnote_reference_open",label:u,level:e.level++}),o=e.bMarks[t],s=e.tShift[t],i=e.parentType,e.tShift[t]=e.skipSpaces(a)-a,e.bMarks[t]=a,e.blkIndent+=4,e.parentType="footnote",e.tShift[t]=u)return!1;if(35!==(o=e.src.charCodeAt(a))||a>=u)return!1;for(s=1,o=e.src.charCodeAt(++a);35===o&&a6||aa&&32===e.src.charCodeAt(i-1)&&(u=i),e.line=t+1,e.tokens.push({type:"heading_open",hLevel:s,lines:[t,e.line],level:e.level}),a=r||e.tShift[i]3||(o=e.bMarks[i]+e.tShift[i])>=(s=e.eMarks[i])||45!==(n=e.src.charCodeAt(o))&&61!==n||(o=e.skipChars(o,n),(o=e.skipSpaces(o))3||a+2>=u)return!1;if(60!==e.src.charCodeAt(a))return!1;if(33===(o=e.src.charCodeAt(a+1))||63===o){if(n)return!0}else{if(47!==o&&!function(e){var t=32|e;return t>=97&&t<=122}(o))return!1;if(47===o){if(!(s=e.src.slice(a,u).match(K)))return!1}else if(!(s=e.src.slice(a,u).match(Y)))return!1;if(!0!==J[s[1].toLowerCase()])return!1;if(n)return!0}for(i=t+1;ir)return!1;if(u=t+1,e.tShift[u]=e.eMarks[u])return!1;if(124!==(o=e.src.charCodeAt(i))&&45!==o&&58!==o)return!1;if(s=Q(e,t+1),!/^[-:| ]+$/.test(s))return!1;if((l=s.split("|"))<=2)return!1;for(p=[],a=0;a=0;if(c=t+1,e.isEmpty(c)&&++c>r)return!1;if(e.tShift[c]=e.options.maxNesting)return!1;l=e.tokens.length,e.tokens.push({type:"dl_open",lines:u=[t,0],level:e.level++}),i=t,s=c;e:for(;;){for(b=!0,m=!1,e.tokens.push({type:"dt_open",lines:[i,i],level:e.level++}),e.tokens.push({type:"inline",content:e.getLines(i,i+1,e.blkIndent,!1).trim(),level:e.level+1,lines:[i,i],children:[]}),e.tokens.push({type:"dt_close",level:--e.level});;){if(e.tokens.push({type:"dd_open",lines:a=[c,0],level:e.level++}),d=e.tight,h=e.ddIndent,p=e.blkIndent,g=e.tShift[s],f=e.parentType,e.blkIndent=e.ddIndent=e.tShift[s]+2,e.tShift[s]=o-e.bMarks[s],e.tight=!0,e.parentType="deflist",e.parser.tokenize(e,s,r,!0),e.tight&&!m||(b=!1),m=e.line-s>1&&e.isEmpty(e.line-1),e.tShift[s]=g,e.tight=d,e.parentType=f,e.blkIndent=p,e.ddIndent=h,e.tokens.push({type:"dd_close",level:--e.level}),a[1]=c=e.line,c>=r)break e;if(e.tShift[c]=r)break;if(i=c,e.isEmpty(i))break;if(e.tShift[i]=r)break;if(e.isEmpty(s)&&s++,s>=r)break;if(e.tShift[s]3)){for(o=!1,s=0,i=a.length;s=r))&&!(e.tShift[i]=0&&(e=e.replace(re,function(t,r){var n;return 10===e.charCodeAt(r)?(s=r+1,i=0,t):(n=" ".slice((r-s-i)%4),i=r-s+1,n)})),o=new $(e,this,t,r,n),this.tokenize(o,o.line,o.lineMax)};for(var ie=[],ae=0;ae<256;ae++)ie.push(0);function ue(e){return e>=48&&e<=57||e>=65&&e<=90||e>=97&&e<=122}function le(e,t){var r,n,o,s=t,i=!0,a=!0,u=e.posMax,l=e.src.charCodeAt(t);for(r=t>0?e.src.charCodeAt(t-1):-1;s=u&&(i=!1),(o=s-t)>=4?i=a=!1:(32!==(n=s?@[]^_`{|}~-".split("").forEach(function(e){ie[e.charCodeAt(0)]=1});var ce=/\\([ \\!"#$%&'()*+,.\/:;<=>?@[\]^_`{|}~-])/g;var pe=/\\([ \\!"#$%&'()*+,.\/:;<=>?@[\]^_`{|}~-])/g;var he=["coap","doi","javascript","aaa","aaas","about","acap","cap","cid","crid","data","dav","dict","dns","file","ftp","geo","go","gopher","h323","http","https","iax","icap","im","imap","info","ipp","iris","iris.beep","iris.xpc","iris.xpcs","iris.lwz","ldap","mailto","mid","msrp","msrps","mtqp","mupdate","news","nfs","ni","nih","nntp","opaquelocktoken","pop","pres","rtsp","service","session","shttp","sieve","sip","sips","sms","snmp","soap.beep","soap.beeps","tag","tel","telnet","tftp","thismessage","tn3270","tip","tv","urn","vemmi","ws","wss","xcon","xcon-userid","xmlrpc.beep","xmlrpc.beeps","xmpp","z39.50r","z39.50s","adiumxtra","afp","afs","aim","apt","attachment","aw","beshare","bitcoin","bolo","callto","chrome","chrome-extension","com-eventbrite-attendee","content","cvs","dlna-playsingle","dlna-playcontainer","dtn","dvb","ed2k","facetime","feed","finger","fish","gg","git","gizmoproject","gtalk","hcp","icon","ipn","irc","irc6","ircs","itms","jar","jms","keyparc","lastfm","ldaps","magnet","maps","market","message","mms","ms-help","msnim","mumble","mvn","notes","oid","palm","paparazzi","platform","proxy","psyc","query","res","resource","rmi","rsync","rtmp","secondlife","sftp","sgn","skype","smb","soldat","spotify","ssh","steam","svn","teamspeak","things","udp","unreal","ut2004","ventrilo","view-source","webcal","wtai","wyciwyg","xfire","xri","ymsgr"],fe=/^<([a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)>/,ge=/^<([a-zA-Z.\-]{1,25}):([^<>\x00-\x20]*)>/;function de(e,t){return e=e.source,t=t||"",function r(n,o){return n?(o=o.source||o,e=e.replace(n,o),r):new RegExp(e,t)}}var me=de(/(?:unquoted|single_quoted|double_quoted)/)("unquoted",/[^"'=<>`\x00-\x20]+/)("single_quoted",/'[^']*'/)("double_quoted",/"[^"]*"/)(),be=de(/(?:\s+attr_name(?:\s*=\s*attr_value)?)/)("attr_name",/[a-zA-Z_:][a-zA-Z0-9:._-]*/)("attr_value",me)(),ve=de(/<[A-Za-z][A-Za-z0-9]*attribute*\s*\/?>/)("attribute",be)(),ke=de(/^(?:open_tag|close_tag|comment|processing|declaration|cdata)/)("open_tag",ve)("close_tag",/<\/[A-Za-z][A-Za-z0-9]*\s*>/)("comment",/|/)("processing",/<[?].*?[?]>/)("declaration",/]*>/)("cdata",//)();var Ae=/^&#((?:x[a-f0-9]{1,8}|[0-9]{1,8}));/i,ye=/^&([a-z][a-z0-9]{1,31});/i;var xe=[["text",function(e,t){for(var r=e.pos;r=0&&32===e.pending.charCodeAt(r))if(r>=1&&32===e.pending.charCodeAt(r-1)){for(var s=r-2;s>=0;s--)if(32!==e.pending.charCodeAt(s)){e.pending=e.pending.substring(0,s+1);break}e.push({type:"hardbreak",level:e.level})}else e.pending=e.pending.slice(0,-1),e.push({type:"softbreak",level:e.level});else e.push({type:"softbreak",level:e.level});for(o++;o=a)return!1;if(126!==e.src.charCodeAt(u+1))return!1;if(e.level>=e.options.maxNesting)return!1;if(s=u>0?e.src.charCodeAt(u-1):-1,i=e.src.charCodeAt(u+2),126===s)return!1;if(126===i)return!1;if(32===i||10===i)return!1;for(n=u+2;nu+3)return e.pos+=n-u,t||(e.pending+=e.src.slice(u,n)),!0;for(e.pos=u+2,o=1;e.pos+1=a)return!1;if(43!==e.src.charCodeAt(u+1))return!1;if(e.level>=e.options.maxNesting)return!1;if(s=u>0?e.src.charCodeAt(u-1):-1,i=e.src.charCodeAt(u+2),43===s)return!1;if(43===i)return!1;if(32===i||10===i)return!1;for(n=u+2;n=a)return!1;if(61!==e.src.charCodeAt(u+1))return!1;if(e.level>=e.options.maxNesting)return!1;if(s=u>0?e.src.charCodeAt(u-1):-1,i=e.src.charCodeAt(u+2),61===s)return!1;if(61===i)return!1;if(32===i||10===i)return!1;for(n=u+2;n=e.options.maxNesting)return!1;for(e.pos=c+r,a=[r];e.pos=o)return!1;if(e.level>=e.options.maxNesting)return!1;for(e.pos=s+1;e.pos=o)return!1;if(e.level>=e.options.maxNesting)return!1;for(e.pos=s+1;e.pos=e.options.maxNesting)return!1;if(r=f+1,(n=D(e,f))<0)return!1;if((a=n+1)=h)return!1;for(f=a,q(e,a)?(s=e.linkContent,a=e.pos):s="",f=a;a=h||41!==e.src.charCodeAt(a))return e.pos=p,!1;a++}else{if(e.linkLevel>0)return!1;for(;a=0?o=e.src.slice(f,a++):a=f-1),o||(void 0===o&&(a=n+1),o=e.src.slice(r,n)),!(u=e.env.references[M(o)]))return e.pos=p,!1;s=u.href,i=u.title}return t||(e.pos=r,e.posMax=n,c?e.push({type:"image",src:s,title:i,alt:e.src.substr(r,n-r),level:e.level}):(e.push({type:"link_open",href:s,title:i,level:e.level++}),e.linkLevel++,e.parser.tokenize(e),e.linkLevel--,e.push({type:"link_close",level:--e.level}))),e.pos=a,e.posMax=h,!0}],["footnote_inline",function(e,t){var r,n,o,s,i=e.posMax,a=e.pos;return!(a+2>=i||94!==e.src.charCodeAt(a)||91!==e.src.charCodeAt(a+1)||e.level>=e.options.maxNesting||(r=a+2,(n=D(e,a+1))<0||(t||(e.env.footnotes||(e.env.footnotes={}),e.env.footnotes.list||(e.env.footnotes.list=[]),o=e.env.footnotes.list.length,e.pos=r,e.posMax=n,e.push({type:"footnote_ref",id:o,level:e.level}),e.linkLevel++,s=e.tokens.length,e.parser.tokenize(e),e.env.footnotes.list[o]={tokens:e.tokens.splice(s)},e.linkLevel--),e.pos=n+1,e.posMax=i,0)))}],["footnote_ref",function(e,t){var r,n,o,s,i=e.posMax,a=e.pos;if(a+3>i)return!1;if(!e.env.footnotes||!e.env.footnotes.refs)return!1;if(91!==e.src.charCodeAt(a))return!1;if(94!==e.src.charCodeAt(a+1))return!1;if(e.level>=e.options.maxNesting)return!1;for(n=a+2;n=i||(n++,r=e.src.slice(a+2,n-1),void 0===e.env.footnotes.refs[":"+r]||(t||(e.env.footnotes.list||(e.env.footnotes.list=[]),e.env.footnotes.refs[":"+r]<0?(o=e.env.footnotes.list.length,e.env.footnotes.list[o]={label:r,count:0},e.env.footnotes.refs[":"+r]=o):o=e.env.footnotes.refs[":"+r],s=e.env.footnotes.list[o].count,e.env.footnotes.list[o].count++,e.push({type:"footnote_ref",id:o,subId:s,level:e.level})),e.pos=n,e.posMax=i,0)))}],["autolink",function(e,t){var r,n,o,s,i,a=e.pos;return!(60!==e.src.charCodeAt(a)||(r=e.src.slice(a)).indexOf(">")<0||((n=r.match(ge))?he.indexOf(n[1].toLowerCase())<0||(i=B(s=n[0].slice(1,-1)),!e.parser.validateLink(s)||(t||(e.push({type:"link_open",href:i,level:e.level}),e.push({type:"text",content:s,level:e.level+1}),e.push({type:"link_close",level:e.level})),e.pos+=n[0].length,0)):!(o=r.match(fe))||(i=B("mailto:"+(s=o[0].slice(1,-1))),!e.parser.validateLink(i)||(t||(e.push({type:"link_open",href:i,level:e.level}),e.push({type:"text",content:s,level:e.level+1}),e.push({type:"link_close",level:e.level})),e.pos+=o[0].length,0))))}],["htmltag",function(e,t){var r,n,o,s=e.pos;return!(!e.options.html||(o=e.posMax,60!==e.src.charCodeAt(s)||s+2>=o||33!==(r=e.src.charCodeAt(s+1))&&63!==r&&47!==r&&!function(e){var t=32|e;return t>=97&&t<=122}(r)||!(n=e.src.slice(s).match(ke))||(t||e.push({type:"htmltag",content:e.src.slice(s,s+n[0].length),level:e.level}),e.pos+=n[0].length,0)))}],["entity",function(e,t){var r,o,s=e.pos,i=e.posMax;if(38!==e.src.charCodeAt(s))return!1;if(s+10)e.pos=r;else{for(t=0;t=s)break}else e.pending+=e.src[e.pos++]}e.pending&&e.pushPending()},we.prototype.parse=function(e,t,r,n){var o=new E(e,this,t,r,n);this.tokenize(o)};var Ee={default:{options:{html:!1,xhtmlOut:!1,breaks:!1,langPrefix:"language-",linkTarget:"",typographer:!1,quotes:"“”‘’",highlight:null,maxNesting:20},components:{core:{rules:["block","inline","references","replacements","smartquotes","references","abbr2","footnote_tail"]},block:{rules:["blockquote","code","fences","footnote","heading","hr","htmlblock","lheading","list","paragraph","table"]},inline:{rules:["autolink","backticks","del","emphasis","entity","escape","footnote_ref","htmltag","links","newline","text"]}}},full:{options:{html:!1,xhtmlOut:!1,breaks:!1,langPrefix:"language-",linkTarget:"",typographer:!1,quotes:"“”‘’",highlight:null,maxNesting:20},components:{core:{},block:{},inline:{}}},commonmark:{options:{html:!0,xhtmlOut:!0,breaks:!1,langPrefix:"language-",linkTarget:"",typographer:!1,quotes:"“”‘’",highlight:null,maxNesting:20},components:{core:{rules:["block","inline","references","abbr2"]},block:{rules:["blockquote","code","fences","heading","hr","htmlblock","lheading","list","paragraph"]},inline:{rules:["autolink","backticks","emphasis","entity","escape","htmltag","links","newline","text"]}}}};function De(e,t,r){this.src=t,this.env=r,this.options=e.options,this.tokens=[],this.inlineMode=!1,this.inline=e.inline,this.block=e.block,this.renderer=e.renderer,this.typographer=e.typographer}function _e(e,t){"string"!=typeof e&&(t=e,e="default"),t&&null!=t.linkify&&console.warn("linkify option is removed. Use linkify plugin instead:\n\nimport Remarkable from 'remarkable';\nimport linkify from 'remarkable/linkify';\nnew Remarkable().use(linkify)\n"),this.inline=new we,this.block=new te,this.core=new G,this.renderer=new w,this.ruler=new C,this.options={},this.configure(Ee[e]),this.set(t||{})}function Be(e,t){if(Array.prototype.indexOf)return e.indexOf(t);for(var r=0,n=e.length;r=0;r--)!0===t(e[r])&&e.splice(r,1)}function Fe(e){throw new Error("Unhandled case for value: '"+e+"'")}_e.prototype.set=function(e){i(this.options,e)},_e.prototype.configure=function(e){var t=this;if(!e)throw new Error("Wrong `remarkable` preset, check name/content");e.options&&t.set(e.options),e.components&&Object.keys(e.components).forEach(function(r){e.components[r].rules&&t[r].ruler.enable(e.components[r].rules,!0)})},_e.prototype.use=function(e,t){return e(this,t),this},_e.prototype.parse=function(e,t){var r=new De(this,e,t);return this.core.process(r),r.tokens},_e.prototype.render=function(e,t){return t=t||{},this.renderer.render(this.parse(e,t),this.options,t)},_e.prototype.parseInline=function(e,t){var r=new De(this,e,t);return r.inlineMode=!0,this.core.process(r),r.tokens},_e.prototype.renderInline=function(e,t){return t=t||{},this.renderer.render(this.parseInline(e,t),this.options,t)};var Me=function(){function e(e){void 0===e&&(e={}),this.tagName="",this.attrs={},this.innerHTML="",this.whitespaceRegex=/\s+/,this.tagName=e.tagName||"",this.attrs=e.attrs||{},this.innerHTML=e.innerHtml||e.innerHTML||""}return e.prototype.setTagName=function(e){return this.tagName=e,this},e.prototype.getTagName=function(){return this.tagName||""},e.prototype.setAttr=function(e,t){return this.getAttrs()[e]=t,this},e.prototype.getAttr=function(e){return this.getAttrs()[e]},e.prototype.setAttrs=function(e){return Object.assign(this.getAttrs(),e),this},e.prototype.getAttrs=function(){return this.attrs||(this.attrs={})},e.prototype.setClass=function(e){return this.setAttr("class",e)},e.prototype.addClass=function(e){for(var t,r=this.getClass(),n=this.whitespaceRegex,o=r?r.split(n):[],s=e.split(n);t=s.shift();)-1===Be(o,t)&&o.push(t);return this.getAttrs().class=o.join(" "),this},e.prototype.removeClass=function(e){for(var t,r=this.getClass(),n=this.whitespaceRegex,o=r?r.split(n):[],s=e.split(n);o.length&&(t=s.shift());){var i=Be(o,t);-1!==i&&o.splice(i,1)}return this.getAttrs().class=o.join(" "),this},e.prototype.getClass=function(){return this.getAttrs().class||""},e.prototype.hasClass=function(e){return-1!==(" "+this.getClass()+" ").indexOf(" "+e+" ")},e.prototype.setInnerHTML=function(e){return this.innerHTML=e,this},e.prototype.setInnerHtml=function(e){return this.setInnerHTML(e)},e.prototype.getInnerHTML=function(){return this.innerHTML||""},e.prototype.getInnerHtml=function(){return this.getInnerHTML()},e.prototype.toAnchorString=function(){var e=this.getTagName(),t=this.buildAttrsStr();return["<",e,t=t?" "+t:"",">",this.getInnerHtml(),""].join("")},e.prototype.buildAttrsStr=function(){if(!this.attrs)return"";var e=this.getAttrs(),t=[];for(var r in e)e.hasOwnProperty(r)&&t.push(r+'="'+e[r]+'"');return t.join(" ")},e}();var Se=function(){function e(e){void 0===e&&(e={}),this.newWindow=!1,this.truncate={},this.className="",this.newWindow=e.newWindow||!1,this.truncate=e.truncate||{},this.className=e.className||""}return e.prototype.build=function(e){return new Me({tagName:"a",attrs:this.createAttrs(e),innerHtml:this.processAnchorText(e.getAnchorText())})},e.prototype.createAttrs=function(e){var t={href:e.getAnchorHref()},r=this.createCssClass(e);return r&&(t.class=r),this.newWindow&&(t.target="_blank",t.rel="noopener noreferrer"),this.truncate&&this.truncate.length&&this.truncate.length=a)return u.host.length==t?(u.host.substr(0,t-o)+r).substr(0,a+n):i(c,a).substr(0,a+n);var p="";if(u.path&&(p+="/"+u.path),u.query&&(p+="?"+u.query),p){if((c+p).length>=a)return(c+p).length==t?(c+p).substr(0,t):(c+i(p,a-c.length)).substr(0,a+n);c+=p}if(u.fragment){var h="#"+u.fragment;if((c+h).length>=a)return(c+h).length==t?(c+h).substr(0,t):(c+i(h,a-c.length)).substr(0,a+n);c+=h}if(u.scheme&&u.host){var f=u.scheme+"://";if((c+f).length0&&(g=c.substr(-1*Math.floor(a/2))),(c.substr(0,Math.ceil(a/2))+r+g).substr(0,a+n)}(e,r):"middle"===n?function(e,t,r){if(e.length<=t)return e;var n,o;null==r?(r="…",n=8,o=3):(n=r.length,o=r.length);var s=t-o,i="";return s>0&&(i=e.substr(-1*Math.floor(s/2))),(e.substr(0,Math.ceil(s/2))+r+i).substr(0,s+n)}(e,r):function(e,t,r){return function(e,t,r){var n;return e.length>t&&(null==r?(r="…",n=3):n=r.length,e=e.substring(0,t-n)+r),e}(e,t,r)}(e,r)},e}(),Te=function(){function e(e){this.__jsduckDummyDocProp=null,this.matchedText="",this.offset=0,this.tagBuilder=e.tagBuilder,this.matchedText=e.matchedText,this.offset=e.offset}return e.prototype.getMatchedText=function(){return this.matchedText},e.prototype.setOffset=function(e){this.offset=e},e.prototype.getOffset=function(){return this.offset},e.prototype.getCssClassSuffixes=function(){return[this.getType()]},e.prototype.buildTag=function(){return this.tagBuilder.build(this)},e}(),Le=function(e,t){return(Le=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)};function Re(e,t){function r(){this.constructor=e}Le(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}var Ne=function(){return(Ne=Object.assign||function(e){for(var t,r=1,n=arguments.length;r-1},e.isValidUriScheme=function(e){var t=e.match(this.uriSchemeRegex),r=t&&t[0].toLowerCase();return"javascript:"!==r&&"vbscript:"!==r},e.urlMatchDoesNotHaveProtocolOrDot=function(e,t){return!(!e||t&&this.hasFullProtocolRegex.test(t)||-1!==e.indexOf("."))},e.urlMatchDoesNotHaveAtLeastOneWordChar=function(e,t){return!(!e||!t)&&!this.hasWordCharAfterProtocolRegex.test(e)},e.hasFullProtocolRegex=/^[A-Za-z][-.+A-Za-z0-9]*:\/\//,e.uriSchemeRegex=/^[A-Za-z][-.+A-Za-z0-9]*:/,e.hasWordCharAfterProtocolRegex=new RegExp(":[^\\s]*?["+We+"]"),e.ipRegex=/[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?(:[0-9]*)?\/?$/,e}(),ut=function(e){function t(t){var r,n=e.call(this,t)||this;return n.stripPrefix={scheme:!0,www:!0},n.stripTrailingSlash=!0,n.decodePercentEncoding=!0,n.matcherRegex=(r=new RegExp("[/?#](?:["+Qe+"\\-+&@#/%=~_()|'$*\\[\\]?!:,.;✓]*["+Qe+"\\-+&@#/%=~_()|'$*\\[\\]✓])?"),new RegExp(["(?:","(",/(?:[A-Za-z][-.+A-Za-z0-9]{0,63}:(?![A-Za-z][-.+A-Za-z0-9]{0,63}:\/\/)(?!\d+\/?)(?:\/\/)?)/.source,rt(2),")","|","(","(//)?",/(?:www\.)/.source,rt(6),")","|","(","(//)?",rt(10)+"\\.",ot.source,"(?![-"+Ke+"])",")",")","(?::[0-9]+)?","(?:"+r.source+")?"].join(""),"gi")),n.wordCharRegExp=new RegExp("["+Qe+"]"),n.stripPrefix=t.stripPrefix,n.stripTrailingSlash=t.stripTrailingSlash,n.decodePercentEncoding=t.decodePercentEncoding,n}return Re(t,e),t.prototype.parseMatches=function(e){for(var t,r=this.matcherRegex,n=this.stripPrefix,o=this.stripTrailingSlash,s=this.decodePercentEncoding,i=this.tagBuilder,a=[],u=function(){var r=t[0],u=t[1],c=t[4],p=t[5],h=t[9],f=t.index,g=p||h,d=e.charAt(f-1);if(!at.isValid(r,u))return"continue";if(f>0&&"@"===d)return"continue";if(f>0&&g&&l.wordCharRegExp.test(d))return"continue";if(/\?$/.test(r)&&(r=r.substr(0,r.length-1)),l.matchHasUnbalancedClosingParen(r))r=r.substr(0,r.length-1);else{var m=l.matchHasInvalidCharAfterTld(r,u);m>-1&&(r=r.substr(0,m))}var b=["http://","https://"].find(function(e){return!!u&&-1!==u.indexOf(e)});if(b){var v=r.indexOf(b);r=r.substr(v),u=u.substr(v),f+=v}var k=u?"scheme":c?"www":"tld",A=!!u;a.push(new Oe({tagBuilder:i,matchedText:r,offset:f,urlMatchType:k,url:r,protocolUrlMatch:A,protocolRelativeMatch:!!g,stripPrefix:n,stripTrailingSlash:o,decodePercentEncoding:s}))},l=this;null!==(t=r.exec(e));)u();return a},t.prototype.matchHasUnbalancedClosingParen=function(e){var t,r=e.charAt(e.length-1);if(")"===r)t="(";else{if("]"!==r)return!1;t="["}for(var n=0,o=0,s=e.length-1;o"===e?(h=new ft(Ne({},h,{name:P()})),z()):He.test(e)||Ve.test(e)||":"===e||R()}function b(e){">"===e?R():He.test(e)?c=3:R()}function v(e){Ge.test(e)||("/"===e?c=12:">"===e?z():"<"===e?N():"="===e||$e.test(e)||Ze.test(e)?R():c=5)}function k(e){Ge.test(e)?c=6:"/"===e?c=12:"="===e?c=7:">"===e?z():"<"===e?N():$e.test(e)&&R()}function A(e){Ge.test(e)||("/"===e?c=12:"="===e?c=7:">"===e?z():"<"===e?N():$e.test(e)?R():c=5)}function y(e){Ge.test(e)||('"'===e?c=8:"'"===e?c=9:/[>=`]/.test(e)?R():"<"===e?N():c=10)}function x(e){'"'===e&&(c=11)}function w(e){"'"===e&&(c=11)}function C(e){Ge.test(e)?c=4:">"===e?z():"<"===e&&N()}function E(e){Ge.test(e)?c=4:"/"===e?c=12:">"===e?z():"<"===e?N():(c=4,u--)}function D(e){">"===e?(h=new ft(Ne({},h,{isClosing:!0})),z()):c=4}function _(t){"--"===e.substr(u,2)?(u+=2,h=new ft(Ne({},h,{type:"comment"})),c=14):"DOCTYPE"===e.substr(u,7).toUpperCase()?(u+=7,h=new ft(Ne({},h,{type:"doctype"})),c=20):R()}function B(e){"-"===e?c=15:">"===e?R():c=16}function q(e){"-"===e?c=18:">"===e?R():c=16}function F(e){"-"===e&&(c=17)}function M(e){c="-"===e?18:16}function S(e){">"===e?z():"!"===e?c=19:"-"===e||(c=16)}function T(e){"-"===e?c=17:">"===e?z():c=16}function L(e){">"===e?z():"<"===e&&N()}function R(){c=0,h=a}function N(){c=1,h=new ft({idx:u})}function z(){var t=e.slice(p,h.idx);t&&o(t,p),"comment"===h.type?s(h.idx):"doctype"===h.type?i(h.idx):(h.isOpening&&r(h.name,h.idx),h.isClosing&&n(h.name,h.idx)),R(),p=u+1}function P(){var t=h.idx+(h.isClosing?2:1);return e.slice(t,u).toLowerCase()}p=0&&n++},onText:function(e,r){if(0===n){var s=function(e,t){if(!t.global)throw new Error("`splitRegex` must have the 'g' flag set");for(var r,n=[],o=0;r=t.exec(e);)n.push(e.substring(o,r.index)),n.push(r[0]),o=r.index+r[0].length;return n.push(e.substring(o)),n}(e,/( | |<|<|>|>|"|"|')/gi),i=r;s.forEach(function(e,r){if(r%2==0){var n=t.parseText(e,i);o.push.apply(o,n)}i+=e.length})}},onCloseTag:function(e){r.indexOf(e)>=0&&(n=Math.max(n-1,0))},onComment:function(e){},onDoctype:function(e){}}),o=this.compactMatches(o),o=this.removeUnwantedMatches(o)},e.prototype.compactMatches=function(e){e.sort(function(e,t){return e.getOffset()-t.getOffset()});for(var t=0;to?t:t+1;e.splice(i,1);continue}e[t+1].getOffset()/i.test(e)}function bt(){var e=[],t=new gt({stripPrefix:!1,url:!0,email:!0,replaceFn:function(t){switch(t.getType()){case"url":e.push({text:t.matchedText,url:t.getUrl()});break;case"email":e.push({text:t.matchedText,url:"mailto:"+t.getEmail().replace(/^mailto:/i,"")})}return!1}});return{links:e,autolinker:t}}function vt(e){var t,r,n,o,s,i,a,u,l,c,p,h,f,g,d=e.tokens,m=null;for(r=0,n=d.length;r=0;t--)if("link_close"!==(s=o[t]).type){if("htmltag"===s.type&&(g=s.content,/^\s]/i.test(g)&&p>0&&p--,mt(s.content)&&p++),!(p>0)&&"text"===s.type&&dt.test(s.content)){if(m||(h=(m=bt()).links,f=m.autolinker),i=s.content,h.length=0,f.link(i),!h.length)continue;for(a=[],c=s.level,u=0;u + + + + Blog | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    Blog

    + +
    +
    +

    + How To Produce Semantically Correct MathML From XaTeX/LaTeX (and other accessibility ideas) +

    + +
    +

    During a recent run-in with the Simon Fraser Fraser University accessibility department, +I learned that they’re 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. +Here’s a quick guide!

    +
    +
    + +
    +
    +

    + Idea For A VPN Service +

    + +
    +

    Recently I’ve been thinking about starting a VPN service. +This service has some interesting requirements that I have never seen a VPN service do before, so I’d like to put down my thoughts as to what might be sensible for a centralized yet encrypted* VPN service.

    +
    +
    + + + +
    +
    +

    + Pinebook Pro, The Ultimate ARM Laptop +

    + +
    +

    I recently got my Pinebook Pro. +It was more expensive than I was expecting, coming in at (including shipping and handling) C$335. +I always forget the exchange rate and assume it’s similar to the U.S. dollar, but it never is, haha! +Anyway, this is just my first impressions and what I did to fix a few issues.

    +
    +
    + +
    +
    +

    + UEFI Development On x86 With EDK2 +

    + +
    +

    I made this blog so I could remember how to do stuff that had instructions spread around the internet. +So here is how I setup my environment for developing EFI applications.

    +
    +
    + + + +
    +
    +

    + Lichess Accessibility +

    + +
    +

    I wanted to play chess with somebody who used a screen reader, without requiring a screen reader myself; +some sites, like QuintenC’s Playroom have a rather poor visual interface for anyone who would like the play the game visually. +Lichess is an free and open-source website for chess players; +it bridges this gap by having two “modes” on the site: +standard mode and accessibility mode.

    +
    +
    + + + +
    +
    +

    + Orca, Emacspeak and Chromium Accessibility on A Raspberry Pi Running Manjaro ARM +

    + +
    +

    I wanted to get a gift for my blind friend who has been interested in learning Linux for a while now. +Just when I was about to start looking for something I decided to take a look at Brian Lunduke’s newest video featuring the Raspberry Pi 400. +The Raspberry Pi 400 has come full circle in terms of computing. +It is a keyboard. All the computing is done from within the keyboard. +Much like the Comodore64, this computer comes without a screen but is still technically fully functional without one. +I had my ‘Aha!’ moment, and decided that the Raspberry Pi 400 would be a very cool present.

    +
    +
    + + + + + +
    +
    +

    + Curiosity +

    + +
    +

    Curiosity is fundamental to a deep understanding of any subject. +Masters, Ph.Ds, and other fancy name suffixes will never help you +if you don’t have the spirit of curiosity burning inside of you.

    +
    +
    + +
    +
    +

    + Minesweeper Bomb Generation And Tile Revealing +

    + +
    +

    When I was creating a little Minesweeper game, I got confused at some points. +My bomb generation didn’t look quite right, and I for sure didn’t quite get the whole cascading tile reveal thing. +With a bit of internet research, I found what I was looking for. +I’ll explain it all in one place for my own research purposes.

    +
    +
    + + + +
    +
    +

    + How to Solve The Django Deployment Puzzle +

    + +
    +

    A few days ago I had a Django project I wanted to put on a real server. +This project is still in its infancy, but I thought it would be nice to put it on my resume and show my friends. +Little did I know the headache coming my way. +Here are some tips to help you not make the same mistakes as me.

    +
    +
    + +
    +
    +

    + BSD Journey, Part 1 +

    + +
    +

    As Linux becomes controlled by corporate sponsors and becomes more full of proprietary blobs, drivers, and even closed-source software like Steam, +One may wonder if there are other options out there. +For me, somebody that is intensely interested in security, there is one option: OpenBSD.

    +
    +
    + +
    +
    +

    + Know How Your Representative Votes In Parliament +

    + +
    +

    As an advocate for openness, I had an idea to make a project out of the government of Canada’s Open Data +initiative to take a look at how my local MP voted on various pieces of legislation. +It turns out though that this was not necessary due to how easy it was to find this information on the government’s own website. +In this article, I will explain how you can do the same.

    +
    +
    + +
    +
    +

    + Installing MultiCraft on Gentoo Linux +

    + +
    +

    In a very odd combination of requirements, +I needed to install MultiCraft on a Gentoo Linux system. +The PHP USE flags are important so you don’t have to recompile it three times like I did.

    +
    +
    + +
    +
    +

    + Independence +

    + +
    +
    +

    “When given a choice between independence and dependence, always choose independence; you will never regret that choice!”—Luke Smith

    +
    +
    +
    + +
    +
    +

    + How to use tmux to send and receive things from your Minecraft server +

    + +
    +

    So recently I had problem. +I run a Minecraft server on a big Linux computer I have running in my room. +Now, as a system administrator it is very helpful to be able to run some simple commands without needing to login with my key, password, TFA, etc. +It is, frankly, a lot of work. +Especially when I really just want to be playing games but I just need to check something quickly.

    +
    +
    + +
    +
    +

    + Site Update +

    + +
    +

    I updated the site with some easier to identify information about me and my projects :)

    +
    +
    + +
    +
    +

    + New Game: Clue (coming soon) +

    + +
    +

    Ooo! Exciting! +Today I want to announce a new project I’ll be working on which should be live within the month of May: +Clue.

    +
    +
    + + + +
    +
    +

    + What is XSS? +

    + +
    +

    I found a cross-site scripting (XSS) attack +in a well-known quiz hosting website. +I disclosed the vulnerability to them years ago, so I thought +now might be a good time to write about it.

    +
    +
    + + + + + +
    +
    +

    + How To Encrypt Your Own Documents Using gpg +

    + +
    +

    If you have ever wanted to garuntee the utmost security of your emails and documents, then this is the guide for you! +It should be noted that in some circles the tools used are more common than in others. +These are the everyday tools of many privacy advocates and computer nerds.

    +
    +
    + + + +
    +
    +

    + How Does Encryption Work, in Theory? +

    + +
    +

    There are many kinds of encryption used in our everyday communication. Online and offline, over the internet and in person. In this article, I will explain the basics of how encryption should work in theory. I explain in this article why encryption is important, and why you should care about it.

    +
    +
    + +
    +
    +

    + Is Encryption Worth It? +

    + +
    +

    What is the most embarassing thing you have typed into Google search? What is the most personal secret you told a friend in confidence? What is your bank password? What is your business’s secret to stay ahead of the competition?

    +
    +
    + +
    +
    +

    + Padding And Margin +

    + +
    +

    Many people have expressed confusion over how padding and margins work in HTML/CSS. I have been one of those people. In this short article I will explain what the differences are between the two, and how it may affect the functionality of your site.

    +
    +
    + + +
    +
    + +
    + + diff --git a/_site/book-notes/canadian-history-for-dummies/index.html b/_site/book-notes/canadian-history-for-dummies/index.html new file mode 100644 index 0000000..8b7d853 --- /dev/null +++ b/_site/book-notes/canadian-history-for-dummies/index.html @@ -0,0 +1,74 @@ + + + + + Canadian History For Dummies | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +
    +
    +

    Canadian History For Dummies

    + +
    +
    + +

    Lookup more info on these isolated languages:

    + +
    +

    “The Pacific Northwest was the most densely populated area of Canada. +It has been estimated that almost half of Canada’s total native population was living in British Columbia at the time of first contact. +More than 30 languages were spoken here, making it one of the most linguistically diverse areas anywhere on earth. +And two of the languages (Haida and Tlingit) are isolates: +unique and unrelated to any others.”— +[1, P. 25]

    +
    + +
    + +

    Find info on thie document this is taken from:

    + +
    +

    “To seeke out, discover and finde whatsoever liescountreyes, regions or provinces of the hethen and infidells … whiche before this time have beene unknowen to all Christians”—from the directives given to John Cabot by Henry VII +[1, P. 38]

    +
    + +
    + +
    1. [1]W. Ferguson, Canadian History For Dummies, 2nd Edition. John Wiley & Sons Canada, Ltd., 2005.
    + +
    + +
    +
    + +
    + + diff --git a/_site/book-notes/index.html b/_site/book-notes/index.html new file mode 100644 index 0000000..0c3273b --- /dev/null +++ b/_site/book-notes/index.html @@ -0,0 +1,49 @@ + + + + + Book Notes | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    + + +
    +
    + +
    + + diff --git a/_site/book-notes/industrial-society-and-its-future/index.html b/_site/book-notes/industrial-society-and-its-future/index.html new file mode 100644 index 0000000..07130cf --- /dev/null +++ b/_site/book-notes/industrial-society-and-its-future/index.html @@ -0,0 +1,43 @@ + + + + + Industrial Society and Its Future | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    + + +
    +
    + +
    + + diff --git a/_site/contact/index.html b/_site/contact/index.html new file mode 100644 index 0000000..b47cbaf --- /dev/null +++ b/_site/contact/index.html @@ -0,0 +1,56 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    Here are my contact details if you would like to contact me professionally or personally.

    + +
    Personal Email - tait.hoyem@protonmail.com
    +


    + +
    Work Email - tait@tait.tech
    +


    + +
    School Email - tait.hoyem@edu.sait.ca
    +


    + +
    Phone - +1 (403) 771-4754
    +


    + + +
    +
    + +
    + + diff --git a/_site/cover-letter-vcc/index.html b/_site/cover-letter-vcc/index.html new file mode 100644 index 0000000..dd1bf4f --- /dev/null +++ b/_site/cover-letter-vcc/index.html @@ -0,0 +1,42 @@ + + + + + Cover Letter - VCC | tait.tech + + + + + + +
    +
    +

    Tait Hoyem

    + +

    Hello, Alice

    + +

    This will be short and sweet.

    + +

    Copying and explaining information in accessible ways is something I have been doing my entire life—sometimes without realizing it. +As a partially-sighted student, +I know what information makes sense without visuals and what information will be completely meaningless without additional context or description.

    + +

    I also have some background in computer science. +This allows me to transcribe and explain only what is important. +Somebody without this background may not know when a number being shown on screen is important or when it is genuinely meaningless. +I also do not need to go back and forth to the text I am copying from to be one-hundered percent sure I have the right paranthasies, brackets, less-than signs, et cetera. +All these patterns make sense to me when I read them.

    + +

    Although I would prefer to work with computer science students, +I am willing to be used more broadly if that is of use to you.

    + +

    Thank you for your consideration,

    + +

    —Tait Hoyem

    + +
    +
    +
    +
    + + diff --git a/_site/cover-letters/bloombase/index.html b/_site/cover-letters/bloombase/index.html new file mode 100644 index 0000000..fae5048 --- /dev/null +++ b/_site/cover-letters/bloombase/index.html @@ -0,0 +1,61 @@ + + + + + Cover Letter | Bloombase | tait.tech + + + + + + +
    +
    +
    +To: Bloombase
    +
    +128 W Hastings St #350
    +Vancouver, British Columbia
    +V6B 1G8, Canada
    +
    +Job Number: 620145
    +
    + +

    I am a student at the Southern Alberta Institute of Technology (SAIT), +in Calgary, Alberta +where I am taking a general IT diploma with a software development major.

    + +

    The simple way of explaining my relationship with security is that it is exactly what I want to be doing with my career. +When given an assignment to chose any technology related topic, +I chose to talk about Edward Snowden; +for an emerging trend topic, I delved into public-key encryption and the Curve25519 eliptic crytographic method.

    + +

    I run Linux as my primary operating system, and I use an offline password manager. My browser clears cookies every time I restart it. +These are some basic things I do personally to protect myself.

    + +

    I think everybody has things they should be keeping secure, +and I am very motivated to help them achieve the messure of security needed.

    + +

    Security is build right into my goals for my programming career. I have two goals:

    + +
      +
    1. Create ease of use for strong crytography, security and privacy tools.
    2. +
    3. Accessibility of software to the visually impaired.
    4. +
    + +

    These goals are intertwined in: “If you make something good, make it for everyone.”

    + +

    I’ve included my resume for your review, and would like to note that I would be pleased to relocate to Vancouver for this position.

    + +

    Thank you for your time in considering me for this position!

    + +

    Sincerely,

    + +

    Tait Hoyem

    + +
    +
    +
    +
    + + diff --git a/_site/cover-letters/protonmail/index.html b/_site/cover-letters/protonmail/index.html new file mode 100644 index 0000000..478cb63 --- /dev/null +++ b/_site/cover-letters/protonmail/index.html @@ -0,0 +1,60 @@ + + + + + Cover Letter | Proton Technologies | tait.tech + + + + + + +
    +
    +
    +To: Proton Technologies AG
    +
    +Route de la Galaise 32,
    +1228 Plan-les-Ouates
    +Geneva, Switzerland
    +
    +Job Description: Front-End Developer
    +
    + +

    Why I Want To Work For Proton

    + +

    Hello,

    + +

    You may notice from the email address that I personally use a Protonmail email address. That in itself may not prove anything. +That said, security and privacy is what I live for. +Everybody deserves access to secure communication away from the prying eyes of the NSA, its global partners in spymenship and other governments and special interest groups around the world.

    + +

    Proton embodies, to me, what email—and other internet services in general—should be. A service you pay for with your money, not your data.

    + +

    What I Plan To Achieve

    + +

    My specialty is in improving accessibility with screen readers and other blind-friendly devices. +The recent addition of key bindings to navigate the interface will go a long way towards accessibility for the visually impaired. +Awesome work!

    + +

    I want to help Protonmail be the best secure, accessible email platform in the world.

    + +

    Why I Applied Despite Lack Of Experience

    + +

    I have been writing websites since high-school; I went to collage to learn what ‘real’ software development is like. Despite this, I put more time and effort into my off-school hours to learn what people are actually using on a large scale: Django, Ruby On Rails, Redis, Docker, ES6 Javascript. None of which was taught in class. +Since leaving school I have had the opportunity to use everything I learned out of class in real-world scenarios and I trust that I can take this information to Proton where I can help make the software better for everyone.

    + +

    I am a self-starter and a go-getter. Nothing proves it like how much I learned outside of my classes compared to inside.

    + +
    + +

    I would be honoured to work for a company I truly believe in.

    + +

    —Tait Hoyem

    + +
    +
    +
    +
    + + diff --git a/_site/emacspeak-tutorials/arrays/index.html b/_site/emacspeak-tutorials/arrays/index.html new file mode 100644 index 0000000..5f0f527 --- /dev/null +++ b/_site/emacspeak-tutorials/arrays/index.html @@ -0,0 +1,64 @@ + + + + + Arrays in C | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    Arrays in C, an Emacspeak Tutorial

    + +

    Arrays are sequential collections of objects of the same type (mostly). +Arrays and for loops are natural pairs. +Check out my resources on loops in conjunction with this video.

    + +

    Hopefully everything was explained alright. +Let me know if not.

    + + + + + + +
    +
    + +
    + + diff --git a/_site/emacspeak-tutorials/functions/index.html b/_site/emacspeak-tutorials/functions/index.html new file mode 100644 index 0000000..8bb970a --- /dev/null +++ b/_site/emacspeak-tutorials/functions/index.html @@ -0,0 +1,63 @@ + + + + + Functions in C | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    Functions in C, an Emacspeak Tutorial

    + +

    Functions allow us to modularize our programs into many small parts. +Of course if we need to do something once at the start of our program, and once again at the end, +it will be rather inconvenient to copy and paste all our code all the time.

    + +

    Hope everything makes sense. If not, let me know.

    + + + + + + +
    +
    + +
    + + diff --git a/_site/emacspeak-tutorials/index.html b/_site/emacspeak-tutorials/index.html new file mode 100644 index 0000000..38ff10d --- /dev/null +++ b/_site/emacspeak-tutorials/index.html @@ -0,0 +1,86 @@ + + + + + Emacspeak Tutorial Resources | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    Emacspeak Tutorial Resources

    + +

    This is a list of resources and videos associated with my emacspeak tutorials. +They are grouped by language and library. +Although I try my best to keep each individual video as self-sufficient as possible… +sometimes it’s not quite possible. Especially with complex libraries. +The lack of sorting is intentional. +See Luke Smith’s video on why to ramble and not be sequential.

    + + + + + +
    +
    + +
    + + diff --git a/_site/emacspeak-tutorials/loops/index.html b/_site/emacspeak-tutorials/loops/index.html new file mode 100644 index 0000000..44e6146 --- /dev/null +++ b/_site/emacspeak-tutorials/loops/index.html @@ -0,0 +1,63 @@ + + + + + Loops in C | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    Loops in C, an Emacspeak Tutorial

    + +

    For loop, while loop! Why all the confusion?

    + +

    I explain in my own way how loops work using basic C programming techniques to do many things in very few lines. +Hopefully you don’t get payed by lines of code because that would be ridiculous.

    + + + + + + + +
    +
    + +
    + + diff --git a/_site/emacspeak-tutorials/pointers/index.html b/_site/emacspeak-tutorials/pointers/index.html new file mode 100644 index 0000000..aebcd70 --- /dev/null +++ b/_site/emacspeak-tutorials/pointers/index.html @@ -0,0 +1,65 @@ + + + + + Pointers in C | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    Pointers in C, an Emacspeak Tutorial

    + +

    Pointers are often seen as this complicated thing, +but really they’re just like a home address. +You can go somewhere to find something specific. +123 Main St. is where Bob lives, +and as long as I know Bob lives there we can say “Deliver groceries to 123 Main St.” and we know this means that Bob will get his groceries.

    + +

    Hopefully everything is explained well. The resources are as follows:

    + + + + + + +
    +
    + +
    + + diff --git a/_site/emacspeak-tutorials/printing/index.html b/_site/emacspeak-tutorials/printing/index.html new file mode 100644 index 0000000..710dfd4 --- /dev/null +++ b/_site/emacspeak-tutorials/printing/index.html @@ -0,0 +1,62 @@ + + + + + Printing in C | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    Printing in C, an Emacspeak Tutorial

    + +

    “Hello World!”

    + +

    The most famous incantation among programmers and software engineers since it was popularized in the 1990s by Ken Thompson and Dennis Richie. +Today I will explain how to make your own “Hello World!” application in C using the Emacspeak environment for the blind and visually impaired.

    + + + + + + +
    +
    + +
    + + diff --git a/_site/emacspeak-tutorials/structs/index.html b/_site/emacspeak-tutorials/structs/index.html new file mode 100644 index 0000000..224cb65 --- /dev/null +++ b/_site/emacspeak-tutorials/structs/index.html @@ -0,0 +1,65 @@ + + + + + Structs in C | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    Structs in C, an Emacspeak Tutorial

    + +

    Structs are a way of grouping related data together. +Think of a struct the same way your would any other noun. +They often have many individual attributes that combine to make that thing. +For example, a student may have a first name, last name, student ID, GPA, list of classes, etc. +In this tutorial I use a very trimmed down version of what a structure generally contains to make the point without overwhelming you.

    + +

    Hopefully it helps. Let me know about any improvements.

    + + + + + + +
    +
    + +
    + + diff --git a/_site/emacspeak-tutorials/user-input/index.html b/_site/emacspeak-tutorials/user-input/index.html new file mode 100644 index 0000000..a0220d7 --- /dev/null +++ b/_site/emacspeak-tutorials/user-input/index.html @@ -0,0 +1,62 @@ + + + + + User Input and Variables in C | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    User Input and Variables in C, an Emacspeak Tutorial

    + +

    How do you interact with a user in C?

    + +

    Check out my emacspeak tutorial on how to get user input of numbers (like ages) and strings (like names and addresses).

    + + + + + + + +
    +
    + +
    + + diff --git a/_site/feed.xml b/_site/feed.xml new file mode 100644 index 0000000..48f923c --- /dev/null +++ b/_site/feed.xml @@ -0,0 +1,1223 @@ +Jekyll2021-11-16T23:18:30-07:00/feed.xmlHow To Produce Semantically Correct MathML From XaTeX/LaTeX (and other accessibility ideas)2021-09-18T00:00:00-06:002021-09-18T00:00:00-06:00/2021/09/18/how-to-generate-proper-content-mathml-from-katex-or-latex<p>During a recent run-in with the Simon Fraser Fraser University accessibility department, +I learned that they’re 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. +Here’s a quick guide!</p> + +<h2 id="diagrams">Diagrams</h2> + +<p>Most unexplained diagrams I saw were ones which mirrored classic computer-science imagery; +these diagrams, for the most part, were not complex nor exotic; +they are straight-forward to explain in writing, +or easy to turn into a table. +I’ll show two examples here, +one will show a visual aide in relation to stacks and queues, +and the other will show a memory representation of a stack. +Both of these were explained as “image” to the student.</p> + +<h2 id="stacks">Stacks</h2> + +<p>Diagram 1:</p> + +<figure> + <img src="/assets/img/access1/stack.png" alt="image...lol! Just kidding, will explain it below w/ table" /> + <figcaption>Simple diagram explaining the push/pop process. Source: <a href="https://stackoverflow.com/questions/32151392/stacks-queues-and-linked-lists">Stackoverflow</a></figcaption> +</figure> + +<p>Ok, so here we have a diagram showing the pushing and popping process of a stack. +Now, “image” is hardly sufficient to explain this, so let’s try it with text. +I won’t finish it because it gets unwieldy very fast:</p> + +<blockquote> + <p>A diagram showing a stack. It starts with the operation “Push A”, and now the stack contains the variable “A”; now the stack pushes “B”, which displays now “B” on top of “A”…</p> +</blockquote> + +<p>This is no solution. +It is hard to explain this correctly and accurately without being extremely verbose and frankly, confusing—this defeats the whole purpose of describing the image. +The good news, is that computer science diagrams especially tend to lean towards being tabular data. +Now to be clear, something does not need to look like a table to be tabular data; +this image happens to look almost like a table if you squinted hard enough, +but many data not written down in a table, are still “tabular data”. +I will show an example of that next! +For now though, here is the same idea, same data without words:</p> + +<table> + <thead> + <tr> + <th>Operator</th> + <th>Stack Values</th> + </tr> + </thead> + <tbody> + <tr> + <td>Push A</td> + <td>[A]</td> + </tr> + <tr> + <td>Push B</td> + <td>[B, A]</td> + </tr> + <tr> + <td>Push C</td> + <td>[C, B, A]</td> + </tr> + <tr> + <td>Push D</td> + <td>[D, C, B, A]</td> + </tr> + <tr> + <td>Pop D</td> + <td>[C, B, A]</td> + </tr> + </tbody> +</table> + +<p>Now this diagram does imply you can pop other items, like “Pop A”, which is just not true. +But that’s the fault of the diagram, not the representation of it.</p> + +<p>Here is the raw text equivalent (in Markdown):</p> + +<pre> +Operator|Stack Values +---|--- +Push A|[A] +Push B|{B, A] +Push C|[C, B, A] +Push D|[D, C, B, A] +Pop (D)|[C, B, A] +</pre> + +<h2 id="stacks-in-memory">Stacks in Memory</h2> + +<p>So I couldn’t find a good non-copyright image of a stack in memory, but I’ll write it down here in plain text, and you should get the idea. +Now again, remember this is still labeled “image” to the student, +they do not have access to a text version of this.</p> + +<pre> +( ) ( ( ( ) ) ) ( ) ( ( ) ( ( ) +1 0 1 2 3 2 1 0 1 0 1 2 1 2 3 2 +</pre> + +<p>Now, someone who looks at this can probably see that the number goes up for a left parenthesis, and down for a right parenthesis. +“Image”, however, does not handle the detail. +The issue here is a transcriber is likely to want to transcribe this as <em>text</em>. +But it’s really not. +This is again, tabular data, which is best represented in a table.</p> + +<p>Table of this:</p> + +<table> + <thead> + <tr> + <th>Character</th> + <th>Counter</th> + </tr> + </thead> + <tbody> + <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>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>(</td> + <td>1</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> + </tbody> +</table> + +<p>Raw text in markdown:</p> + +<pre> +Character|Counter +---|--- +(|1 +)|0 +(|1 +(|2 +(|3 +)|2 +)|1 +)|0 +(|1 +)|0 +(|1 +(|2 +)|1 +(|2 +(|3 +)|2 +</pre> + +<p>Insanely simple! +Look for clues of tabular data. +Things which have a one to one correspondence of any kind can usually be represented as a table, even if it’s only “aligned” on the slide or note.</p> + +<h2 id="math-expressions--mathml">Math Expressions &amp; MathML</h2> + +<p>Here is a more complex example: +using math within a presentation.</p> + +<p>Let’s take for example the mathematical expression <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>16</mn><mo>=</mo><msup><mn>2</mn><mn>4</mn></msup></mrow><annotation encoding="application/x-tex">16 = 2^{4}</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">16</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">4</span></span></span></span></span></span></span></span></span></span></span></span>. This is a very simple math expression that completely breaks in some cases. +When converting some math expressions to text, it will convert that expression as <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>16</mn><mo>=</mo><mn>24</mn></mrow><annotation encoding="application/x-tex">16 = 24</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">16</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">24</span></span></span></span>, erasing the superscript to denote the exponent.</p> + +<p>This gets even worse with large mathematical expressions like this:</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>B2U</mtext><mo stretchy="false">(</mo><mi>X</mi><mo stretchy="false">)</mo><mo>=</mo><munderover><mo>∑</mo><mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow><mrow><mi>w</mi><mo>−</mo><mn>1</mn></mrow></munderover><msub><mi>x</mi><mi>i</mi></msub><mo>×</mo><msup><mn>2</mn><mi>i</mi></msup></mrow><annotation encoding="application/x-tex"> +\text{B2U}(X) = \sum_{i=0}^{w-1} x_{i} \times 2^{i} +</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">B2U</span></span><span class="mopen">(</span><span class="mord mathdefault" style="margin-right:0.07847em;">X</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:3.0787820000000004em;vertical-align:-1.277669em;"></span><span class="mop op-limits"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.8011130000000004em;"><span style="top:-1.872331em;margin-left:0em;"><span class="pstrut" style="height:3.05em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathdefault mtight">i</span><span class="mrel mtight">=</span><span class="mord mtight">0</span></span></span></span><span style="top:-3.050005em;"><span class="pstrut" style="height:3.05em;"></span><span><span class="mop op-symbol large-op">∑</span></span></span><span style="top:-4.300005em;margin-left:0em;"><span class="pstrut" style="height:3.05em;"></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 class="mbin 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:1.277669em;"><span></span></span></span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord mathdefault">x</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: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">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="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.8746639999999999em;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.8746639999999999em;"><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">i</span></span></span></span></span></span></span></span></span></span></span></span></span></p> + +<p>Here is what I get by extracting the text from the PDF:</p> + +<pre> +B2U(X ) = + +w-1 + +Σ xi •2 +i=0 + +i +</pre> + +<p>And this is generous, as the sigma sign, bullet point, equal sign and minus sign were for some reason not UTF-8 encoded so it displayed as a chat sign emoji, down arrow, video camera and book sign respectively. +Not sure about you, but I certainly can’t get the equation out of that mess.</p> + +<p>These can be written in LaTeX, then converted to MathML (an accessible math format) using <a href="https://katex.org">KaTeX</a>. +Here’s an example of what to write to product the function above:</p> + +<pre> +\text{B2U}(X) = \sum_{i=0}^{w-1} x_{i} \times 2^{i} +</pre> + +<p>For someone who is doing transcription as a <em>job</em> for visually impaired students, +I would go so far as to say to learn this is a necessity.</p> + +<ol> + <li>It’s not difficult. You can learn the vast majority of LaTeX math syntax in an afternoon.</li> + <li>It’s easier for <em>everyone</em> to read. Especially with KaTeX. KaTeX is able to convert the formula to both MathML for screenreader users and HTML markup for people who just want to see those fancy math expressions.</li> +</ol> + +<p>Likely, the teacher is already using some LaTeX derivative to create the math in the first place, +they might as well use a program like KaTeX, MathJax or likewise to convert it to MathML.</p> + +<h2 id="code--output">Code &amp; Output</h2> + +<p>How did it even happen that entire programs and outputs were just ignored with the label “picture of output” is beyond me. +Everything should be transcribed. +Whoever transcribed that document should be fired.</p> + +<h2 id="conclusion">Conclusion</h2> + +<p>To teachers:</p> + +<p>Presenting information in plain text, or at least having alternates forms of images, diagrams and math formulas makes education better for everyone, not just blind students. +It makes it better for people running on cheaper devices which may not handle running heavy software like Microsoft PowerPoint; +it makes it better for people who use operating systems other than MacOS and Windows (this is especially important in the technology sector, where Linux/BSD users make up a sizeable minority of users); +and finally, it makes it easier to search through the content of all notes at once using simple text-manipulation tools.</p> + +<p>To accessibility departments:</p> + +<p>Running a <code class="language-plaintext highlighter-rouge">pdftotext</code> program, or simply transcribing handwritten notes is not enough to properly describe slides and notes—handwritten or not. +Every diagram, math equation, annotation, piece of code or output—every single thing must be transcribed to plain text, or some alternate format like MathML.</p> + +<p>I find it sad that a student (with their own full-time job) can product better work than someone who has this job exclusively at a major university. +Perhaps I am mistaken and the university has volunteers do this work. +In that case I guess you can’t ask for too much, but somehow I feel like this is probably not the case.</p> + +<p>Big sad.</p>During a recent run-in with the Simon Fraser Fraser University accessibility department, I learned that they’re 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. Here’s a quick guide!Idea For A VPN Service2021-08-31T00:00:00-06:002021-08-31T00:00:00-06:00/2021/08/31/vpns-api<p>Recently I’ve been thinking about starting a VPN service. +This service has some interesting requirements that I have never seen a VPN service do before, so I’d like to put down my thoughts as to what might be sensible for a centralized yet encrypted* VPN service.</p> + +<p>I would license all the code and scripts under the AGPLv3. +This creates an environment where I could allow my company to use this code, and any other company for that matter. However, no company would be allowed to take it into their own hands and use it without contributing back to the project.</p> + +<h2 id="e2ee-vpn">E2EE VPN</h2> + +<p>I want this service in many ways to be on par with <a href="https://protonmail.com">ProtonMail</a>: +end-to-end encrypted (E2EE), and with a focus in data security for the user of the service.</p> + +<p>Full encryption, so that even me, the writer and the deployer of the service, cannot view any information about the user: this is the utmost security. +The bad news is that this is very hard to do in a convenient way. +I’ve decided for now that the best thing to do is to target the Linux nerd. +Target the user who is familiar with these advanced security practices, then make them available to the general public as the layers on top of the robust security are refined.</p> + +<h2 id="why">Why?</h2> + +<p>End-to-end encryption is necessary in a country like Canada, where I may be sent a subpoena to provide customer data. +This is the case especially in the <a href="https://en.wikipedia.org/wiki/Five_Eyes">Five Eyes</a> anglophone group of countries, who essentially spy on each others’ citizens for eachother. +In essence, any data in the hand of one government agency in the “Eyes Countries” may be shared between the Five, Nine, and 14 Eyes countries.</p> + +<p>I am not against government surveillance <em>in principle</em>. +In theory, the government should be finding bad guys: pedophiles, sex trafficking rings and drug cartels. +In practice, the U.S. government especially, uses its authority to spy on its own citizens who are simply minding their own business. <del>Bulk data collection</del> mass surveillance is not a freedom respecting characteristic of modern western democracies. +I do run the risk of not being able to help much in the case of a genuine warrant against a genuine, evil criminal. +That is the risk of privacy.</p> + +<p>That said, let’s see what can be built that can do these 2 things:</p> +<ol> + <li>Maximize privacy for the user.</li> + <li>Allow for (optional) monetization, depending on the provider. This is in some contradiction to premise 1.</li> +</ol> + +<h2 id="what-we-need">What We Need</h2> + +<p>A VPN service needs access to some basic information:</p> +<ol> + <li>Service discontinue time (the amount of time until the customer must renew).</li> + <li>Active connections (a number which can not be exceeded by an individual user).</li> +</ol> + +<p>The client needs access to some information from the server as well:</p> +<ol> + <li>A list of VPNs able to be connected to (with filters).</li> + <li>For every VPN: + <ol> + <li>IP Address.</li> + <li>Maximum bandwidth.</li> + <li>Number of connected users or connection saturation percentage.</li> + <li>Supported protocols.</li> + </ol> + </li> +</ol> + +<p>Can we do this in a end-to-end encrypted fashion? +I’m honestly not sure. But here are my ideas so far as to how <em>some</em> of these functions might work.</p> + +<h2 id="how-to-do-it">How To Do It</h2> + +<h3 id="usernames">“Usernames”</h3> + +<p>There will be one button to create your account: <em>“Generate username”</em> +The username, or unique identifier for a user will be generated for them by a random generator. +I plan to generate a username from a list of <a href="https://en.wikipedia.org/wiki/Base64">Base 64</a> characters; it will be a guaranteed length of 16. +This gives a total of: <code class="language-plaintext highlighter-rouge">79228162514264337593543950336</code> or <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>7.9</mn><mo>×</mo><mn>1</mn><msup><mn>0</mn><mn>28</mn></msup></mrow><annotation encoding="application/x-tex">7.9 \times 10^{28}</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">7</span><span class="mord">.</span><span class="mord">9</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">2</span><span class="mord mtight">8</span></span></span></span></span></span></span></span></span></span></span></span> posibilities. +This is sufficient for a username.</p> + +<p>The other option is to use a standard “username” field that uses a modern hash function like <a href="https://en.wikipedia.org/wiki/Secure_Hash_Algorithms">SHA512</a> to store it in the database. +This is less secure as it is vulnerable to a brute-force attack of finding users, +but this is also a very easy attack to defend against, i.e. IP banning after 10-ish tries of not finding a username.</p> + +<p>A <em>non-unique, universal</em> <a href="https://en.wikipedia.org/wiki/Salt_(cryptography)">salt</a> will also be used on each username before storing it in the database to make it more secure. +This decreases the possibility of an advanced attacker being able to find usernames in a leaked database using <a href="https://en.wikipedia.org/wiki/Rainbow_table">rainbow tables</a>. +That said, the fact that it is a fixed salt makes it much more vulnerable to an attack. +Although it would be known only by the server machine, it would still be somewhat of a vulnerability. +The operator may also store the salt in an encrypted password store of their own in case the server is erased, broken into, etc. +It would be fairly easy, if they have access to the active salt, to migrate to a new salt every few days/months, or perhaps every time a server upgrade/maintenance happens. +This does run the possibility of larger issues if the server is shut down or hangs during a migration and needs to be restarted. +Many users may end up with accounts they cannot access without manual cleanup.</p> + +<p>In the end, the <em>application</em> would need a backup of this salt, otherwise login times would become linear to the number of users as the database checks every user’s salt to see if it matches the hash made with the username input. +Note that the <em>database</em> does not store the salt, so finding it will be very hard, even in the case of a leaked database.</p> + +<p>So, here’s the overview: +The username will be generated, then stored <em>after</em> being salted and hashed. +The salt will be a fixed or rolling salt across all usernames to avoid linear scaling of searching for a user. +The server will only see the username once, when sending it to the user for them to save for the first time; +there will be no database entry with the original username in it.</p> + +<p>This does mean that if the username is lost, the account is lost too. There is no way to recover the account. +Again, this is ok for now, as my target audience is advanced Linux and privacy enthusiasts.</p> + +<h3 id="passwords">“Passwords”</h3> + +<p>There are a few options for passwords/secret keys.</p> + +<p>I think the best is to treat it similarly to the username is above, except it will <em>not</em> be generated for you. +When a new account is generated, you will be taken to a password reset screen where you will set your password to whatever your want, using your own secure system to handle it. +This is ideal for Linux and tech enthusiasts as they generally already have a password management system setup.</p> + +<p>This will also be salted, with its own unique salt, then hashed and stored alongside the username.</p> + +<h3 id="active-time-remaining">Active Time Remaining</h3> + +<p>It is easy and ideal to have a field connected to a user with their expiry date for their account. +When a payment is made, this date will be increased by the number of days, hours and minutes proportional to the payment received.</p> + +<p>For example: if a “month” (30 days) costs ten dollars, then a payment of fifteen dollars would add 45 days to an account. So essentially 33 cents per day, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mn>10</mn><mrow><mn>30</mn><mo>×</mo><mn>24</mn></mrow></mfrac><mo>=</mo><mn>0.0138</mn></mrow><annotation encoding="application/x-tex"> \frac{10}{30 \times 24}=0.0138</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.2484389999999999em;vertical-align:-0.403331em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.845108em;"><span style="top:-2.655em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">3</span><span class="mord mtight">0</span><span class="mbin mtight">×</span><span class="mord mtight">2</span><span class="mord mtight">4</span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.394em;"><span class="pstrut" style="height:3em;"></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.403331em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></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">0</span><span class="mord">.</span><span class="mord">0</span><span class="mord">1</span><span class="mord">3</span><span class="mord">8</span></span></span></span> dollars per hour, or <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mn>10</mn><mrow><mn>30</mn><mo>×</mo><mn>24</mn><mo>×</mo><mn>60</mn></mrow></mfrac><mo>=</mo><mn>0.00023</mn><mover accent="true"><mn>148</mn><mo stretchy="true">‾</mo></mover></mrow><annotation encoding="application/x-tex">\frac{10}{30 \times 24 \times 60}=0.00023\overline{148}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.2484389999999999em;vertical-align:-0.403331em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.845108em;"><span style="top:-2.655em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">3</span><span class="mord mtight">0</span><span class="mbin mtight">×</span><span class="mord mtight">2</span><span class="mord mtight">4</span><span class="mbin mtight">×</span><span class="mord mtight">6</span><span class="mord mtight">0</span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.394em;"><span class="pstrut" style="height:3em;"></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.403331em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></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.8444400000000001em;vertical-align:0em;"></span><span class="mord">0</span><span class="mord">.</span><span class="mord">0</span><span class="mord">0</span><span class="mord">0</span><span class="mord">2</span><span class="mord">3</span><span class="mord overline"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8444400000000001em;"><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">1</span><span class="mord">4</span><span class="mord">8</span></span></span><span style="top:-3.76444em;"><span class="pstrut" style="height:3em;"></span><span class="overline-line" style="border-bottom-width:0.04em;"></span></span></span></span></span></span></span></span></span> dollars per minute. +This is the second biggest threat to the users’ data privacy, as this, by definition, cannot be encrypted as my server needs access to this data to decide whether a user should be allowed to: view a list of VPN nodes available to them or connect to a VPN. +The best I can think of in this case is:</p> +<ol> + <li>Use a system similar to the username: use a common salt and hash algorithm to store them in the database.</li> + <li>Use full-disk and full-database encryption to keep the data secure to outside attackers.</li> +</ol> + +<p>This is not a fantastic solution, and still has the threat of a service provider snooping in on the database. +The truth is: a service provider has root access to any machine it hosts. +This necessitates that the <em>physical</em> infrastructure hosting the central database server must by physically owned and operated by the VPN operator and not any third party. +In addition, it means top security root passwords, tamper resistant cases (in the case of a co-hosting or server room environment), sensors to indicate it has been opened or touched. +If you thought this was bad, wait until the next part.</p> + +<h3 id="active-connections">Active Connections</h3> + +<p>In order to stop a user from simply using the entire bandwidth of all the VPN nodes available to them, there must be a way to know how many active connections the user has. +This is <em>by far</em> the biggest issue in terms of user privacy. +There are a few options here:</p> +<ol> + <li>Do not have a limit on the number of connections a user may have. This is dangerous from a <a href="https://en.wikipedia.org/wiki/Denial-of-service_attack">DDoS (distributed denial-of-service)</a> perspective. +This also makes the VPN provider vulnerable to be used as a DDoS distribution method by putting all their traffic through the VPN provider, and them not having any logs—the bad guys could use the distributed nature of VPN nodes to attack whoever they see fit. +This is not a viable option.</li> + <li>Have a list of connected users sent to the central server every 15 to 30 seconds. This is fairly efficient, but more privacy invasive.</li> + <li>When a user connects, log an explicit “connect” message. +When a user disconnects, send an explicit “disconnect”. +Have the VPN server report an <em>implicit</em> “disconnect” after an amount of time, say 15 minutes, then send an implicit “connect” message once traffic continues. This is all in RAM under temporary storage and is lost upon restart of the server.</li> +</ol> + +<p>The best method (used currently by <a href="https://mullvad.net">Mullvad VPN</a>) is number 3.</p> + +<h2 id="panel">Panel</h2> + +<p>The admin panel will have some broad info about the nodes:</p> + +<ul> + <li>Active connections</li> + <li>Server load (held and reported every minute by the nodes themselves. Not sure how to do this yet.)</li> + <li>Location</li> + <li>IP Address</li> + <li>Failed connections in last X amount of time (i.e. invalid credentials)</li> + <li>Physical server status (i.e. owned by the hoster vs. contracted out to another hosting company in the area)</li> +</ul> + +<p>This panel would also have options to stop, start or soft stop the VPN service on each node for maintenance. +A soft stop will stop new connections and remove it from the list of available servers for the end-user. Users will disconnect whenever they feel like it—eventually winding down to zero connections. +This allows maintenance without service disruption.</p> + +<p>I’m not sure how to do this securely. +Best I can think of right now is have an admin login, then have the server have a key in each node machine. +This completely compromises the SSH key system though. +Now every node is secured with nothing but a password. Maybe the console will require connecting to a local instance on a machine through an encrypted connection which will require a key. +Even then, that does make every machine vulnerable to one point of failure (the key to connect to the local instance).</p> + +<p>Another way to approach this, security-wise is to make a shell script (or locally running flask app) which reads info about the servers from a sqlite database. +Then, it uses the local computer to connect to the servers—assuming the local machine has all the keys necessary to do so.</p> + +<p>This fixes one problem and creates another. +It fixes the single point of failure in the cloud. This <em>massively</em> reduces the attack surface to intentionally stealing physical hardware from trusted parties, or software-hacking these same trusted people. +But, if the key is lost by the host… The entire service is kaput. No maintenance may be performed, no checks, bans, addition of servers can be done whatsoever. +This also increases the possibility of sloppy security from trusted parties. +Perhaps a trusted member leaves his laptop unattended for a few minutes and a hacker is able to steal the simple key file. He’s in!!! +This is very unlikely, I must say, but it comes down to: should I trust people or machines more to keep the data secure. +Depending on the person, I might trust them more.</p> + +<h2 id="conclusion">Conclusion</h2> + +<p>With all of these ideas in mind, I have realized how difficult it really is to make a VPN service. +Boy do they deserve every dollar they get! +If you don’t have a VPN, get one. +Doesn’t really matter which one, unless you’re a nerd—for your average person you can just pick whatever the best deal is at the time and you’re off to the races.</p> + +<p>Anyway, I think I’ve rambled on long enough about VPNs and my crazy ideas, so I’m going to leave this one for now.</p> + +<p>Happy VPN hacking :D</p>Recently I’ve been thinking about starting a VPN service. This service has some interesting requirements that I have never seen a VPN service do before, so I’d like to put down my thoughts as to what might be sensible for a centralized yet encrypted* VPN service.UEFI Audio Protocol &amp; UEFI BIOS Accessibility2021-06-21T00:00:00-06:002021-06-21T00:00:00-06:00/2021/06/21/uefi-audio<p>Good news about the state of accessibility in the BIOS!</p> + +<h2 id="preamble">Preamble</h2> + +<p>On my <a href="/ideas/">ideas page</a>, I have always had up the idea of an accessibility layer for blind people to be able to use their BIOS. +Although it targets a very small percentage of the population, +computer programming is often at least a hobby of visually imapired individuals as it is (mostly) a text-based job: +You write code in plain text, then run it to get either plain text or some kind of HTML output. +Mostly an accessible career for those who cannot see. +That said, there has always been an issue with low-level computer infrastructure (i.e. the BIOS/UEFI). +These menus—which let you edit your boot order, RAM timings, CPU and GPU overclocking and sometimes even fan speed—they were completely inaccessible to those who could not see them. +Well, until… soon. I had a talk with one of the big bois working on EDK2, the UEFI implementation which is used by most motherboard vendors to create their firmware. +I thought I would share the info I understand, and the conversation in full.</p> + +<h2 id="news">News</h2> + +<p>Here is what I know:</p> + +<ol> + <li>This year, the GSoC (Google Summer of Code) project had <a href="https://summerofcode.withgoogle.com/projects/#6499615798460416">a submission of Ethin Probst</a> to implement VirtIO audio drivers for EDK2.</li> + <li><a href="https://qemu.org">QEMU</a>, the emulator that was chosen to test for this project does not have VirtIO support (yet). I haven’t found info on when this will be done.</li> + <li>Because of 2, Ethin and his mentors for his project, Ray Ni and Leif Lindholm decided to first implement USB-dongle audio support first, as this is a) supported in QEMU, and b) is good enough to start squashing bugs at the audio level.</li> + <li>Because GSoC is usually over around September, there will likely be some more news coming soon!</li> +</ol> + +<h2 id="the-irc-chat">The IRC Chat</h2> + +<p>Here is the log of the IRC chat for anyone who is interested in anything I might have missed:</p> + +<pre class="terminal"> +tait_dot_tech: Hello there, I'm new to IRC so just checking my messages are coming through. +tait_dot_tech: Looks light it's alright. Ok so I have a question: does anyone know of an active project looking at making UEFI accessible to the blind (i.e. speec) [sic] from within the UEFI environment? Main concern is having blind users be able to boot Linux USBs (I know, very niche thing), but depending on how good it is, could potentially be used to allow blind individuals to change their overclocking, +tait_dot_tech: hardware RAID, boot order, RAM timings, etc. all on their own. Just wondering if there is any project doing this? I have tried my best to find anything, and am just trying not to duplicate effort. Thanks :) +leiflindholm: tait_dot_tech: we have a google summer of code project running this year, prototyping a standard for audio output. To hopefully be added to the UEFI specification in the future. +leiflindholm: once we have a standard for audio output, we can work on adding support for audio output to the Human Interface Infrastructure +leiflindholm: which is the thing that lets menus be loaded and displayed independent of specific graphical implementation +tait_dot_tech: Oh wow! Glad to hear there is progress on this! Is there a link to the Google summer of code project, or anything else where I can keep tabs? +leiflindholm: tait_dot_tech: there isn't much yet, we're only on week 3 of GSoC. +leiflindholm: https://summerofcode.withgoogle.com/projects/#6499615798460416 is the link if it's something you want to point others to, but any discussion/reporting is likely to hapen [sic] on our mailing lists +tait_dot_tech: By "our" mailing list, do you mean GSoC, or Edk2? +leiflindholm: edk2 +leiflindholm: although, on average, at least 99% of edk2-devel will *not* be about audio support +leiflindholm: When we have anything interesting to say, we'll also post to edk2-discuss/edk2-announce +tait_dot_tech: Sweet! I'll join that one just in case! I'd be happy to test anything in beta-ish state and report back with any device I can get my hands on. Is that the right list to watch for hepling test it out? +leiflindholm: I'd say so. +leiflindholm: The original plan was to start with wirtio [sic] audio support, so anyone could help out anywhere, but that support is not yet upstream in qemu. So for now we're working on an [sic] USB audio class driver. That will certainly be useful to have more people testing with different equipment once we have something working. +tait_dot_tech: Ahh! So if I want to test, I should get a USB audio dongle. Gotcha! Thank you so much! You've been super helpful! +leiflindholm: np :) +</pre> + +<p>Things are (slowly) looking up for audio (and eventually screen-reader support) in UEFI! +Phew! Glad I’m not the only one thinking about this!</p> + +<p>Happy UEFI hacking :)</p>Good news about the state of accessibility in the BIOS!Pinebook Pro, The Ultimate ARM Laptop2021-06-02T00:00:00-06:002021-06-02T00:00:00-06:00/2021/06/02/pinebook-pro<p>I recently got my Pinebook Pro. +It was more expensive than I was expecting, coming in at (including shipping and handling) C$335. +I always forget the exchange rate and assume it’s similar to the U.S. dollar, but it never is, haha! +Anyway, this is just my first impressions and what I did to fix a few issues.</p> + +<h2 id="initial-impressions">Initial Impressions</h2> + +<p>My first impressions of this are quite good. +I like the keyboard; it is firm and not mushy for the price. +It actually has a similar keyboard to my school-supplied Dell, which I quite enjoyed typing on. +The shell is aluminium and doesn’t feel <em>too</em> cheap, but I should note that it sure doesn’t feel like a Macbook if that’s what you’re expecting. +All in all build quality seems pretty good for a product in this price range. +I’m actually using it right now to write this article, and I’m actually typing faster than I would on my desktop.</p> + +<p>The screen is bright enough and has anti-glare applied to it. I can use it with moderate light behind me, but not a sunset. Decent, and I can’t even use my phone with a sunset right on it, so that’s not a huge loss at all as I think my phone costs more than this haha!</p> + +<p>The trackpad is fine. +I don’t use the mouse very often, and if I need it I’m more likely to bring an external one. +It works for what I need though. +I can’t seem to get the glossy protector off the trackpad though so maybe it would be better if I did haha!</p> + +<p>The temperatures are okay. I would consider them not ideal. +The left side closer to the hinge can get quite warm when I push it. +To be expected in some respects, but the metal case certainly makes the heat come out fast and hot! +It is also passively cooled, so a bit of heat makes sense and is reasonable. +I wonder if I could mod this to have an active low-profile fan? +A project for later, I suppose.</p> + +<p>The keyboard is pretty standard for a 14-inch laptop. +No numpad (except with function key), has F1-12 and media keys using function+F1-12. +Screen brightness, sound up, down and mute, and num and scroll lock. +These seem to work no matter what distribution you have (I’ve used Manjaro KDE and Manjaro Sway). +Perhaps this would react differently on Arch for ARM with no key bindings. +I’m not sure if this is implemented in software or hardware.</p> + +<p>The speakers and very tin-y and do not sound good at all. +That said, they look very replaceable, so I’ll look into a mod in the future. +The Pinebook Pro comes with a headphone port, so you could just use that if the sound bothers you.</p> + +<h2 id="some-suggestions">Some suggestions</h2> + +<p>I had some issues when it first arrived.</p> + +<ol> + <li>Reboot did not work. The display would glitch out and show horizontal lines. It would only work after a full shutdown.</li> + <li>Booting would sometimes not work at all. My SD card would sometimes boot, sometimes not. eMMC would sometimes work and sometimes not. Sometimes I would even get to the login screen, or fully logged in before it decided to freeze/hang. I could “drop to console” (Ctrl+Alt+Fx), but it only made my mouse stop showing, it would not actually display a console. This problem was worse when not plugged in.</li> + <li>Performance was not stellar, even for the RK3399.</li> + <li>I don’t like the Manjaro logo that displays during boot.</li> +</ol> + +<h3 id="dont-use-kde">Don’t use KDE</h3> + +<p>KDE for me is a bit slow. +It is not a keyboard-driven desktop. +To give it some credit though, it does at least have zoom support built in; this is something I wish other desktops would have enabled by default. +I’m looking at your, Xfce.</p> + +<p>I switched to Manjaro Sway, which is a Wayland-based i3-like tiling window manager. +I’ve used this on my Raspberry Pi 4, and it is by far my preference among other default distro configurations.</p> + +<p>This can be done by flashing an SD card with any random Linux distro, then download <a href="">Manjaro Sway ARM for the Pinebook Pro</a>.</p> + +<p>Quickly, we should prepare the eMMC. Open <code class="language-plaintext highlighter-rouge">fdisk</code> with your eMMC module and remove all partitions. +If you have issues with this, check if any partition is mounted, unmount it, then try again. +<code class="language-plaintext highlighter-rouge">fdisk</code> is well documented elsewhere, so I won’t cover it here.</p> + +<p>Once your .xz file is downloaded, <code class="language-plaintext highlighter-rouge">unxz</code> the .xz file downloaded.</p> + +<pre class="terminal"> +$ cd ~/Downloads +$ unxz Manjaro-Sway-ARM-pbp-20.10.img.xz +</pre> + +<p>Not exactly those commands, but close.</p> + +<p>Once you have that, flash your eMMC by using <code class="language-plaintext highlighter-rouge">dd</code>.</p> + +<pre class="terminal"> +# dd if=./Manjaro-Sway-ARM-pbp-20.10.img of=/dev/mmcblkX bs=1M conv=fsync +</pre> + +<p>Now remove your SD card. +U-Boot will prefer your SD card over your eMMC, so if you leave it in, it <em>will</em> boot to your SD card.</p> + +<h3 id="flash-your-u-boot-bsp">Flash Your U-Boot (BSP)</h3> + +<p>U-Boot appeared to be the solution to my other two issues. +I was able to flash a new U-Boot program by using the following commands. +Be sure to run <code class="language-plaintext highlighter-rouge">lsblk</code> beforehand to know which <code class="language-plaintext highlighter-rouge">/dev/emmcblk</code> to write to. +Replace <code class="language-plaintext highlighter-rouge">X</code> with the correct number for your system.</p> + +<pre class="terminal"> +# pacman -S uboot-pinebookpro-bsp +# dd if=/boot/idbloader.img of=/dev/mmcblkX seek=64 conv=notrunc +# dd if=/boot/uboot.img of=/dev/mmcblkX seek=16384 conv=Notrunc +# dd if=/boot/trust.img of=/dev/mmcblkX seek=24576 conv=notrunc +</pre> + +<p>The <code class="language-plaintext highlighter-rouge">dd</code> instructions are printed out after installing the <code class="language-plaintext highlighter-rouge">uboot-pinebookpro-bsp</code> package, so make sure to follow what is printed there if it is different that what I have provided.</p> + +<p>After doing this, not only have I since booted 100% of the time, +but my display now works correctly after a reboot without a full shutdown.</p> + +<p>Whew! Looking good!!!</p> + +<h3 id="maybe-get-some-of-the-accessories">Maybe get some of the accessories</h3> + +<p>I didn’t buy any accessories from Pine64. +I regret this somewhat. +For one thing, without an accessory to read the eMMC over USB, you need to have a working Linux distro on the SD card to get anywhere with it. +Flashing directly to the eMMC would have saved me a <em>lot</em> of time.</p> + +<p>The other accessory I could see the occasional use for is the Ethernet adapter. +When downloading a big update (1GB+), it could be useful to wire in just temporarily. +Not a huge deal, but worth mentioning.</p> + +<p>I would also be interested in the other batteries they have available. +Even though it comes with a battery, and I also don’t think you can install a second one, I would be interested to see if I could get more life out of it with an improved battery. +If this is a standard battery (Pine64 tends to use standard parts), then I would consider getting it from a supplier as well.</p> + +<p>The Pinebook Pro does not come with any HDMI ports. +It comes with a USB type-C port that can be adapted to HDMI. +Or you can get a display that supports USB type-C. +I do not have a display that supports USB type-C, so it might be worth it for me to buy an adapter or find a compatible one more locally. +Shipping from Hong Kong ain’t cheap.</p> + +<h3 id="replace-the-boot-logo">Replace the boot logo</h3> + +<p>The boot splash screen can be replaced, but I haven’t figured out how yet. +I will post an update to the blog when I do find out.</p> + +<h2 id="conclusion">Conclusion</h2> + +<p>I really want to use the Pinebook Pro more. +Pine64 do a lot for the open-source community and they do their best to use only open hardware. +They do fail in some respects, but they do much better than the mainline distributors like Dell, HP or ASUS.</p> + +<p>Thanks, Pine64! I’m excited to use your products!</p> + +<p>Happy ARM hacking :)</p>I recently got my Pinebook Pro. It was more expensive than I was expecting, coming in at (including shipping and handling) C$335. I always forget the exchange rate and assume it’s similar to the U.S. dollar, but it never is, haha! Anyway, this is just my first impressions and what I did to fix a few issues.UEFI Development On x86 With EDK22021-04-18T00:00:00-06:002021-04-18T00:00:00-06:00/2021/04/18/uefi-development-environment<p>I made this blog so I could remember how to do stuff that had instructions spread around the internet. +So here is how I setup my environment for developing EFI applications.</p> + +<h2 id="requirements">Requirements</h2> + +<p>On Artix or other Arch-based distros like Manjaro I installed the following packages: <code class="language-plaintext highlighter-rouge">gcc nasm iasl</code></p> + +<p>Here is what the packages do:</p> + +<ul> + <li>GCC is obviously the GNU Compiler Collection and it allows us to compile C code to machine code.</li> + <li>NASM stands for Netwide Assembler. It is an assembler and disassembler for 32 and 64 bit Intel x86 platforms.</li> + <li>IASL stands for the ACPI Source Language Compiler/Decompiler. This will compile any ACPI calls to our local machine’s code.</li> +</ul> + +<p>We need all these packages to start our (U)EFI journey. +Now that these are installed, let’s setup our environment.</p> + +<h2 id="building-edk2">Building EDK2</h2> + +<p>I used the stable/202011 branch as that is latest stable version of the EDK2 project.</p> + +<p>So first let’s pull the project:</p> + +<p><code class="language-plaintext highlighter-rouge">git clone https://github.com/tianocore/edk2.git</code></p> + +<p>Now, let’s fetch the tags and switch to the latest stable version:</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cd edk2 +git fetch +git checkout stable/202011 +</code></pre></div></div> + +<p>Perfect! We’re on stable now! Let’s grab all our submodules: <code class="language-plaintext highlighter-rouge">git submodule update --init</code></p> + +<p>This will take a bit the first time you do it. But no fear, once that’s done, we can finally build the base tools.</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>make -C BaseTools +export EDK_TOOLS_PATH=$HOME/Documents/edk2/BaseTools +. edksetup.sh BaseTools +</code></pre></div></div> + +<p>Notice we source a file with <code class="language-plaintext highlighter-rouge">.</code> before continuing. This is needed to load some tools and options into our shell for later. The environment variable <code class="language-plaintext highlighter-rouge">EDK_TOOLS_PATH</code> is set so that EDK knows where to find itself later. Now that everything is loaded up, we can modify a config file located at <code class="language-plaintext highlighter-rouge">Conf/target.txt</code>.</p> + +<p>The most important options are these, feel free to append them to the end of the file; there is no default value for them.</p> + +<pre class="file"> +ACTIVE_PLATFORM = MdeModulePkg/MdeModulePkg.dsc +TOOL_CHAIN_TAG = GCC5 +# for 64-bit development +TARGET_ARCH = X64 +# for 32-bit development +TARGET_ARCH = IA32 +# for 32 and 64-bit development +TARGET_ARCH = IA32 X64 + +# set multithreading to 1 + (2 x # of cores) +MAX_CONCURRENT_THREAD_NUMBER = 9 +</pre> + +<p>There are other options, but I don’t know about them much, so I’m just sticking with this for now.</p> + +<p>Finally, after all this work, we can build some .efi files. Let’s compile the <code class="language-plaintext highlighter-rouge">Helloworld.efi</code> file! +Simply run the <code class="language-plaintext highlighter-rouge">build</code> command in the terminal. +You can find your compiled EFI files by running this <code class="language-plaintext highlighter-rouge">ls</code> command:</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ls Build/MdeModule/DEBUG_*/*/HelloWorld.efi +</code></pre></div></div> + +<p>This will show all HelloWorld.efi files for all architectures and toolchains (if you decide to change them).</p> + +<h2 id="running-in-uefi-shell">Running In UEFI Shell</h2> + +<p>Once all this is complete, you will want to run your EFI files. +To do so, let’s first add an EFI shell to use at boot. +This will appear as an option in your bootloader, like GRUB, which is what I will show documentation for in this article.</p> + +<p>So, first thing is first, +<a href="https://github.com/tianocore/edk2/blob/UDK2018/ShellBinPkg/UefiShell/X64/Shell.efi?raw=true">download and EFI shell file</a>. +Second, move it to a partition (FAT formatted) which can be used for the UEFI. +On my Linux system, this is /boot. On others there may be no FAT filesystem so attach a USB and format it as FAT. +Third, add the <code class="language-plaintext highlighter-rouge">EFI Shell</code> option to your grub boot file. +Substitute hdX with the right hard drive (I did it with trial and error) as when it doesn’t work I would hit ‘e’ on grub and add the <code class="language-plaintext highlighter-rouge">ls</code> GRUB command. +Substitute the gptX with the correct partition, or msdosX if it is a DOS formatted partition table. +My <code class="language-plaintext highlighter-rouge">Shell.efi</code> was placed in /boot/EFI/.</p> + +<p><label>/etc/grub.d/40_custom</label></p> +<pre class="file"> +menuentry "EFI Shell" { + insmod part_gpt + insmod chain + insmod fat + set root='(hd4,gpt2)' + chainloader /EFI/Shell.efi +} +</pre> + +<p>Now regenerate your grub configuration file with <code class="language-plaintext highlighter-rouge">grub-update</code> (Debian-based) or <code class="language-plaintext highlighter-rouge">grub-mkconfig -o /boot/grub/grub.cfg</code> (Other).</p> + +<p>You’ll know if your shell is working if you see the following text on boot into the EFI shell:</p> + +<pre class="terminal"> +UEFI Interactive Shell v2.1 +EDK II +UEFI v2.4 (EDI II, 0x000100000) +Mapping table: + ... +Shell&gt; +</pre> + +<h2 id="running-hello-world">Running Hello World</h2> + +<p>When we run our <code class="language-plaintext highlighter-rouge">ls</code> command from earlier, remember we saw our <code class="language-plaintext highlighter-rouge">HelloWorld.efi</code> file. +Let’s move this file somewhere useful, like for me, <code class="language-plaintext highlighter-rouge">/boot</code>. +Then, once we’re in our UEFI shell we can run commands:</p> + +<pre class="terminal"> +Shell&gt; .\HelloWorld.efi +UEFI Hello World! +Shell&gt; +</pre> + +<p>And that… All that is how you set up a UEFI development environment.</p> + +<h2 id="conclusion">Conclusion</h2> + +<p>This took me a long time to figure out. +I needed to scrounge resources from around the internet, +and I had to look at my config files for hours to make sure that I hadn’t missed a step that I did without thinking. +I hope this will be useful to you and my future self.</p> + +<p>Happy UEFI hacking :)</p>I made this blog so I could remember how to do stuff that had instructions spread around the internet. So here is how I setup my environment for developing EFI applications.The “Quiz Your Friends” XSS Exploit2021-04-04T00:00:00-06:002021-04-04T00:00:00-06:00/2021/04/04/quiz-your-friends-xss<p>Note: I have alerted the administrators of this site multiple times about this vulnerability. +One email was sent many years ago, which is more than enough time for <a href="https://en.wikipedia.org/wiki/Responsible_disclosure">responsible disclosure</a>.</p> + +<p>Update: They have fixed the vulnerability as of the day of release for this article.</p> + +<h2 id="background">Background</h2> + +<p>In early 2014, when my “programming” skills consisted of editing web pages with inspect element, I was sent a link from an old friend in a town about 3 hours away. +This was a link to a quiz about them. +I had to answer as many questions right as I could about them and I got a score at the end based on my answers. +It seemed fun enough, so I went for it. +In the following weeks this quiz website became quite a trend amongst my friend group as we all started making quizes to see how well we all knew eachother.</p> + +<p>A few weeks into this trend, I was staying at a friends’ place and told him about this site, +so he goes and creates his own quiz and sends it to all his friends, group chats, Google Plus groups, et cetera.</p> + +<h2 id="hackerman">Hackerman</h2> + +<p>While filling in my friend’s survey I thought it would be +funny for them to know it is me without anyone else knowing. +We were young and had <code class="language-plaintext highlighter-rouge">Inspect Element</code>ed a few things together, +so it was a safe bet that an HTML joke would let them know.</p> + +<p>I decided to write my name like so: <code class="language-plaintext highlighter-rouge">&lt;b&gt;Steve&lt;/b&gt;</code>. +Steve is in reference to the <a href="https://minecraft.gamepedia.com/Player">main character</a> in the video game Minecraft.</p> + +<figure> + <img src="/assets/img/qyf-xss/2-bold.png" /> + <figcaption> + <p>Me typing in my name as <span class="mono">&lt;b&gt;Steve&lt;/b&gt;</span>.</p> + </figcaption> +</figure> + +<p>Now in theory this should have shown in in the leaderboard as: “&lt;b&gt;Steve&lt;/b&gt;” +However, to my horror and excitement, I saw this in the leaderboard:</p> + +<figure> + <img src="/assets/img/qyf-xss/3-steve-board.png" /> + <figcaption> + <p><span class="mono">&lt;b&gt;Steve&lt;/b&gt;</span> displaying in the leaderboard as bold text: <b>Steve</b></p> + </figcaption> +</figure> + +<p>The text “Steve” showed up <strong>in bold</strong> on the leaderboard. +This told me all I needed to know. +How did this happen? You might wonder.</p> + +<h3 id="server-side-validation">Server-Side Validation</h3> + +<p>Here is a great demonstration why you should do most of your validation on the server side. +As a user, I can edit any of the HTML, CSS, or Javascript your server serves to me.</p> + +<p>Quiz your friends uses the <code class="language-plaintext highlighter-rouge">maxlength=20</code> HTML attribute on the name input field. +Imagine trying to fit in a script tag doing anything useful with 20 characters! Don’t forget that includes the <code class="language-plaintext highlighter-rouge">&lt;script&gt;</code> tag. +That would leave 13 characters for Javascript. +Although I’m sure a genius would be able to <a href="https://code.golf/">code golf</a> that, I know I couldn’t.</p> + +<p>Now obviously I can edit any HTML that a server has sent to me. +If I open up my inspect element window, I can go ahead and change that <code class="language-plaintext highlighter-rouge">maxlength</code> attribute to anything I want. +Let’s change it to 100!</p> + +<figure> + <img src="/assets/img/qyf-xss/5-maxlength.png" alt="An image of the Quiz Your Friends name input field with inspect element. THe code reads: &lt;font class=&quot;style6&quot;&gt;&lt;input class=&quot;inputbutton&quot; name=&quot;takername&quot; type=&quot;text&quot; id=&quot;takername&quot; maxlength=&quot;20&quot; width=&quot;425&quot; placeholder=&quot;Your First Name&quot; style=&quot;text-align: center; text-decoration:inherit; font-size:38px;&quot; tabindex=&quot;-1&quot;&gt;&lt;/font&gt;" /> + <figcaption> + Manually changing the maxlength attribute. + </figcaption> +</figure> + +<p>In theory, there is a way that a site can stop people from just putting in their name of any length: server-side validation. +The server <em>could</em> check to see if the input is too long and reject it if it is. +The Quiz My Friends server has <em>no such checks in place</em>. +Therefore, I can send an almost arbitrary load to them. +Being able to send something potentially very large (more than a few megabytes) is a vulnerability of its own. +Imagine being able to send entire executable programs as your “name” in one of these quizzes?</p> + +<h2 id="javascript">Javascript</h2> + +<p>So I went on my merry way thinking about ways to use malicious javascript. +Then, I thought that might be mean, so I decided to warn users instead. +I filled in the name with a script tag and a call to <code class="language-plaintext highlighter-rouge">alert()</code> to warn the user about this site. +I edited the max-length attribute to allow me to type a long string like this:</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>&lt;script&gt;alert("Don't use this site. It is not secure!");&lt;/script&gt; +</code></pre></div></div> + +<p>Sure enough, I got a text from my friend saying: “Tait! I know this is you, why would you do that!” +A bit salty, but who wouldn’t be.</p> + +<h2 id="cross-site-scripting-xss">Cross-Site Scripting (XSS)</h2> + +<p>As my final act, I decided to use a cross-site script that I could edit and have it load with new changes at any time.</p> + +<p>I set this as my name:</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>&lt;script src="https://tait.tech/assets/js/hacked.js"&gt;&lt;/script&gt; +</code></pre></div></div> + +<p>This script pops up a warning, telling the user that the site is insecure and it is now redirecting to an article about the attack. +This script redirects to an <a href="https://tait.tech/2020/04/25/xss/">older post I made</a> about how XSS works.</p> + +<h2 id="conclusion">Conclusion</h2> + +<p>Watch out for sketchy websites that may be vulnerable to malicious or insecure sites which are ripe for abuse. +Always check that you are using an encrypted connection, HTTPS. +And if you see any messages warning you that a site is not secure and redirecting you to some random site… +Take their info with a grain of salt.</p> + +<p>Happy Hacking, literally :)</p>Note: I have alerted the administrators of this site multiple times about this vulnerability. One email was sent many years ago, which is more than enough time for responsible disclosure.Lichess Accessibility2021-01-31T00:00:00-07:002021-01-31T00:00:00-07:00/2021/01/31/lichess<p>I wanted to play chess with somebody who used a screen reader, without requiring a screen reader myself; +some sites, like QuintenC’s Playroom have a rather poor visual interface for anyone who would like the play the game visually. +<a href="https://lichess.org">Lichess</a> is an free and open-source website for chess players; +it bridges this gap by having two “modes” on the site: +standard mode and accessibility mode.</p> + +<h2 id="accessibility-mode">Accessibility Mode</h2> + +<p>Accessibility mode is far from perfect on lichess.org. +That said, the idea to separate the sites into different modes was a good call. +It stops the inevitable “this would work well for screen readers but cause visual issues” shenanigans, +or, vice-verse “this looks great but it might be weird with a screen reader”. +This way all the things which affect the visual interface are in one place, +and all things which affect the non-visual user interface (NVUI) are written in another.</p> + +<p>In my quest to play chess with visual and non-visual players with both having optimal experiences, I tried Lichess with my friend from <a href="https://melly.tech/">melly.tech</a>. +She pointed out that the method to interface with the board previously was rather poor. +This is because it required an “enter” at the end of each command and the commands tended to read out a row or column of a chessboard not just an individual square.</p> + +<p>For example, to list all pieces (or lack thereof) on the e file, I would type the command:</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>s e +</code></pre></div></div> + +<p>Although this seems good in theory, and it’s great when you need an entire file, there was no way to get only one square. +In addition, imagine typing to navigate around the board:</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>s e1 +s f1 +s e2 +</code></pre></div></div> + +<p>For the inexperienced player, it seems to be more convenient to bind some keys and have the user bounce to various buttons, which they can push to say “I want to move this piece”. +This is what I was told anyway. +So I want to work making a system so you could use the following basic keys:</p> + +<ul> + <li>left/right/up/down arrow: move on the board.</li> + <li>k/q/r/b/n/p: move to next piece represented by its character in chess notation.</li> + <li>shift + k/q/r/b/n/p: move back to the last piece represented by its character in chess notation.</li> + <li>click/enter/space: select piece to move.</li> + <li>click/enter/space again: move piece here.</li> + <li>m: show where I can move with this piece.</li> + <li>shift+m: show where I can capture with this piece.</li> + <li>1-8: move to rank 1-8; stay on same file.</li> + <li>shift + 1-8: move to file a-h; stay on same rank.</li> +</ul> + +<p>This gives a pretty solid basis for playing the game. +One caveat is after you have moved a pawn all the way to the farthest rank, only the destination tile will accept your promotion choice. +Therefore, all the other keys still work on other square, but if you are on the destination square of a promotion q/r/b/n will promote your piece, not jump you to the next/previous one.</p> + +<p>This pull request was merged earlier this month:</p> + +<h2 id="more-to-come">More To Come</h2> + +<p>Next thing I want to do is implement the analysis board. +Right now it is not accessible whatsoever.</p> + +<h2 id="help-me">Help Me</h2> + +<p>If you are a screen reader user or know about accessibility and want to help make Lichess an awesome chess site for sighted and unsighted players alike, +then send me an email at <a href="mailto:tait@tait.tech">tait@tait.tech</a> and I’ll BCC you once I start testing the analysis board.</p> + +<p>Happy hacking, y’all!</p>I wanted to play chess with somebody who used a screen reader, without requiring a screen reader myself; some sites, like QuintenC’s Playroom have a rather poor visual interface for anyone who would like the play the game visually. Lichess is an free and open-source website for chess players; it bridges this gap by having two “modes” on the site: standard mode and accessibility mode.How to Deploy Lichess’s Lila With Nginx2020-12-20T00:00:00-07:002020-12-20T00:00:00-07:00/2020/12/20/deploy-lichess<p>I was getting ready to have a public test of some changes I made to <a href="https://lichess.org">lichess.org</a>’s <a href="https://lichess.org/source">open source chess platform</a>. +In preperation, I got my Let’s Encrypt certificates and nginx configurations setup… +and it wouldn’t work. +Here are some tips for myself and future Lichess developers.</p> + +<h2 id="reasoning">Reasoning</h2> + +<p>My pull request involves accessibility. +It will extend Lichess’s NVUI (Non-Visual User Interface) to be more accessible to beginner level chess players. +At the time of writing this, Lichess’s NVUI only supports searching pieces by type, rank and file. +It does not support any kind of interactive board.</p> + +<p>I wanted to play chess with a friend of mine who uses a screen reader. +Even though Lichess does indeed have a separate rendering of the page for visually impaired users, +I have heard from a few people that it is not the best.</p> + +<p>I don’t use a screen reader myself, so I thought having a public latest changes deployed server would work better for testing. +It would certainly work better than getting some of my less computer literate friends to connect to me via VSCode/VPN and view my local repository.</p> + +<p>So here is how to deploy it:</p> + +<h2 id="setup-a-development-environment">Setup a development environment</h2> + +<p>This is described <a href="https://github.com/ornicar/lila/wiki/Lichess-Development-Onboarding">in Lichess’s documentation itself</a>. +I will not elaborate further as it is not necessary.</p> + +<h2 id="setup-nginx">Setup nginx</h2> + +<p>This is the part that stumps most people. +Getting a local development server usually works alright, but once you want to reverse proxy it for security and professionalism purposes, it get more interesting.</p> + +<p>Here is the relevant portion of my nginx configuration for lila:</p> + +<pre class="file"> +server_name chess.tait.tech; + +location / { + proxy_pass http://127.0.0.1:9663; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-NginX-Proxy true; + proxy_set_header X-Real-IP $remote_addr; +} +</pre> + +<p>This is the config for the lila-ws websocket subdomain:</p> + +<pre class="file"> +server_name ws.chess.tait.tech; + +location / { + proxy_pass http://127.0.0.1:9664; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; +} +</pre> + +<p>You will need to deploy these on two virtual hosts.</p> + +<h2 id="lila">Lila</h2> + +<p><a href="https://github.com/ornicar/lila/">Lila</a> is the name for the main chess server, we need to change a few settings. Here is my git diff for the <code class="language-plaintext highlighter-rouge">conf/base.conf</code> file:</p> + +<pre class="file"> +- domain = "localhost:9663" +- socket.domains = [ "localhost:9664" ] ++ domain = "chess.tait.tech" ++ socket.domains = [ "ws.chess.tait.tech" ] + asset.domain = ${net.domain} +- asset.base_url = "http://"${net.asset.domain} ++ asset.base_url = "https://"${net.asset.domain} + asset.minified = false +- base_url = "http://"${net.domain} ++ base_url = "https://"${net.domain} +</pre> + +<h3 id="lila-ws">Lila-ws</h3> + +<p><a href="https://github.com/ornicar/lila-ws/">Lila-ws</a> is the websocket component of Lila.</p> + +<p>The most common complaint amongst aspiring Lichess developers is websockets not working. +They constantly get these ‘101’ responses from the websocket, +and it also seems that the websocket returns instead of staying in the ‘pending’ state as it should be.</p> + +<p>Here is how to fix that (in diff format):</p> + +<pre class="file"> +-csrf.origin = "http://127.0.0.1:9000" ++csrf.origin = "https://chess.tait.tech" +</pre> + +<p>You need to tell lila-ws where the websocket requests will be coming from. This is how to do that.</p> + +<h2 id="conclusion">Conclusion</h2> + +<p>This is not a long article, but just some notes for future me and Lila developers.</p>I was getting ready to have a public test of some changes I made to lichess.org’s open source chess platform. In preperation, I got my Let’s Encrypt certificates and nginx configurations setup… and it wouldn’t work. Here are some tips for myself and future Lichess developers.Getting Pacaur Working on a Raspberry Pi 4 with Manjaro ARM or Arch Linux2020-12-01T00:00:00-07:002020-12-01T00:00:00-07:00/2020/12/01/pacaur-rpi<p>I recently installed Manjaro ARM (based on Arch Linux ARM) on a Raspberry Pi 4. +I used some standard commands to start to add the <code class="language-plaintext highlighter-rouge">pacaur</code> package so I can easily retrieve <a href="https://wiki.archlinux.org/index.php/Arch_User_Repository">AUR packages</a> without needing to do it manually. +Unfortunately, there is a small problem with compiling this on ARM.</p> + +<h2 id="always_inline">always_inline</h2> + +<p>To setup the install for <code class="language-plaintext highlighter-rouge">pacaur</code>, I first needed to download <a href="https://aur.archlinux.org/packages/auracle-git">auracle-git</a> AUR package manually. +I ran into an error when compiling this package.</p> + +<p>But first, my setup:</p> +<pre class="terminal"> +$ git clone https://aur.archlinux.org/auracle-git +$ cd auracle-git +$ makepkg -sri +</pre> + +<p>Around half way through compiling this project, I got this cryptic message telling me there was a “target specific option mismatch”…Whatever that means. +The full error is below, hopefully that helps my chances on the search engines.</p> + +<pre class="terminal"> +In file included from ../subprojects/abseil-cpp-20200225.2/absl/random/internal/randen_hwaes.cc:225: +/usr/lib/gcc/aarch64-unknown-linux-gnu/9.3.0/include/arm_neon.h: In function 'Vector128 {anonymous}::AesRound(const Vector128&amp;, const Vector128&amp;)': +/usr/lib/gcc/aarch64-unknown-linux-gnu/9.3.0/include/arm_neon.h:12452:1: error: inlining failed in call to always_inline 'uint8x16_t vaesmcq_u8(uint8x16_t)': target specific option mismatch +12452 | vaesmcq_u8 (uint8x16_t data) +</pre> + +<p>Luckily, there is a very easy fix for this. +The user redfish <a href="https://aur.archlinux.org/packages/auracle-git#comment-762117">helpfully pointed out</a> +on the <code class="language-plaintext highlighter-rouge">auracle-git</code> package page that you need to add a special make option to your <code class="language-plaintext highlighter-rouge">/etc/make.conf</code> file to make this work.</p> + +<p>His solution, as commented is like so:</p> + +<blockquote> + <p>If you get this error when building for ARM aarch64:</p> + + <p>(insert error message from before)</p> + + <p>Then check that in /etc/makepkg.conf CFLAGS and CXXFLAGS have the +crypto suffix in -march flag, like -march=armv8-a+crypto (the base identifier may very depending on your hardware)</p> +</blockquote> + +<p>Basically, there is a file on Linux: <code class="language-plaintext highlighter-rouge">/etc/makepkg.conf</code> which tells your computer how to compile <em>all</em> programs on the system. +By default the Manjaro ARM (RPi4) edition has the following relevant lines in <code class="language-plaintext highlighter-rouge">makepkg.conf</code>.</p> + +<pre class="file"> +CFLAGS="-march=armv8-a -O2 -pipe -fstack-protector-strong -fno-plt" +CXXFLAGS="-march=armv8-a -O2 -pipe -fstack-protector-strong -fno-plt" +</pre> + +<p>What Mr. redfish is telling us is that we must add ‘+crypto’ to the end of the -march compiler flag so that our compiler will know how to inline that pesky vaesmcq_u8 function.</p> + +<p>So in the end, your <code class="language-plaintext highlighter-rouge">makepkg.conf</code>’s relevant lines will look like so:</p> +<pre class="file"> +CFLAGS="-march=armv8-a+crypto -O2 -pipe -fstack-protector-strong -fno-plt" +CXXFLAGS="-march=armv8-a+crypto -O2 -pipe -fstack-protector-strong -fno-plt" +</pre> + +<h2 id="why">Why?</h2> + +<p>Redfish continues:</p> + +<blockquote> + <p>Build of abseil-cpp package works because it uses CMake which adds the correct -march flag regardless of makepkg.conf, whereas when abseil-cpp is build as a subproject within this package, it uses meson, which does not add the flag and thus fails with the above error.</p> +</blockquote> + +<p>In other words, one of the dependencies pulled in with auracle is not compiling without this special compiler flag enabled.</p> + +<h2 id="conclusion">Conclusion</h2> + +<p>Thanks to redfish for posting this solution to the forums! +Would’ve been quite the rabbit hole for me to figure out how to do that. +In fact, it is very likely I would have never figured that one out.</p> + +<p>After this issue is resolved, the installation of <code class="language-plaintext highlighter-rouge">pacaur</code> goes as expected. Nice and easy! +Pacuar will compile on any architecture so it’s smooth sailing from here.</p> + +<p>Happy hacking!</p>I recently installed Manjaro ARM (based on Arch Linux ARM) on a Raspberry Pi 4. I used some standard commands to start to add the pacaur package so I can easily retrieve AUR packages without needing to do it manually. Unfortunately, there is a small problem with compiling this on ARM.ZFS NAS Box, Part 22020-11-15T00:00:00-07:002020-11-15T00:00:00-07:00/2020/11/15/nas2<p>Back in <a href="/2020/04/12/nas1/">part one of my NAS project</a> I discussed how I wanted to set up my hardware. +Today, I set up the NAS (almost).</p> + +<p>There were some hiccup along the way, like learning that M.2 slots can disable some of your SATA ports or waiting a month for a host bus adapter to come in from China.</p> + +<h2 id="why-did-it-take-so-long">Why Did It Take So Long</h2> + +<p>So it turns out I was going to spend a lot more on this project than I originally anticipated. +I ended up getting a server machine instead of a sleek NAS box. +Here are some of the quick specs:</p> + +<ul> + <li>Standard ATX case by Thermaltake.</li> + <li>LSI 9211-8i.</li> + <li>The cheapest HDMI graphics card I could find on Kijiji.</li> + <li>6x 3TB Segate HDDs.</li> + <li>1x 250G Kingston SSD.</li> + <li>AMD Ryzen 5 3600.</li> + <li>MSI B450 Gaming Plus Max.</li> + <li>2x 8GB FlareX 3200Mhz RAM.</li> + <li>1x 16GB Kingston 3200Mhz RAM.</li> +</ul> + +<h2 id="zfs">ZFS</h2> + +<p>This is how I decided to configure my storage pools. +In hindsight, this was not the best choice for upgrading. +I may change it in the future to a 0+1 setup, but it works for now.</p> + +<p>I have 5x 3TB in a RAIDZ2 with one drive not attached for redundancy’s sake. +How does one setup a ZFS pool. Check this out:</p> + +<pre class="terminal"> +# zpool create poolname raidz2 \ +/dev/by-id/blahblahblah1 \ +/dev/by-id/blahblahblah2 \ +/dev/by-id/blahblahblah3 \ +/dev/by-id/blahblahblah4 \ +/dev/by-id/blahblahblah5 +</pre> + +<p>And zippidy-doo! We’ve got a ZFS pool! +We can check its status with <code class="language-plaintext highlighter-rouge">zpool status</code>.</p> + +<pre class="terminal"> +$ zfs status + pool: raid + state: ONLINE + scan: scrub in progress since Wed Nov 18 18:41:41 2020 + 1.84T scanned at 8.51G/s, 121G issued at 562M/s, 1.84T total + 0B repaired, 6.45% done, 0 days 00:53:25 to go +config: + + NAME STATE READ WRITE CKSUM + raid ONLINE 0 0 0 + raidz2-0 ONLINE 0 0 0 + ata-HGST_HUS724030ALA640_PN2234P8JTNMYY ONLINE 0 0 0 + ata-HGST_HUS724030ALA640_PN2234P8JVSXTY ONLINE 0 0 0 + ata-HGST_HUS724030ALA640_PN2234P8JXAS8Y ONLINE 0 0 0 + ata-HGST_HUS724030ALA640_PN2234P8JXBARY ONLINE 0 0 0 + ata-HGST_HUS724030ALA640_PN2234P8JXP77Y ONLINE 0 0 0 + +errors: No known data errors +</pre> + +<p>I had run a scrub right before this, so there’s some extra detail in that. +This is really fun! I will be doing more home storage projects soon. +Perhaps Raspberry Pi NAS using all 4 USB ports to load SATA drives on it. +Now that would be fun!</p> + +<h2 id="so-i-kinda-have-a-nas-now">So I Kinda Have A NAS Now…?</h2> + +<p>So right now I can only copy files with <code class="language-plaintext highlighter-rouge">rsync</code>, <code class="language-plaintext highlighter-rouge">scp</code> and moving data via a physical drive. +The one major disadvantage this has is speed.</p> + +<p>Due to this machine being connected directly outside my network and pulling DHCP like a normal router would, I need to send my data through the WAN connection to get my files to it. +This is rather unfortunate as my upload speed is capped at 20 megabits per second, despite my upload being in the 300+ range.</p> + +<p>Part 3 will involve a LAN card so I can connect both to the DHCP server of my ISP and my local router. +This way my transfer speeds should be in the range of 1 gigabit per second. +This will make my life much easier, at least on the local network.</p> + +<h2 id="fun-fact">Fun Fact!</h2> + +<p>Do not try to use the M.2 slot on a consumer motherboard where you are also using all the SATA ports. +On my consumer gaming motherboard, the SATA ports next to the M.2 slot became <em>disabled</em> when I attached the M.2 SSD. +I found this out form my motherboard documentation, which I read only after a week of thinking my motherboard itself was defective, and sending it in for repairs that did absolutely nothing.</p> + +<h2 id="thoughts">Thoughts</h2> + +<p>I like having all this space. I plan on using it up pretty fast, so I’m already looking at how to expand. +Hopefully that gives a decent overview of how I set up my drives.</p> + +<p>Happy hacking!</p>Back in part one of my NAS project I discussed how I wanted to set up my hardware. Today, I set up the NAS (almost). \ No newline at end of file diff --git a/_site/ideas/index.html b/_site/ideas/index.html new file mode 100644 index 0000000..f555c6f --- /dev/null +++ b/_site/ideas/index.html @@ -0,0 +1,136 @@ + + + + + Ideas | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    Ideas

    + +

    1. Accessible BIOS

    + +

    Update: +See my blog post with the guy who’s writing the new audio driver into EDK2.

    + +

    Some server motherboards include serial UART/I2C ports which can be used to manage a BIOS via serial. +If this is possible, would it be able to attach to a braille display via an intermediary like a Rockchip/Pi SBC or Arduino compatible chip using BRLTTY and serial input from the motherboard? +Maybe not as it appears to require a full Unicode terminal, which I have the suspicion that BRLTTY will not be able to automatically filter out the formatting characters.

    + +

    I found one paper referencing the (in)accessibility of BIOS, specifically UEFI BIOS from Brazil. +I have downloaded the paper and uploaded it here for reference. +PDF of “UEFI BIOS Accessibility for the Visually Impaired”.

    + +

    After emailing the authors of the paper, I found out that one of them, Rafael Machado, was able to get a song playing in UEFI as a part of his masters. +Here is a link to the Github Msc UEFI PreOS Accessibility; +he has links to YouTube videos where he is shown playing a song on an ASUS laptop with PCIe connected speakers: Song Playing in UEFI

    + +

    I have downloaded and played around with his Github project but to no avail. +Either I am not setting it up correctly, or I do not have the proper sound setup, but in any case no sound plays from either my laptop or desktop.

    + +

    This requires more research and investment to understand UEFI, HDA audio, what systems have it and how to work with words and other sounds.

    + +

    2. Terminal-oriented browser

    + +

    Use selenium to allow a cross-engine compatible terminal-browser with JS support. Yes, sure, it has all the bloat of the modern web as it uses the full code of Chrome/Firefox/Webkit—but at least it can be used in the terminal. +Guaranteed to be accessible.

    + +

    I’m thinking of similar key commands to Orca/NVDA but output is send to the terminal. Unsure of how to handle aria-live regions, but perhaps a queue could be used to print text. Unsure how to calculate delay as the user may be using a screen reader at different speeds and/or a braille display.

    + +

    Change backend on-the-fly with a page reload. So if a website doesn’t work with Webkit, load it in Firefox with a key command.

    + +

    Just an idea.

    + +

    3. Dead Simple Chess App

    + +

    I want to make a simple chess app which can connect to multiple backends +(i.e. Lichess, Chess.com, etc.) and then users can use one app to play many games. +This should be quite simple given how easy the lichess API is, and the chess.com API coming soon!

    + +
    +

    This is read-only data. You cannot send game-moves or other commands to Chess.com from this system. If you wish to send commands, you will be interested in the Interactive API releasing later this year.

    +
    + +

    4. Open-Source VPN Deployment System

    + +

    Help my business and others start their own.

    + +
      +
    • Update active servers with pings from said server. Encrypt with GPG to verify, send over HTTPS. Use sub-keys (?) so they may be revoked anytime. Use token to login server.
    • +
    • Ability to create new tokens with no priviledges; only to say “Hi I’m still here.”
    • +
    • All IP address changes need to be manually approved.
    • +
    • Status queries, must be logged in: POST /status/[id/] should give back info about: +
        +
      • Number of connections.
      • +
      • Level (paid, free, plus when I can find out how to get Netflix working).
      • +
      • Protocol (OpenVPN/Wireguard). Wireguard should be default.
      • +
      • Max throughput (i.e. 1Gb/sec, 100Mb/sec)
      • +
      • Current average throughput over last minute, 15 minutes, hour and day. (i.e. 15Mb/sec 12Mb/sec)
      • +
      +
    • +
    • Decide server: POST /new/[coutnry_code/] should send a response of a possible server to connect to (or even the full Wireguard file possibly)
    • +
    • Get server files: POST /download/[country_code/[number/]] return the wireguard config file for that region, or a zip file containing the group of them.
    • +
    • A client would be able to use a native wireguard client on linux (i.e. store in /etc/wireguard (or its default location).
    • +
    • A client would allow local options like blocking LAN, kill-switch support, and the ability to change your VPN region based on latest list of servers.
    • +
    • The list of servers will be updated manually with bt update.
    • +
    + +

    5. 3d printing of Google Maps/OpenStreetMaps data for the visually impaired.

    + +

    A larger project, to be sure, but one I think could be of interest. +Imagine being able to download some data from Google or OpenStrretMaps, +then put it into a program and have it generate a 3d map which can be printed. +Unsure what to do, as the braille overlay on top of the streets and important buildings, etc. needs to be of a uniform size (braille cannot be scaled) but the buildings, streets, and parks do need to be scaled in size.

    + +

    I think for beginning, forget the braille entirely and simply product a map. +This can be done in the STL file format or some intermediary if that is easier. +Roads will have a slight border on the side, +parks will have a diamond texture, +buildings will have slight rectangular borders (slightly wider than the roads), +paths will be a thin line, and the label for the path will need to extend the thin line into a (rounded) rectangle with text on it.

    + +

    Start with roads. +Get a road, get it to generate the correct shape. +Then add a border around the side. +Then, add 4 more roads and figure out how to intersect them.

    + +

    If it can be done on a display, it can be done in a file.

    + +

    Start with that. Wow what a daunting project!

    + +

    This is being worked on through the touch-mapper project. +They do not, however, have labels yet.

    + +
    +
    + +
    + + diff --git a/_site/index.html b/_site/index.html new file mode 100644 index 0000000..efc8a12 --- /dev/null +++ b/_site/index.html @@ -0,0 +1,124 @@ + + + + + Home | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    Tait Hoyem

    + +

    I have three goals in my software development career:

    + +
      +
    1. Strong adherence to the UNIX principles of software design.
    2. +
    3. Security, privacy, and anonymity of the internet.
    4. +
    5. Accessibility of technology to the visually impaired.
    6. +
    + +

    Some of my projects reflect these goals; +others are just fun along the way. +I have all of my code projects hosted on my Github.

    + +

    Projects

    +

    Here is some of my best work:

    +
      + +
    • +

      + Quote Retrieval System — Written for a local roofing business to securely distribute quotes. +

      +
    • + +
    • +

      + Yggdrasil Screen Reader — Some small pieces of code I've written have made it into a new screen reader for Linux, written in Rust. +

      +
    • + +
    • +

      + Lichess — A patch for Lichess.org which vastly extends support for screen reader users. The website has millions of active players. +

      +
    • + +
    • +

      + epub-with-pinyin — A program to add Pinyin above Chinese characters in .epub files to assist those learning Mandarin Chinese. +

      +
    • + +
    • +

      + Programming Tutorials For The Visually Impaired — Amateur-level production quality videos with all file buffers, written text, and shell commands read out by Emacspeak, an Emacs extention for the blind. +

      +
    • + +
    • +

      + lamegames — A little games website I made for some demonstrations of Django and websocket functionality. Very, very lame. Would not recommend. +

      +
    • + +
    • +

      + chess — A command-line chess game in C++. It is compileable on nearly any system. +

      +
    • + +
    • +

      + Emacspeak C Tutorials — C Tutorials for the Visually Impaired +

      +
    • + +
    • +

      + Simple Markdown Editor — A simple online (and offline) browser-based markdown editor that supports the automatic creation of MathML. +

      +
    • + +
    + +

    I also occasionally put some content on my blog

    + +

    Contact

    +
    +

    You can reach me via email at tait@tait.tech.

    +
    +

    If you use PGP, download my public key.

    + + +
    +
    + +
    + + diff --git a/_site/isaif/index.html b/_site/isaif/index.html new file mode 100644 index 0000000..d9bb727 --- /dev/null +++ b/_site/isaif/index.html @@ -0,0 +1,2435 @@ + + + + + The Unabomber Manifesto: Industrial Society and its Future | tait.tech + + + + +
    +
    +
    +

    The Unabomber Manifesto: Industrial Society and its Future

    +
    + by Theodore John Kaczynski +
    +
    +
    +

    Table Of Contents

    + +
      +
    1. Table Of Contents
    2. +
    3. Introduction
    4. +
    5. The Psychology of Modern Leftism
    6. +
    7. Feelings of Inferiority
    8. +
    9. Oversocialization
    10. +
    11. The Power Process
    12. +
    13. Surrogate Activities
    14. +
    15. Autonomy
    16. +
    17. Sources of Social Problems
    18. +
    19. Disruption of the Power Process in Modern Society
    20. +
    21. How Some People Adjust
    22. +
    23. The Motives of Scientists
    24. +
    25. The Nature of Freedom
    26. +
    27. Some Principles of History
    28. +
    29. Industrial-Technological Society Cannot Be Reformed
    30. +
    31. Restriction of Freedom is Unavoidable in Industrial Society
    32. +
    33. The ‘Bad’ Parts of Technology Cannot Be Separated from the ‘Good’ Parts
    34. +
    35. Technology is a More Powerful Social Force than the Aspiration for Freedom
    36. +
    37. Simpler Social Problems Have Proved Intractable
    38. +
    39. Revolution is Easier than Reform
    40. +
    41. Control of Human Behavior
    42. +
    43. Human Race at a Crossroads
    44. +
    45. Human Suffering
    46. +
    47. The Future
    48. +
    49. Strategy
    50. +
    51. Two Kinds of Technology
    52. +
    53. The Danger of Leftism
    54. +
    55. Final Note
    56. +
    57. Notes
    58. +
    59. My Footnotes
    60. +
    + +

    Introduction

    + +

    1. The Industrial Revolution and its consequences have been a disaster for the human race. They have greatly increased the +life-expectancy of those of us who live in “advanced” countries, but they have destabilized society, have made life unfulfilling, +have subjected human beings to indignities, have led to widespread psychological suffering (in the Third World to physical +suffering as well) and have inflicted severe damage on the natural world. The continued development of technology will +worsen the situation. It will certainly subject human being to greater indignities and inflict greater damage on the natural world, +it will probably lead to greater social disruption and psychological suffering, and it may lead to increased physical suffering +even in “advanced” countries. +

    + +

    2. The industrial-technological system may survive or it may break down. If it survives, it MAY eventually achieve a low level +of physical and psychological suffering, but only after passing through a long and very painful period of adjustment and only at +the cost of permanently reducing human beings and many other living organisms to engineered products and mere cogs in the +social machine. Furthermore, if the system survives, the consequences will be inevitable: There is no way of reforming or +modifying the system so as to prevent it from depriving people of dignity and autonomy. +

    + +

    3. If the system breaks down the consequences will still be very painful. But the bigger the system grows the more disastrous +the results of its breakdown will be, so if it is to break down it had best break down sooner rather than later. +

    + +

    4. We therefore advocate a revolution against the industrial system. This revolution may or may not make use of violence; it +may be sudden or it may be a relatively gradual process spanning a few decades. We can’t predict any of that. But we do +outline in a very general way the measures that those who hate the industrial system should take in order to prepare the way for +a revolution against that form of society. This is not to be a POLITICAL revolution. Its object will be to overthrow not +governments but the economic and technological basis of the present society. +

    + +

    5. In this article we give attention to only some of the negative developments that have grown out of the industrial-technological system. Other such developments we mention only briefly or ignore altogether. This does not mean that we +regard these other developments as unimportant. For practical reasons we have to confine our discussion to areas that have +received insufficient public attention or in which we have something new to say. For example, since there are well-developed +environmental and wilderness movements, we have written very little about environmental degradation or the destruction of +wild nature, even though we consider these to be highly important. +

    + +

    The Psychology of Modern Leftism

    + +

    6. Almost everyone will agree that we live in a deeply troubled society. One of the most widespread manifestations of the +craziness of our world is leftism, so a discussion of the psychology of leftism can serve as an introduction to the discussion of +the problems of modern society in general. +

    + +

    7. But what is leftism? During the first half of the 20th century leftism could have been practically identified with socialism. +Today the movement is fragmented and it is not clear who can properly be called a leftist. When we speak of leftists in this +article we have in mind mainly socialists, collectivists, “politically correct” types, feminists, gay and disability activists, animal +rights activists and the like. But not everyone who is associated with one of these movements is a leftist. What we are trying to +get at in discussing leftism is not so much movement or an ideology as a psychological type, or rather a collection of related +types. Thus, what we mean by “leftism” will emerge more clearly in the course of our discussion of leftist psychology. +(Also, see paragraphs 227-230.) +

    + +

    8. Even so, our conception of leftism will remain a good deal less clear than we would wish, but there doesn’t seem to be any +remedy for this. All we are trying to do here is indicate in a rough and approximate way the two psychological tendencies that +we believe are the main driving force of modern leftism. We by no means claim to be telling the WHOLE truth about leftist +psychology. Also, our discussion is meant to apply to modern leftism only. We leave open the question of the extent to which +our discussion could be applied to the leftists of the 19th and early 20th centuries. +

    + +

    9. The two psychological tendencies that underlie modern leftism we call “feelings of inferiority” and “oversocialization.” +Feelings of inferiority are characteristic of modern leftism as a whole, while oversocialization is characteristic only of a certain +segment of modern leftism; but this segment is highly influential. +

    + +

    Feelings of Inferiority

    + +

    10. By “feelings of inferiority” we mean not only inferiority feelings in the strict sense but a whole spectrum of related traits; +low self-esteem, feelings of powerlessness, depressive tendencies, defeatism, guilt, self-hatred, etc. We argue that modern +leftists tend to have some such feelings (possibly more or less repressed) and that these feelings are decisive in determining the +direction of modern leftism. +

    + +

    11. When someone interprets as derogatory almost anything that is said about him (or about groups with whom he identifies) +we conclude that he has inferiority feelings or low self-esteem. This tendency is pronounced among minority rights activists, +whether or not they belong to the minority groups whose rights they defend. They are hypersensitive about the words used to +designate minorities and about anything that is said concerning minorities. The terms “Negro,” “oriental,” “handicapped” or +“chick” for an African, an Asian, a disabled person or a woman originally had no derogatory connotation. “Broad” and “chick” +were merely the feminine equivalents of “guy,” “dude” or “fellow.” The negative connotations have been attached to these +terms by the activists themselves. Some animal rights activists have gone so far as to reject the word “pet” and insist on its +replacement by “animal companion.” Leftish anthropologists go to great lengths to avoid saying anything about primitive +peoples that could conceivably be interpreted as negative. They want to replace the word “primitive” by “nonliterate.” They +may seem almost paranoid about anything that might suggest that any primitive culture is inferior to ours. (We do not mean to +imply that primitive cultures ARE inferior to ours. We merely point out the hyper sensitivity of leftish anthropologists.) +

    + +

    12. Those who are most sensitive about “politically incorrect” terminology are not the average black ghetto-dweller, Asian +immigrant, abused woman or disabled person, but a minority of activists, many of whom do not even belong to any +“oppressed” group but come from privileged strata of society. Political correctness has its stronghold among university +professors, who have secure employment with comfortable salaries, and the majority of whom are heterosexual white males +from middle to upper-class families. +

    + +

    13. Many leftists have an intense identification with the problems of groups that have an image of being weak (women), +defeated (American Indians), repellent (homosexuals) or otherwise inferior. The leftists themselves feel that these groups are +inferior. They would never admit to themselves that they have such feelings, but it is precisely because they do see these +groups as inferior that they identify with their problems. (We do not mean to suggest that women, Indians, etc. ARE inferior; +we are only making a point about leftist psychology.) +

    + +

    14. Feminists are desperately anxious to prove that women are as strong and as capable as men. Clearly they are nagged by a +fear that women may NOT be as strong and as capable as men. +

    + +

    15. Leftists tend to hate anything that has an image of being strong, good and successful. They hate America, they hate +Western civilization, they hate white males, they hate rationality. The reasons that leftists give for hating the West, etc. clearly +do not correspond with their real motives. They SAY they hate the West because it is warlike, imperialistic, sexist, ethnocentric +and so forth, but where these same faults appear in socialist countries or in primitive cultures, the leftist finds excuses for them, +or at best he GRUDGINGLY admits that they exist; whereas he ENTHUSIASTICALLY points out (and often greatly +exaggerates) these faults where they appear in Western civilization. Thus it is clear that these faults are not the leftist’s real +motive for hating America and the West. He hates America and the West because they are strong and successful. +

    + +

    16. Words like “self-confidence,” “self-reliance,” “initiative,” “enterprise,” “optimism,” etc., play little role in the liberal and +leftist vocabulary. The leftist is anti-individualistic, pro-collectivist. He wants society to solve every one’s problems for them, +satisfy everyone’s needs for them, take care of them. He is not the sort of person who has an inner sense of confidence in his +ability to solve his own problems and satisfy his own needs. The leftist is antagonistic to the concept of competition because, +deep inside, he feels like a loser. +

    + +

    17. Art forms that appeal to modern leftish intellectuals tend to focus on sordidness, defeat and despair, or else they take an +orgiastic tone, throwing off rational control as if there were no hope of accomplishing anything through rational calculation and +all that was left was to immerse oneself in the sensations of the moment. +

    + +

    18. Modern leftish philosophers tend to dismiss reason, science, objective reality and to insist that everything is culturally +relative. It is true that one can ask serious questions about the foundations of scientific knowledge and about how, if at all, the +concept of objective reality can be defined. But it is obvious that modern leftish philosophers are not simply cool-headed +logicians systematically analyzing the foundations of knowledge. They are deeply involved emotionally in their attack on truth +and reality. They attack these concepts because of their own psychological needs. For one thing, their attack is an outlet for +hostility, and, to the extent that it is successful, it satisfies the drive for power. More importantly, the leftist hates science and +rationality because they classify certain beliefs as true (i.e., successful, superior) and other beliefs as false (i.e., failed, inferior). +The leftist’s feelings of inferiority run so deep that he cannot tolerate any classification of some things as successful or superior +and other things as failed or inferior. This also underlies the rejection by many leftists of the concept of mental illness and of +the utility of IQ tests. Leftists are antagonistic to genetic explanations of human abilities or behavior because such explanations +tend to make some persons appear superior or inferior to others. Leftists prefer to give society the credit or blame for an +individual’s ability or lack of it. Thus if a person is “inferior” it is not his fault, but society’s, because he has not been brought +up properly. +

    + +

    19. The leftist is not typically the kind of person whose feelings of inferiority make him a braggart, an egotist, a bully, a self-promoter, a ruthless competitor. This kind of person has not wholly lost faith in himself. He has a deficit in his sense of power and self-worth, but he can still conceive of himself as having the capacity to be strong, and his efforts to make himself strong +produce his unpleasant behavior. [1] But the leftist is too far gone for that. His feelings of inferiority are so ingrained that he +cannot conceive of himself as individually strong and valuable. Hence the collectivism of the leftist. He can feel strong only as +a member of a large organization or a mass movement with which he identifies himself. +

    + +

    20. Notice the masochistic tendency of leftist tactics. Leftists protest by lying down in front of vehicles, they intentionally +provoke police or racists to abuse them, etc. These tactics may often be effective, but many leftists use them not as a means to +an end but because they PREFER masochistic tactics. Self-hatred is a leftist trait. +

    + +

    21. Leftists may claim that their activism is motivated by compassion or by moral principles, and moral principle does play a +role for the leftist of the oversocialized type. But compassion and moral principle cannot be the main motives for leftist +activism. Hostility is too prominent a component of leftist behavior; so is the drive for power. Moreover, much leftist behavior +is not rationally calculated to be of benefit to the people whom the leftists claim to be trying to help. For example, if one +believes that affirmative action is good for black people, does it make sense to demand affirmative action in hostile or +dogmatic terms? Obviously it would be more productive to take a diplomatic and conciliatory approach that would make at +least verbal and symbolic concessions to white people who think that affirmative action discriminates against them. But leftist +activists do not take such an approach because it would not satisfy their emotional needs. Helping black people is not their real +goal. Instead, race problems serve as an excuse for them to express their own hostility and frustrated need for power. In doing +so they actually harm black people, because the activists’ hostile attitude toward the white majority tends to intensify race +hatred. +

    + +

    22. If our society had no social problems at all, the leftists would have to INVENT problems in order to provide themselves +with an excuse for making a fuss. +

    + +

    23. We emphasize that the foregoing does not pretend to be an accurate description of everyone who might be considered a +leftist. It is only a rough indication of a general tendency of leftism. +

    + +

    Oversocialization

    + +

    24. Psychologists use the term “socialization” to designate the process by which children are trained to think and act as society +demands. A person is said to be well socialized if he believes in and obeys the moral code of his society and fits in well as a +functioning +part of that society. It may seem senseless to say that many leftists are over-socialized, since the leftist is +perceived as a rebel. Nevertheless, the position can be defended. Many leftists are not such rebels as they seem. +

    + +

    25. The moral code of our society is so demanding that no one can think, feel and act in a completely moral way. For example, +we are not supposed to hate anyone, yet almost everyone hates somebody at some time or other, whether he admits it to himself +or not. Some people are so highly socialized that the attempt to think, feel and act morally imposes a severe burden on them. In +order to avoid feelings of guilt, they continually have to deceive themselves about their own motives and find moral +explanations for feelings and actions that in reality have a non-moral origin. We use the term “oversocialized” to describe such +people. [2] +

    + +

    26. Oversocialization can lead to low self-esteem, a sense of powerlessness, defeatism, guilt, etc. One of the most important +means by which our society socializes children is by making them feel ashamed of behavior or speech that is contrary to +society’s expectations. If this is overdone, or if a particular child is especially susceptible to such feelings, he ends by feeling +ashamed of HIMSELF. Moreover the thought and the behavior of the oversocialized person are more restricted by society’s +expectations than are those of the lightly socialized person. The majority of people engage in a significant amount of naughty +behavior. They lie, they commit petty thefts, they break traffic laws, they goof off at work, they hate someone, they say spiteful +things or they use some underhanded trick to get ahead of the other guy. The oversocialized person cannot do these things, or if +he does do them he generates in himself a sense of shame and self-hatred. The oversocialized person cannot even experience, +without guilt, thoughts or feelings that are contrary to the accepted morality; he cannot think “unclean” thoughts. And +socialization is not just a matter of morality; we are socialized to conform to many norms of behavior that do not fall under the +heading of morality. Thus the oversocialized person is kept on a psychological leash and spends his life running on rails that +society has laid down for him. In many oversocialized people this results in a sense of constraint and powerlessness that can be +a severe hardship. We suggest that oversocialization is among the more serious cruelties that human being inflict on one +another. +

    + +

    27. We argue that a very important and influential segment of the modern left is oversocialized and that their oversocialization +is of great importance in determining the direction of modern leftism. Leftists of the oversocialized type tend to be intellectuals +or members of the upper-middle class. Notice that university intellectuals [3] constitute the most highly socialized segment of +our society and also the most left-wing segment. +

    + +

    28. The leftist of the oversocialized type tries to get off his psychological leash and assert his autonomy by rebelling. But +usually he is not strong enough to rebel against the most basic values of society. Generally speaking, the goals of today’s +leftists are NOT in conflict with the accepted morality. On the contrary, the left takes an accepted moral principle, adopts it as +its own, and then accuses mainstream society of violating that principle. Examples: racial equality, equality of the sexes, +helping poor people, peace as opposed to war, nonviolence generally, freedom of expression, kindness to animals. More +fundamentally, the duty of the individual to serve society and the duty of society to take care of the individual. All these have +been deeply rooted values of our society (or at least of its middle and upper classes) [4] for a long time. These values are +explicitly or implicitly expressed or presupposed in most of the material presented to us by the mainstream communications +media and the educational system. Leftists, especially those of the oversocialized type, usually do not rebel against these +principles but justify their hostility to society by claiming (with some degree of truth) that society is not living up to these +principles. +

    + +

    29. Here is an illustration of the way in which the oversocialized leftist shows his real attachment to the conventional attitudes +of our society while pretending to be in rebellion against it. Many leftists push for affirmative action, for moving black people +into high-prestige jobs, for improved education in black schools and more money for such schools; the way of life of the black +“underclass” they regard as a social disgrace. They want to integrate the black man into the system, make him a business +executive, a lawyer, a scientist just like upper-middle-class white people. The leftists will reply that the last thing they want is +to make the black man into a copy of the white man; instead, they want to preserve African American culture. But in what does +this preservation of African American culture consist? It can hardly consist in anything more than eating black-style food, listening to black-style music, wearing black-style clothing and going to a black-style church or mosque. In other words, it can +express itself only in superficial matters. In all ESSENTIAL respects most leftists of the oversocialized type want to make the +black man conform to white, middle-class ideals. They want to make him study technical subjects, become an executive or a +scientist, spend his life climbing the status ladder to prove that black people are as good as white. They want to make black +fathers “responsible,” they want black gangs to become nonviolent, etc. But these are exactly the values of the industrial-technological system. The system couldn’t care less what kind of music a man listens to, what kind of clothes he wears or what +religion he believes in as long as he studies in school, holds a respectable job, climbs the status ladder, is a “responsible” +parent, is nonviolent and so forth. In effect, however much he may deny it, the oversocialized leftist wants to integrate the +black man into the system and make him adopt its values. +

    + +

    30. We certainly do not claim that leftists, even of the oversocialized type, NEVER rebel against the fundamental values of our +society. Clearly they sometimes do. Some oversocialized leftists have gone so far as to rebel against one of modern society’s +most important principles by engaging in physical violence. By their own account, violence is for them a form of “liberation.” +In other words, by committing violence they break through the psychological restraints that have been trained into them. +Because they are oversocialized these restraints have been more confining for them than for others; hence their need to break +free of them. But they usually justify their rebellion in terms of mainstream values. If they engage in violence they claim to be +fighting against racism or the like. +

    + +

    31. We realize that many objections could be raised to the foregoing thumbnail sketch of leftist psychology. The real situation +is complex, and anything like a complete description of it would take several volumes even if the necessary data were +available. We claim only to have indicated very roughly the two most important tendencies in the psychology of modern +leftism. +

    + +

    32. The problems of the leftist are indicative of the problems of our society as a whole. Low self-esteem, depressive tendencies +and defeatism are not restricted to the left. Though they are especially noticeable in the left, they are widespread in our society. +And today’s society tries to socialize us to a greater extent than any previous society. We are even told by experts how to eat, +how to exercise, how to make love, how to raise our kids and so forth. +

    + +

    The Power Process

    + +

    33. Human beings have a need (probably based in biology) for something that we will call the “power process.” This is closely +related to the need for power (which is widely recognized) but is not quite the same thing. The power process has four +elements. The three most clear-cut of these we call goal, effort and attainment of goal. (Everyone needs to have goals whose +attainment requires effort, and needs to succeed in attaining at least some of his goals.) The fourth element is more difficult to +define and may not be necessary for everyone. We call it autonomy and will discuss it later (paragraphs 42-44). +

    + +

    34. Consider the hypothetical case of a man who can have anything he wants just by wishing for it. Such a man has power, but +he will develop serious psychological problems. At first he will have a lot of fun, but by and by he will become acutely bored +and demoralized. Eventually he may become clinically depressed. History shows that leisured aristocracies tend to become +decadent. This is not true of fighting aristocracies that have to struggle to maintain their power. But leisured, secure +aristocracies that have no need to exert themselves usually become bored, hedonistic and demoralized, even though they have +power. This shows that power is not enough. One must have goals toward which to exercise one’s power. +

    + +

    35. Everyone has goals; if nothing else, to obtain the physical necessities of life: food, water and whatever clothing and shelter +are made necessary by the climate. But the leisured aristocrat obtains these things without effort. Hence his boredom and demoralization. +

    + +

    36. Non-attainment of important goals results in death if the goals are physical necessities, and in frustration if non-attainment of +the goals is compatible with survival. Consistent failure to attain goals throughout life results in defeatism, low self-esteem or depression. +

    + +

    37. Thus, in order to avoid serious psychological problems, a human being needs goals whose attainment requires effort, and +he must have a reasonable rate of success in attaining his goals. +

    + +

    Surrogate Activities

    + +

    38. But not every leisured aristocrat becomes bored and demoralized. For example, the emperor Hirohito, instead of sinking +into decadent hedonism, devoted himself to marine biology, a field in which he became distinguished. When people do not +have to exert themselves to satisfy their physical needs they often set up artificial goals for themselves. In many cases they then +pursue these goals with the same energy and emotional involvement that they otherwise would have put into the search for +physical necessities. Thus the aristocrats of the Roman Empire had their literary pretensions; many European aristocrats a few +centuries ago invested tremendous time and energy in hunting, though they certainly didn’t need the meat; other aristocracies +have competed for status through elaborate displays of wealth; and a few aristocrats, like Hirohito, have turned to science. +

    + +

    39. We use the term “surrogate activity” to designate an activity that is directed toward an artificial goal that people set up for +themselves merely in order to have some goal to work toward, or let us say, merely for the sake of the “fulfillment” that they +get from pursuing the goal. Here is a rule of thumb for the identification of surrogate activities. Given a person who devotes +much time and energy to the pursuit of goal X, ask yourself this: If he had to devote most of his time and energy to satisfying +his biological needs, and if that effort required him to use his physical and mental faculties in a varied and interesting way, +would he feel seriously deprived because he did not attain goal X? If the answer is no, then the person’s pursuit of goal X is a +surrogate activity. Hirohito’s studies in marine biology clearly constituted a surrogate activity, since it is pretty certain that if +Hirohito had had to spend his time working at interesting non-scientific tasks in order to obtain the necessities of life, he would +not have felt deprived because he didn’t know all about the anatomy and life-cycles of marine animals. On the other hand the +pursuit of sex and love (for example) is not a surrogate activity, because most people, even if their existence were otherwise +satisfactory, would feel deprived if they passed their lives without ever having a relationship with a member of the opposite +sex. (But pursuit of an excessive amount of sex, more than one really needs, can be a surrogate activity.) +

    + +

    40. In modern industrial society only minimal effort is necessary to satisfy one’s physical needs. It is enough to go through a +training program to acquire some petty technical skill, then come to work on time and exert the very modest effort needed to +hold a job. The only requirements are a moderate amount of intelligence and, most of all, simple OBEDIENCE. If one has +those, society takes care of one from cradle to grave. (Yes, there is an underclass that cannot take the physical necessities for +granted, but we are speaking here of mainstream society.) Thus it is not surprising that modern society is full of surrogate +activities. These include scientific work, athletic achievement, humanitarian work, artistic and literary creation, climbing the +corporate ladder, acquisition of money and material goods far beyond the point at which they cease to give any additional +physical satisfaction, and social activism when it addresses issues that are not important for the activist personally, as in the +case of white activists who work for the rights of nonwhite minorities. These are not always PURE surrogate activities, since +for many people they may be motivated in part by needs other than the need to have some goal to pursue. Scientific work may +be motivated in part by a drive for prestige, artistic creation by a need to express feelings, militant social activism by hostility. +But for most people who pursue them, these activities are in large part surrogate activities. For example, the majority of +scientists will probably agree that the “fulfillment” they get from their work is more important than the money and prestige +they earn. +

    + +

    41. For many if not most people, surrogate activities are less satisfying than the pursuit of real goals (that is, goals that people +would want to attain even if their need for the power process were already fulfilled). One indication of this is the fact that, in +many or most cases, people who are deeply involved in surrogate activities are never satisfied, never at rest. Thus the moneymaker constantly strives for more and more wealth. The scientist no sooner solves one problem than he moves on to the next. +The long-distance runner drives himself to run always farther and faster. Many people who pursue surrogate activities will say +that they get far more fulfillment from these activities than they do from the “mundane” business of satisfying their biological +needs, but that is because in our society the effort needed to satisfy the biological needs has been reduced to triviality. More +importantly, in our society people do not satisfy their biological needs AUTONOMOUSLY but by functioning as parts of an +immense social machine. In contrast, people generally have a great deal of autonomy in pursuing their surrogate activities. +

    + +

    Autonomy

    + +

    42. Autonomy as a part of the power process may not be necessary for every individual. But most people need a greater or +lesser degree of autonomy in working toward their goals. Their efforts must be undertaken on their own initiative and must be +under their own direction and control. Yet most people do not have to exert this initiative, direction and control as single +individuals. It is usually enough to act as a member of a SMALL group. Thus if half a dozen people discuss a goal among +themselves and make a successful joint effort to attain that goal, their need for the power process will be served. But if they +work under rigid orders handed down from above that leave them no room for autonomous decision and initiative, then their need for the power process will not be served. The same is true when decisions are made on a collective basis if the group +making the collective decision is so large that the role of each individual is insignificant. [5] +

    + +

    43. It is true that some individuals seem to have little need for autonomy. Either their drive for power is weak or they satisfy it +by identifying themselves with some powerful organization to which they belong. And then there are unthinking, animal types +who seem to be satisfied with a purely physical sense of power (the good combat soldier, who gets his sense of power by +developing fighting skills that he is quite content to use in blind obedience to his superiors). +

    + +

    44. But for most people it is through the power process having a goal, making an AUTONOMOUS effort and attaining the goal +-- that self-esteem, self-confidence and a sense of power are acquired. When one does not have adequate opportunity to go +through the power process the consequences are (depending on the individual and on the way the power process is disrupted) +boredom, demoralization, low self-esteem, inferiority feelings, defeatism, depression, anxiety, guilt, frustration, hostility, +spouse or child abuse, insatiable hedonism, abnormal sexual behavior, sleep disorders, eating disorders, etc. [6] +

    + +

    Sources of Social Problems

    + +

    45. Any of the foregoing symptoms can occur in any society, but in modern industrial society they are present on a massive +scale. We aren’t the first to mention that the world today seems to be going crazy. This sort of thing is not normal for human +societies. There is good reason to believe that primitive man suffered from less stress and frustration and was better satisfied +with his way of life than modern man is. It is true that not all was sweetness and light in primitive societies. Abuse of women +was common among the Australian aborigines, transsexuality was fairly common among some of the American Indian tribes. +But it does appear that GENERALLY SPEAKING the kinds of problems that we have listed in the preceding paragraph were +far less common among primitive peoples than they are in modern society. +

    + +

    46. We attribute the social and psychological problems of modern society to the fact that that society requires people to live +under conditions radically different from those under which the human race evolved and to behave in ways that conflict with +the patterns of behavior that the human race developed while living under the earlier conditions. It is clear from what we have +already written that we consider lack of opportunity to properly experience the power process as the most important of the +abnormal conditions to which modern society subjects people. But it is not the only one. Before dealing with disruption of the +power process as a source of social problems we will discuss some of the other sources. +

    + +

    47. Among the abnormal conditions present in modern industrial society are excessive density of population, isolation of man +from nature, excessive rapidity of social change and the breakdown of natural small-scale communities such as the extended +family, the village or the tribe. +

    + +

    48. It is well known that crowding increases stress and aggression. The degree of crowding that exists today and the isolation +of man from nature are consequences of technological progress. All pre-industrial societies were predominantly rural. The +Industrial Revolution vastly increased the size of cities and the proportion of the population that lives in them, and modern +agricultural technology has made it possible for the Earth to support a far denser population than it ever did before. (Also, +technology exacerbates the effects of crowding because it puts increased disruptive powers in people’s hands. For example, a +variety of noise-making devices: power mowers, radios, motorcycles, etc. If the use of these devices is unrestricted, people +who want peace and quiet are frustrated by the noise. If their use is restricted, people who use the devices are frustrated by the +regulations. But if these machines had never been invented there would have been no conflict and no frustration generated by +them.) +

    + +

    49. For primitive societies the natural world (which usually changes only slowly) provided a stable framework and therefore a +sense of security. In the modern world it is human society that dominates nature rather than the other way around, and modern +society changes very rapidly owing to technological change. Thus there is no stable framework. +

    + +

    50. The conservatives are fools: They whine about the decay of traditional values, yet they enthusiastically support +technological progress and economic growth. Apparently it never occurs to them that you can’t make rapid, drastic changes in +the technology and the economy of a society without causing rapid changes in all other aspects of the society as well, and that +such rapid changes inevitably break down traditional values. +

    + +

    51. The breakdown of traditional values to some extent implies the breakdown of the bonds that hold together traditional small-scale social groups. The disintegration of small-scale social groups is also promoted by the fact that modern conditions often require or tempt individuals to move to new locations, separating themselves from their communities. Beyond that, a +technological society HAS TO weaken family ties and local communities if it is to function efficiently. In modern society an +individual’s loyalty must be first to the system and only secondarily to a small-scale community, because if the internal loyalties +of small-scale communities were stronger than loyalty to the system, such communities would pursue their own advantage at the expense of the system. +

    + +

    52. Suppose that a public official or a corporation executive appoints his cousin, his friend or his co-religionist to a position +rather than appointing the person best qualified for the job. He has permitted personal loyalty to supersede his loyalty to the +system, and that is “nepotism” or “discrimination,” both of which are terrible sins in modern society. Would-be industrial +societies that have done a poor job of subordinating personal or local loyalties to loyalty to the system are usually very +inefficient. (Look at Latin America.) Thus an advanced industrial society can tolerate only those small-scale communities that +are emasculated, tamed and made into tools of the system. [7] +

    + +

    53. Crowding, rapid change and the breakdown of communities have been widely recognized as sources of social problems. +But we do not believe they are enough to account for the extent of the problems that are seen today. +

    + +

    54. A few pre-industrial cities were very large and crowded, yet their inhabitants do not seem to have suffered from +psychological problems to the same extent as modern man. In America today there still are uncrowded rural areas, and we find +there the same problems as in urban areas, though the problems tend to be less acute in the rural areas. Thus crowding does not +seem to be the decisive factor. +

    + +

    55. On the growing edge of the American frontier during the 19th century, the mobility of the population probably broke down +extended families and small-scale social groups to at least the same extent as these are broken down today. In fact, many +nuclear families lived by choice in such isolation, having no neighbors within several miles, that they belonged to no +community at all, yet they do not seem to have developed problems as a result. +

    + +

    56. Furthermore, change in American frontier society was very rapid and deep. A man might be born and raised in a log cabin, +outside the reach of law and order and fed largely on wild meat; and by the time he arrived at old age he might be working at a +regular job and living in an ordered community with effective law enforcement. This was a deeper change than that which +typically occurs in the life of a modern individual, yet it does not seem to have led to psychological problems. In fact, 19th +century American society had an optimistic and self-confident tone, quite unlike that of today’s society. [8] +

    + +

    57. The difference, we argue, is that modern man has the sense (largely justified) that change is IMPOSED on him, whereas the +19th century frontiersman had the sense (also largely justified) that he created change himself, by his own choice. Thus a +pioneer settled on a piece of land of his own choosing and made it into a farm through his own effort. In those days an entire +county might have only a couple of hundred inhabitants and was a far more isolated and autonomous entity than a modern +county is. Hence the pioneer farmer participated as a member of a relatively small group in the creation of a new, ordered +community. One may well question whether the creation of this community was an improvement, but at any rate it satisfied the +pioneer’s need for the power process. +

    + +

    58. It would be possible to give other examples of societies in which there has been rapid change and/or lack of close +community ties without the kind of massive behavioral aberration that is seen in today’s industrial society. We contend that the +most important cause of social and psychological problems in modern society is the fact that people have insufficient +opportunity to go through the power process in a normal way. We don’t mean to say that modern society is the only one in +which the power process has been disrupted. Probably most if not all civilized societies have interfered with the power process +to a greater or lesser extent. But in modern industrial society the problem has become particularly acute. Leftism, at least in its +recent (mid-to-late 20th century) form, is in part a symptom of deprivation with respect to the power process. +

    + +

    Disruption of the Power Process in Modern Society

    + +

    59. We divide human drives into three groups: (1) those drives that can be satisfied with minimal effort; (2) those that can be +satisfied but only at the cost of serious effort; (3) those that cannot be adequately satisfied no matter how much effort one +makes. The power process is the process of satisfying the drives of the second group. The more drives there are in the third +group, the more there is frustration, anger, eventually defeatism, depression, etc. +

    + +

    60. In modern industrial society natural human drives tend to be pushed into the first and third groups, and the second group +tends to consist increasingly of artificially created drives. +

    + +

    61. In primitive societies, physical necessities generally fall into group 2: They can be obtained, but only at the cost of serious +effort. But modern society tends to guaranty the physical necessities to everyone [9] in exchange for only minimal effort, hence +physical needs are pushed into group 1. (There may be disagreement about whether the effort needed to hold a job is +“minimal”; but usually, in lower to middle-level jobs, whatever effort is required is merely that of OBEDIENCE. You sit or +stand where you are told to sit or stand and do what you are told to do in the way you are told to do it. Seldom do you have to +exert yourself seriously, and in any case you have hardly any autonomy in work, so that the need for the power process is not +well served.) +

    + +

    62. Social needs, such as sex, love and status, often remain in group 2 in modern society, depending on the situation of the +individual. [10] But, except for people who have a particularly strong drive for status, the effort required to fulfill the social +drives is insufficient to satisfy adequately the need for the power process. +

    + +

    63. So certain artificial needs have been created that fall into group 2, hence serve the need for the power process. Advertising +and marketing techniques have been developed that make many people feel they need things that their grandparents never +desired or even dreamed of. It requires serious effort to earn enough money to satisfy these artificial needs, hence they fall into +group 2. (But see paragraphs 80-82.) Modern man must satisfy his need for the power process largely through pursuit of the +artificial needs created by the advertising and marketing industry [11], and through surrogate activities. +

    + +

    64. It seems that for many people, maybe the majority, these artificial forms of the power process are insufficient. A theme that +appears repeatedly in the writings of the social critics of the second half of the 20th century is the sense of purposelessness that +afflicts many people in modern society. (This purposelessness is often called by other names such as “anomie” or “middle class vacuity.”) We suggest that the so-called “identity crisis” is actually a search for a sense of purpose, often for commitment +to a suitable surrogate activity. It may be that existentialism is in large part a response to the purposelessness of modern life. +[12] Very widespread in modern society is the search for “fulfillment.” But we think that for the majority of people an activity +whose main goal is fulfillment (that is, a surrogate activity) does not bring completely satisfactory fulfillment. In other words, +it does not fully satisfy the need for the power process. (See paragraph 41.) That need can be fully satisfied only through +activities that have some external goal, such as physical necessities, sex, love, status, revenge, etc. +

    + +

    65. Moreover, where goals are pursued through earning money, climbing the status ladder or functioning as part of the system +in some other way, most people are not in a position to pursue their goals AUTONOMOUSLY. Most workers are someone +else’s employee and, as we pointed out in paragraph 61, must spend their days doing what they are told to do in the way they +are told to do it. Even people who are in business for themselves have only limited autonomy. It is a chronic complaint of +small-business persons and entrepreneurs that their hands are tied by excessive government regulation. Some of these +regulations are doubtless unnecessary, but for the most part government regulations are essential and inevitable parts of our +extremely complex society. A large portion of small business today operates on the franchise system. It was reported in the +Wall Street Journal a few years ago that many of the franchise-granting companies require applicants for franchises to take a +personality test that is designed to EXCLUDE those who have creativity and initiative, because such persons are not +sufficiently docile to go along obediently with the franchise system. This excludes from small business many of the people who +most need autonomy. +

    + +

    66. Today people live more by virtue of what the system does FOR them or TO them than by virtue of what they do for +themselves. And what they do for themselves is done more and more along channels laid down by the system. Opportunities +tend to be those that the system provides, the opportunities must be exploited in accord with rules and regulations [13], and +techniques prescribed by experts must be followed if there is to be a chance of success. +

    + +

    67. Thus the power process is disrupted in our society through a deficiency of real goals and a deficiency of autonomy in the +pursuit of goals. But it is also disrupted because of those human drives that fall into group 3: the drives that one cannot +adequately satisfy no matter how much effort one makes. One of these drives is the need for security. Our lives depend on +decisions made by other people; we have no control over these decisions and usually we do not even know the people who +make them. +

    “We live in a world in which relatively few people – maybe 500 or 1,000 make the important decisions”
    — Philip +B. Heymann of Harvard Law School, quoted by Anthony Lewis, New York Times, April 21, 1995 .
    Our lives depend on +whether safety standards at a nuclear power plant are properly maintained; on how much pesticide is allowed to get into our +food or how much pollution into our air; on how skillful (or incompetent) our doctor is; whether we lose or get a job may +depend on decisions made by government economists or corporation executives; and so forth. Most individuals are not in a +position to secure themselves against these threats to more than a very limited extent. The individual’s search for security is +therefore frustrated, which leads to a sense of powerlessness. +

    + +

    68. It may be objected that primitive man is physically less secure than modern man, as is shown by his shorter life expectancy; +hence modern man suffers from less, not more than the amount of insecurity that is normal for human beings. But +psychological security does not closely correspond with physical security. What makes us FEEL secure is not so much +objective security as a sense of confidence in our ability to take care of ourselves. Primitive man, threatened by a fierce animal +or by hunger, can fight in self-defense or travel in search of food. He has no certainty of success in these efforts, but he is by no +means helpless against the things that threaten him. The modern individual on the other hand is threatened by many things +against which he is helpless: nuclear accidents, carcinogens in food, environmental pollution, war, increasing taxes, invasion of +his privacy by large organizations, nationwide social or economic phenomena that may disrupt his way of life. +

    + +

    69. It is true that primitive man is powerless against some of the things that threaten him; disease for example. But he can +accept the risk of disease stoically. It is part of the nature of things, it is no one’s fault, unless it is the fault of some imaginary, +impersonal demon. But threats to the modern individual tend to be MAN-MADE. They are not the results of chance but are +IMPOSED on him by other persons whose decisions he, as an individual, is unable to influence. Consequently he feels +frustrated, humiliated and angry. +

    + +

    70. Thus primitive man for the most part has his security in his own hands (either as an individual or as a member of a SMALL +group) whereas the security of modern man is in the hands of persons or organizations that are too remote or too large for him +to be able personally to influence them. So modern man’s drive for security tends to fall into groups 1 and 3; in some areas +(food, shelter etc.) his security is assured at the cost of only trivial effort, whereas in other areas he CANNOT attain security. +(The foregoing greatly simplifies the real situation, but it does indicate in a rough, general way how the condition of modern +man differs from that of primitive man.) +

    + +

    71. People have many transitory drives or impulses that are necessarily frustrated in modern life, hence fall into group 3. One +may become angry, but modern society cannot permit fighting. In many situations it does not even permit verbal aggression. +When going somewhere one may be in a hurry, or one may be in a mood to travel slowly, but one generally has no choice but +to move with the flow of traffic and obey the traffic signals. One may want to do one’s work in a different way, but usually one +can work only according to the rules laid down by one’s employer. In many other ways as well, modern man is strapped down +by a network of rules and regulations (explicit or implicit) that frustrate many of his impulses and thus interfere with the power +process. Most of these regulations cannot be dispensed with, because they are necessary for the functioning of industrial +society. +

    + +

    72. Modern society is in certain respects extremely permissive. In matters that are irrelevant to the functioning of the system +we can generally do what we please. We can believe in any religion (as long as it does not encourage behavior that is +dangerous to the system). We can go to bed with anyone we like (as long as we practice “safe sex”). We can do anything we +like as long as it is UNIMPORTANT. But in all IMPORTANT matters the system tends increasingly to regulate our behavior. +

    + +

    73. Behavior is regulated not only through explicit rules and not only by the government. Control is often exercised through +indirect coercion or through psychological pressure or manipulation, and by organizations other than the government, or by the +system as a whole. Most large organizations use some form of propaganda [14] to manipulate public attitudes or behavior. +Propaganda is not limited to “commercials” and advertisements, and sometimes it is not even consciously intended as +propaganda by the people who make it. For instance, the content of entertainment programming is a powerful form of +propaganda. An example of indirect coercion: There is no law that says we have to go to work every day and follow our +employer’s orders. Legally there is nothing to prevent us from going to live in the wild like primitive people or from going into +business for ourselves. But in practice there is very little wild country left, and there is room in the economy for only a limited +number of small business owners. Hence most of us can survive only as someone else’s employee. +

    + +

    74. We suggest that modern man’s obsession with longevity, and with maintaining physical vigor and sexual attractiveness to +an advanced age, is a symptom of unfulfillment resulting from deprivation with respect to the power process. The “mid-life +crisis” also is such a symptom. So is the lack of interest in having children that is fairly common in modern society but almost +unheard-of in primitive societies. +

    + +

    75. In primitive societies life is a succession of stages. The needs and purposes of one stage having been fulfilled, there is no +particular reluctance about passing on to the next stage. A young man goes through the power process by becoming a hunter, +hunting not for sport or for fulfillment but to get meat that is necessary for food. (In young women the process is more +complex, with greater emphasis on social power; we won’t discuss that here.) This phase having been successfully passed +through, the young man has no reluctance about settling down to the responsibilities of raising a family. (In contrast, some +modern people indefinitely postpone having children because they are too busy seeking some kind of “fulfillment.” We suggest +that the fulfillment they need is adequate experience of the power process – with real goals instead of the artificial goals of surrogate activities.) Again, having successfully raised his children, going through the power process by providing them with +the physical necessities, the primitive man feels that his work is done and he is prepared to accept old age (if he survives that +long) and death. Many modern people, on the other hand, are disturbed by the prospect of physical deterioration and death, as is +shown by the amount of effort they expend trying to maintain their physical condition, appearance and health. We argue that +this is due to unfulfillment resulting from the fact that they have never put their physical powers to any practical use, have +never gone through the power process using their bodies in a serious way. It is not the primitive man, who has used his body +daily for practical purposes, who fears the deterioration of age, but the modern man, who has never had a practical use for his +body beyond walking from his car to his house. It is the man whose need for the power process has been satisfied during his +life who is best prepared to accept the end of that life. +

    + +

    76. In response to the arguments of this section someone will say, “Society must find a way to give people the opportunity to +go through the power process.” This won't work for those who need autonomy in the power process. For such people the value of the opportunity is destroyed by the very fact that society gives it to +them. What they need is to find or make their own opportunities. As long as the system GIVES them their opportunities it still +has them on a leash. To attain autonomy they must get off that leash. +

    + +

    How Some People Adjust

    + +

    77. Not everyone in industrial-technological society suffers from psychological problems. Some people even profess to be +quite satisfied with society as it is. We now discuss some of the reasons why people differ so greatly in their response to +modern society. +

    + +

    78. First, there doubtless are innate differences in the strength of the drive for power. Individuals with a weak drive for power may +have relatively little need to go through the power process, or at least relatively little need for autonomy in the power process. +These are docile types who would have been happy as plantation darkies in the Old South. (We don’t mean to sneer at the +“plantation darkies” of the Old South. To their credit, most of the slaves were NOT content with their servitude. We do sneer at +people who ARE content with servitude.) +

    + +

    79. Some people may have some exceptional drive, in pursuing which they satisfy their need for the power process. For +example, those who have an unusually strong drive for social status may spend their whole lives climbing the status ladder +without ever getting bored with that game. +

    + +

    80. People vary in their susceptibility to advertising and marketing techniques. Some are so susceptible that, even if they make +a great deal of money, they cannot satisfy their constant craving for the shiny new toys that the marketing industry dangles +before their eyes. So they always feel hard-pressed financially even if their income is large, and their cravings are frustrated. +

    + +

    81. Some people have low susceptibility to advertising and marketing techniques. These are the people who aren’t interested in +money. Material acquisition does not serve their need for the power process. +

    + +

    82. People who have medium susceptibility to advertising and marketing techniques are able to earn enough money to satisfy +their craving for goods and services, but only at the cost of serious effort (putting in overtime, taking a second job, earning +promotions, etc.). Thus material acquisition serves their need for the power process. But it does not necessarily follow that their +need is fully satisfied. They may have insufficient autonomy in the power process (their work may consist of following orders) +and some of their drives may be frustrated (e.g., security, aggression). (We are guilty of oversimplification in paragraphs 80-82 +because we have assumed that the desire for material acquisition is entirely a creation of the advertising and marketing +industry. Of course it’s not that simple. [11] +

    + +

    83. Some people partly satisfy their need for power by identifying themselves with a powerful organization or mass movement. +An individual lacking goals or power joins a movement or an organization, adopts its goals as his own, then works toward +those goals. When some of the goals are attained, the individual, even though his personal efforts have played only an +insignificant part in the attainment of the goals, feels (through his identification with the movement or organization) as if he +had gone through the power process. This phenomenon was exploited by the fascists, Nazis and communists. Our society uses it +too, though less crudely. Example: Manuel Noriega was an irritant to the U.S. (goal: punish Noriega). The U.S. invaded +Panama (effort) and punished Noriega (attainment of goal); the U.S. went through the power process and many +Americans, because of their identification with the U.S., experienced the power process vicariously. Hence the widespread +public approval of the Panama invasion; it gave people a sense of power. [15] We see the same phenomenon in armies, +corporations, political parties, humanitarian organizations, religious or ideological movements. In particular, leftist movements tend to attract people who are seeking to satisfy their need for power. But for most people identification with a large +organization or a mass movement does not fully satisfy the need for power. +

    + +

    84. Another way in which people satisfy their need for the power process is through surrogate activities. As we explained in +paragraphs 38-40, a surrogate activity is an activity that is directed toward an artificial goal that the individual pursues for the +sake of the “fulfillment” that he gets from pursuing the goal, not because he needs to attain the goal itself. For instance, there is +no practical motive for building enormous muscles, hitting a little ball into a hole or acquiring a complete series of postage +stamps. Yet many people in our society devote themselves with passion to bodybuilding, golf or stamp-collecting. Some people +are more “other-directed” than others, and therefore will more readily attach importance to a surrogate activity simply because +the people around them treat it as important or because society tells them it is important. That is why some people get very +serious about essentially trivial activities such as sports, or bridge, or chess, or arcane scholarly pursuits, whereas others who +are more clear-sighted never see these things as anything but the surrogate activities that they are, and consequently never +attach enough importance to them to satisfy their need for the power process in that way. It only remains to point out that in +many cases a person’s way of earning a living is also a surrogate activity. Not a PURE surrogate activity, since part of the +motive for the activity is to gain the physical necessities and (for some people) social status and the luxuries that advertising +makes them want. But many people put into their work far more effort than is necessary to earn whatever money and status +they require, and this extra effort constitutes a surrogate activity. This extra effort, together with the emotional investment that +accompanies it, is one of the most potent forces acting toward the continual development and perfecting of the system, with +negative consequences for individual freedom (see paragraph 131). Especially, for the most creative scientists and engineers, +work tends to be largely a surrogate activity. This point is so important that it deserves a separate discussion, which we shall +give in a moment (paragraphs 87-92). +

    + +

    85. In this section we have explained how many people in modern society do satisfy their need for the power process to a +greater or lesser extent. But we think that for the majority of people the need for the power process is not fully satisfied. In the +first place, those who have an insatiable drive for status, or who get firmly “hooked” on a surrogate activity, or who identify +strongly enough with a movement or organization to satisfy their need for power in that way, are exceptional personalities. +Others are not fully satisfied with surrogate activities or by identification with an organization (see paragraphs 41, 64). In the +second place, too much control is imposed by the system through explicit regulation or through socialization, which results in a +deficiency of autonomy, and in frustration due to the impossibility of attaining certain goals and the necessity of restraining too +many impulses. +

    + +

    86. But even if most people in industrial-technological society were well satisfied, we would still be opposed to that form +of society, because (among other reasons) we consider it demeaning to fulfill one’s need for the power process through +surrogate activities or through identification with an organization, rather than through pursuit of real goals. +

    + +

    The Motives of Scientists

    + +

    87. Science and technology provide the most important examples of surrogate activities. Some scientists claim that they are +motivated by “curiosity” or by a desire to “benefit humanity.” But it is easy to see that neither of these can be the principal +motive of most scientists. As for “curiosity,” that notion is simply absurd. Most scientists work on highly specialized problems +that are not the object of any normal curiosity. For example, is an astronomer, a mathematician or an entomologist curious +about the properties of isopropyltrimethylmethane? Of course not. Only a chemist is curious about such a thing, and he is +curious about it only because chemistry is his surrogate activity. Is the chemist curious about the appropriate classification of a +new species of beetle? No. That question is of interest only to the entomologist, and he is interested in it only because +entomology is his surrogate activity. If the chemist and the entomologist had to exert themselves seriously to obtain the +physical necessities, and if that effort exercised their abilities in an interesting way but in some nonscientific pursuit, then they +wouldn’t give a damn about isopropyltrimethylmethane or the classification of beetles. Suppose that lack of funds for +postgraduate education had led the chemist to become an insurance broker instead of a chemist. In that case he would have +been very interested in insurance matters but would have cared nothing about isopropyltrimethylmethane. In any case it is not +normal to put into the satisfaction of mere curiosity the amount of time and effort that scientists put into their work. The +“curiosity” explanation for the scientists’ motive just doesn’t stand up. +

    + +

    88. The “benefit of humanity” explanation doesn’t work any better. Some scientific work has no conceivable relation to the +welfare of the human race most of archaeology or comparative linguistics for example. Some other areas of science present +obviously dangerous possibilities. Yet scientists in these areas are just as enthusiastic about their work as those who develop +vaccines or study air pollution. Consider the case of Dr. Edward Teller, who had an obvious emotional involvement in promoting nuclear power plants. Did this involvement stem from a desire to benefit humanity? If so, then why didn’t Dr. Teller +get emotional about other “humanitarian” causes? If he was such a humanitarian then why did he help to develop the H-bomb? +As with many other scientific achievements, it is very much open to question whether nuclear power plants actually do benefit +humanity. Does the cheap electricity outweigh the accumulating waste and the risk of accidents? Dr. Teller saw only one side +of the question. Clearly his emotional involvement with nuclear power arose not from a desire to “benefit humanity” but from a +personal fulfillment he got from his work and from seeing it put to practical use. +

    + +

    89. The same is true of scientists generally. With possible rare exceptions, their motive is neither curiosity nor a desire to +benefit humanity but the need to go through the power process: to have a goal (a scientific problem to solve), to make an effort +(research) and to attain the goal (solution of the problem.) Science is a surrogate activity because scientists work mainly for the +fulfillment they get out of the work itself. +

    + +

    90. Of course, it’s not that simple. Other motives do play a role for many scientists. Money and status for example. Some +scientists may be persons of the type who have an insatiable drive for status (see paragraph 79) and this may provide much of +the motivation for their work. No doubt the majority of scientists, like the majority of the general population, are more or less +susceptible to advertising and marketing techniques and need money to satisfy their craving for goods and services. Thus +science is not a PURE surrogate activity. But it is in large part a surrogate activity. +

    + +

    91. Also, science and technology constitute a power mass movement, and many scientists gratify their need for power through +identification with this mass movement (see paragraph 83). +

    + +

    92. Thus science marches on blindly, without regard to the real welfare of the human race or to any other standard, obedient +only to the psychological needs of the scientists and of the government officials and corporation executives who provide the +funds for research. +

    + +

    The Nature of Freedom

    + +

    93. We are going to argue that industrial-technological society cannot be reformed in such a way as to prevent it from +progressively narrowing the sphere of human freedom. But, because “freedom” is a word that can be interpreted in many ways, +we must first make clear what kind of freedom we are concerned with. +

    + +

    94. By “freedom” we mean the opportunity to go through the power process, with real goals not the artificial goals of surrogate +activities, and without interference, manipulation or supervision from anyone, especially from any large organization. Freedom +means being in control (either as an individual or as a member of a SMALL group) of the life-and-death issues of one’s +existence; food, clothing, shelter and defense against whatever threats there may be in one’s environment. Freedom means +having power; not the power to control other people but the power to control the circumstances of one’s own life. One does not +have freedom if anyone else (especially a large organization) has power over one, no matter how benevolently, tolerantly and +permissively that power may be exercised. It is important not to confuse freedom with mere permissiveness (see paragraph 72). +

    + +

    95. It is said that we live in a free society because we have a certain number of constitutionally guaranteed rights. But these are +not as important as they seem. The degree of personal freedom that exists in a society is determined more by the economic and +technological structure of the society than by its laws or its form of government. [16] Most of the Indian nations of New +England were monarchies, and many of the cities of the Italian Renaissance were controlled by dictators. But in reading about +these societies one gets the impression that they allowed far more personal freedom than our society does. In part this was +because they lacked efficient mechanisms for enforcing the ruler’s will: There were no modern, well-organized police forces, +no rapid long-distance communications, no surveillance cameras, no dossiers of information about the lives of average citizens. +Hence it was relatively easy to evade control. +

    + +

    96. As for our constitutional rights, consider for example that of freedom of the press. We certainly don’t mean to knock that +right; it is very important tool for limiting concentration of political power and for keeping those who do have political power +in line by publicly exposing any misbehavior on their part. But freedom of the press is of very little use to the average citizen +as an individual. The mass media are mostly under the control of large organizations that are integrated into the system. +Anyone who has a little money can have something printed, or can distribute it on the Internet or in some such way, but what +he has to say will be swamped by the vast volume of material put out by the media, hence it will have no practical effect. To +make an impression on society with words is therefore almost impossible for most individuals and small groups. Take us +for example. If we (FC)[A1] had never done anything violent and had submitted the present writings to a publisher, they probably would not have been accepted. If they had been accepted and published, they probably would not have attracted many readers, +because it’s more fun to watch the entertainment put out by the media than to read a sober essay. Even if these writings had had +many readers, most of these readers would soon have forgotten what they had read as their minds were flooded by the mass of +material to which the media expose them. In order to get our message before the public with some chance of making a lasting +impression, we’ve had to kill people. +

    + +

    97. Constitutional rights are useful up to a point, but they do not serve to guarantee much more than what might be called the +bourgeois conception of freedom. According to the bourgeois conception, a “free” man is essentially an element of a social +machine and has only a certain set of prescribed and delimited freedoms; freedoms that are designed to serve the needs of the +social machine more than those of the individual. Thus the bourgeois’s “free” man has economic freedom because that +promotes growth and progress; he has freedom of the press because public criticism restrains misbehavior by political leaders; +he has a right to a fair trial because imprisonment at the whim of the powerful would be bad for the system. This was clearly +the attitude of Simon Bolivar. To him, people deserved liberty only if they used it to promote progress (progress as conceived +by the bourgeois). Other bourgeois thinkers have taken a similar view of freedom as a mere means to collective ends. Chester +C. Tan, “Chinese Political Thought in the Twentieth Century,” page 202, explains the philosophy of the Kuomintang leader Hu +Han-min: “An individual is granted rights because he is a member of society and his community life requires such rights. By +community Hu meant the whole society of the nation.” And on page 259 Tan states that according to Carsum Chang (Chang +Chun-mai, head of the State Socialist Party in China) freedom had to be used in the interest of the state and of the people as a +whole. But what kind of freedom does one have if one can use it only as someone else prescribes? FC’s conception of freedom +is not that of Bolivar, Hu, Chang or other bourgeois theorists. The trouble with such theorists is that they have made the +development and application of social theories their surrogate activity. Consequently the theories are designed to serve the +needs of the theorists more than the needs of any people who may be unlucky enough to live in a society on which the theories +are imposed. +

    + +

    98. One more point to be made in this section: It should not be assumed that a person has enough freedom just because he +SAYS he has enough. Freedom is restricted in part by psychological controls of which people are unconscious, and moreover +many people’s ideas of what constitutes freedom are governed more by social convention than by their real needs. For +example, it’s likely that many leftists of the oversocialized type would say that most people, including themselves, are +socialized too little rather than too much, yet the oversocialized leftist pays a heavy psychological price for his high level of +socialization. +

    + +

    Some Principles of History

    + +

    99. Think of history as being the sum of two components: an erratic component that consists of unpredictable events that +follow no discernible pattern, and a regular component that consists of long-term historical trends. Here we are concerned with +the long-term trends. +

    + +

    100. FIRST PRINCIPLE. If a SMALL change is made that affects a long-term historical trend, then the effect of that change +will almost always be transitory – the trend will soon revert to its original state. (Example: A reform movement designed to +clean up political corruption in a society rarely has more than a short-term effect; sooner or later the reformers relax and +corruption creeps back in. The level of political corruption in a given society tends to remain constant, or to change only slowly +with the evolution of the society. Normally, a political cleanup will be permanent only if accompanied by widespread social +changes; a SMALL change in the society won’t be enough.) If a small change in a long-term historical trend appears to be +permanent, it is only because the change acts in the direction in which the trend is already moving, so that the trend is not +altered by only pushed a step ahead. +

    + +

    101. The first principle is almost a tautology. If a trend were not stable with respect to small changes, it would wander at +random rather than following a definite direction; in other words it would not be a long-term trend at all. +

    + +

    102. SECOND PRINCIPLE. If a change is made that is sufficiently large to alter permanently a long-term historical trend, then +it will alter the society as a whole. In other words, a society is a system in which all parts are interrelated, and you can’t +permanently change any important part without changing all other parts as well. +

    + +

    103. THIRD PRINCIPLE. If a change is made that is large enough to alter permanently a long-term trend, then the +consequences for the society as a whole cannot be predicted in advance. (Unless various other societies have passed through the same change and have all experienced the same consequences, in which case one can predict on empirical grounds that +another society that passes through the same change will be like to experience similar consequences.) +

    + +

    104. FOURTH PRINCIPLE. A new kind of society cannot be designed on paper. That is, you cannot plan out a new form of +society in advance, then set it up and expect it to function as it was designed to do. +

    + +

    105. The third and fourth principles result from the complexity of human societies. A change in human behavior will affect the +economy of a society and its physical environment; the economy will affect the environment and vice versa, and the changes in +the economy and the environment will affect human behavior in complex, unpredictable ways; and so forth. The network of +causes and effects is far too complex to be untangled and understood. +

    + +

    106. FIFTH PRINCIPLE. People do not consciously and rationally choose the form of their society. Societies develop through +processes of social evolution that are not under rational human control. +

    + +

    107. The fifth principle is a consequence of the other four. +

    + +

    108. To illustrate: By the first principle, generally speaking an attempt at social reform either acts in the direction in which the +society is developing anyway (so that it merely accelerates a change that would have occurred in any case) or else it has only a +transitory effect, so that the society soon slips back into its old groove. To make a lasting change in the direction of +development of any important aspect of a society, reform is insufficient and revolution is required. (A revolution does not +necessarily involve an armed uprising or the overthrow of a government.) By the second principle, a revolution never changes +only one aspect of a society, it changes the whole society; and by the third principle changes occur that were never expected or +desired by the revolutionaries. By the fourth principle, when revolutionaries or utopians set up a new kind of society, it never +works out as planned. +

    + +

    109. The American Revolution does not provide a counterexample. The American “Revolution” was not a revolution in our +sense of the word, but a war of independence followed by a rather far-reaching political reform. The Founding Fathers did not +change the direction of development of American society, nor did they aspire to do so. They only freed the development of +American society from the retarding effect of British rule. Their political reform did not change any basic trend, but only +pushed American political culture along its natural direction of development. British society, of which American society was +an offshoot, had been moving for a long time in the direction of representative democracy. And prior to the War of +Independence the Americans were already practicing a significant degree of representative democracy in the colonial +assemblies. The political system established by the Constitution was modeled on the British system and on the colonial +assemblies. With major alteration, to be sure -- there is no doubt that the Founding Fathers took a very important step. But it +was a step along the road that English-speaking world was already traveling. The proof is that Britain and all of its colonies +that were populated predominantly by people of British descent ended up with systems of representative democracy essentially +similar to that of the United States. If the Founding Fathers had lost their nerve and declined to sign the Declaration of +Independence, our way of life today would not have been significantly different. Maybe we would have had somewhat closer +ties to Britain, and would have had a Parliament and Prime Minister instead of a Congress and President. No big deal. Thus the +American Revolution provides not a counterexample to our principles but a good illustration of them. +

    + +

    110. Still, one has to use common sense in applying the principles. They are expressed in imprecise language that allows +latitude for interpretation, and exceptions to them can be found. So we present these principles not as inviolable laws but as +rules of thumb, or guides to thinking, that may provide a partial antidote to naive ideas about the future of society. The +principles should be borne constantly in mind, and whenever one reaches a conclusion that conflicts with them one should +carefully reexamine one’s thinking and retain the conclusion only if one has good, solid reasons for doing so. +

    + +

    Industrial-Technological Society Cannot Be Reformed

    + +

    111. The foregoing principles help to show how hopelessly difficult it would be to reform the industrial system in such a way +as to prevent it from progressively narrowing our sphere of freedom. There has been a consistent tendency, going back at least +to the Industrial Revolution for technology to strengthen the system at a high cost in individual freedom and local autonomy. +Hence any change designed to protect freedom from technology would be contrary to a fundamental trend in the development +of our society. Consequently, such a change either would be a transitory one – soon swamped by the tide of history – or, if +large enough to be permanent would alter the nature of our whole society. This by the first and second principles. Moreover, +since society would be altered in a way that could not be predicted in advance (third principle) there would be great risk. +Changes large enough to make a lasting difference in favor of freedom would not be initiated because it would be realized that +they would gravely disrupt the system. So any attempts at reform would be too timid to be effective. Even if changes large +enough to make a lasting difference were initiated, they would be retracted when their disruptive effects became apparent. +Thus, permanent changes in favor of freedom could be brought about only by persons prepared to accept radical, dangerous +and unpredictable alteration of the entire system. In other words by revolutionaries, not reformers. +

    + +

    112. People anxious to rescue freedom without sacrificing the supposed benefits of technology will suggest naive schemes for +some new form of society that would reconcile freedom with technology. Apart from the fact that people who make such +suggestions seldom propose any practical means by which the new form of society could be set up in the first place, it follows +from the fourth principle that even if the new form of society could be once established, it either would collapse or would give +results very different from those expected. +

    + +

    113. So even on very general grounds it seems highly improbable that any way of changing society could be found that would +reconcile freedom with modern technology. In the next few sections we will give more specific reasons for concluding that +freedom and technological progress are incompatible. +

    + +

    Restriction of Freedom is Unavoidable in Industrial Society

    + +

    114. As explained in paragraphs 65-67, 70-73, modern man is strapped down by a network of rules and regulations, and his +fate depends on the actions of persons remote from him whose decisions he cannot influence. This is not accidental or a result +of the arbitrariness of arrogant bureaucrats. It is necessary and inevitable in any technologically advanced society. The system +HAS TO regulate human behavior closely in order to function. At work people have to do what they are told to do, when they are told to do it, and in the way they are told to do it, otherwise +production would be thrown into chaos. Bureaucracies HAVE TO be run according to rigid rules. To allow any substantial +personal discretion to lower-level bureaucrats would disrupt the system and lead to charges of unfairness due to differences in +the way individual bureaucrats exercised their discretion. It is true that some restrictions on our freedom could be eliminated, +but GENERALLY SPEAKING the regulation of our lives by large organizations is necessary for the functioning of industrial-technological society. The result is a sense of powerlessness on the part of the average person. It may be, however, that formal +regulations will tend increasingly to be replaced by psychological tools that make us want to do what the system requires of us. +(Propaganda [14], educational techniques, “mental health” programs, etc.) +

    + +

    115. The system HAS TO force people to behave in ways that are increasingly remote from the natural pattern of human +behavior. For example, the system needs scientists, mathematicians and engineers. It can’t function without them. So heavy +pressure is put on children to excel in these fields. It isn’t natural for an adolescent human being to spend the bulk of his time +sitting at a desk absorbed in study. A normal adolescent wants to spend his time in active contact with the real world. Among +primitive peoples the things that children are trained to do tend to be in reasonable harmony with natural human impulses. +Among the American Indians, for example, boys were trained in active outdoor pursuits – just the sort of thing that boys like. +But in our society children are pushed into studying technical subjects, which most do grudgingly. +

    + +

    116. Because of the constant pressure that the system exerts to modify human behavior, there is a gradual increase in the number of people who cannot or will not adjust to society's requirements: welfare leeches, youth-gang members, cultists, anti-government rebels, radical environmentalist saboteurs, dropouts and resisters of various kinds.

    + +

    117. In any technologically advanced society the individual’s fate MUST depend on decisions that he personally cannot +influence to any great extent. A technological society cannot be broken down into small, autonomous communities, because +production depends on the cooperation of very large numbers of people and machines. Such a society MUST be highly organized and decisions HAVE TO be made that affect very large numbers of people. When a decision affects, say, a million people, then +each of the affected individuals has, on the average, only a one-millionth share in making the decision. What usually happens +in practice is that decisions are made by public officials or corporation executives, or by technical specialists, but even when +the public votes on a decision the number of voters ordinarily is too large for the vote of any one individual to be significant. +[17] Thus most individuals are unable to influence measurably the major decisions that affect their lives. There is no +conceivable way to remedy this in a technologically advanced society. The system tries to “solve” this problem by using +propaganda to make people WANT the decisions that have been made for them, but even if this “solution” were completely +successful in making people feel better, it would be demeaning. +

    + +

    118. Conservatives and some others advocate more “local autonomy.” Local communities once did have autonomy, but such +autonomy becomes less and less possible as local communities become more enmeshed with and dependent on large-scale +systems like public utilities, computer networks, highway systems, the mass communications media, the modern health care +system. Also operating against autonomy is the fact that technology applied in one location often affects people at other locations far way. +Thus pesticide or chemical use near a creek may contaminate the water supply hundreds of miles +downstream, and the greenhouse effect affects the whole world. +

    + +

    119. The system does not and cannot exist to satisfy human needs. Instead, it is human behavior that has to be modified to fit +the needs of the system. This has nothing to do with the political or social ideology that may pretend to guide the technological +system. It is not the fault of capitalism, and it is not the fault of socialism. +It is the fault of technology, because the system is guided not by ideology but by technical necessity. [18] Of course the +system does satisfy many human needs, but generally speaking it does this only to the extend that it is to the advantage of the +system to do it. It is the needs of the system that are paramount, not those of the human being. For example, the system +provides people with food because the system couldn’t function if everyone starved; it attends to people’s psychological needs +whenever it can CONVENIENTLY do so, because it couldn’t function if too many people became depressed or rebellious. But +the system, for good, solid, practical reasons, must exert constant pressure on people to mold their behavior to the needs of the +system. Too much waste accumulating? The government, the media, the educational system, environmentalists, everyone +inundates us with a mass of propaganda about recycling. Need more technical personnel? A chorus of voices exhorts kids to +study science. No one stops to ask whether it is inhumane to force adolescents to spend the bulk of their time studying subjects +most of them hate. When skilled workers are put out of a job by technical advances and have to undergo “retraining,” no one +asks whether it is humiliating for them to be pushed around in this way. It is simply taken for granted that everyone must bow +to technical necessity. +And for good reason: If human needs were put before technical necessity there would be economic +problems, unemployment, shortages or worse. The concept of “mental health” in our society is defined largely by the extent to +which an individual behaves in accord with the needs of the system and does so without showing signs of stress. +

    + +

    120. Efforts to make room for a sense of purpose and for autonomy within the system are no better than a joke. For example, +one company, instead of having each of its employees assemble only one section of a catalogue, had each assemble a whole +catalogue, and this was supposed to give them a sense of purpose and achievement. Some companies have tried to give their +employees more autonomy in their work, but for practical reasons this usually can be done only to a very limited extent, and in +any case employees are never given autonomy as to ultimate goals -- their “autonomous” efforts can never be directed toward +goals that they select personally, but only toward their employer’s goals, such as the survival and growth of the company. Any +company would soon go out of business if it permitted its employees to act otherwise. Similarly, in any enterprise within a +socialist system, workers must direct their efforts toward the goals of the enterprise, otherwise the enterprise will not serve its +purpose as part of the system. Once again, for purely technical reasons it is not possible for most individuals or small groups to +have much autonomy in industrial society. Even the small-business owner commonly has only limited autonomy. Apart from +the necessity of government regulation, he is restricted by the fact that he must fit into the economic system and conform to its +requirements. For instance, when someone develops a new technology, the small-business person often has to use that +technology whether he wants to or not, in order to remain competitive. + +

    + +

    The ‘Bad’ Parts of Technology Cannot Be Separated from the ‘Good’ Parts

    + +

    121. A further reason why industrial society cannot be reformed in favor of freedom is that modern technology is a unified +system in which all parts are dependent on one another. You can’t get rid of the “bad” parts of technology and retain only the +“good” parts. Take modern medicine, for example. Progress in medical science depends on progress in chemistry, physics, +biology, computer science and other fields. Advanced medical treatments require expensive, high-tech equipment that can be +made available only by a technologically progressive, economically rich society. Clearly you can’t have much Progress in +medicine without the whole technological system and everything that goes with it. +

    + +

    122. Even if medical progress could be maintained without the rest of the technological system, it would by itself bring certain +evils. Suppose for example that a cure for diabetes is discovered. People with a genetic tendency to diabetes will then be able to +survive and reproduce as well as anyone else. Natural selection against genes for diabetes will cease and such genes will spread +throughout the population. (This may be occurring to some extent already, since diabetes, while not curable, can be controlled +through use of insulin.) The same thing will happen with many other diseases susceptibility to which is affected by genetic factors (e.g. childhood cancer) +resulting in massive genetic +degradation of the population. The only solution will be some sort of eugenics program or extensive genetic engineering of +human beings, so that man in the future will no longer be a creation of nature, or of chance, or of God (depending on your +religious or philosophical opinions), but a manufactured product. +

    + +

    123. If you think that big government interferes in your life too much NOW, just wait till the government starts regulating the +genetic constitution of your children. Such regulation will inevitably follow the introduction of genetic engineering of human +beings, because the consequences of unregulated genetic engineering would be disastrous. [19] +

    + +

    124. The usual response to such concerns is to talk about “medical ethics.” But a code of ethics would not serve to protect +freedom in the face of medical progress; it would only make matters worse. A code of ethics applicable to genetic engineering +would be in effect a means of regulating the genetic constitution of human beings. Somebody (probably the upper-middle class, +mostly) would decide that such and such applications of genetic engineering were “ethical” and others were not, so that in +effect they would be imposing their own values on the genetic constitution of the population at large. Even if a code of ethics +were chosen on a completely democratic basis, the majority would be imposing their own values on any minorities who might +have a different idea of what constituted an “ethical” use of genetic engineering. The only code of ethics that would truly +protect freedom would be one that prohibited ANY genetic engineering of human beings, and you can be sure that no such +code will ever be applied in a technological society. No code that reduced genetic engineering to a minor role could stand up +for long, because the temptation presented by the immense power of biotechnology would be irresistible, especially since to the +majority of people many of its applications will seem obviously and unequivocally good (eliminating physical and mental +diseases, giving people the abilities they need to get along in today’s world). Inevitably, genetic engineering will be used +extensively, but only in ways consistent with the needs of the industrial-technological system. [20] +

    + +

    Technology is a More Powerful Social Force than the Aspiration for Freedom

    + +

    125. It is not possible to make a LASTING compromise between technology and freedom, because technology is by far the +more powerful social force and continually encroaches on freedom through REPEATED compromises. Imagine the case of +two neighbors, each of whom at the outset owns the same amount of land, but one of whom is more powerful than the other. +The powerful one demands a piece of the other’s land. The weak one refuses. The powerful one says, “OK, let’s compromise. +Give me half of what I asked.” The weak one has little choice but to give in. Some time later the powerful neighbor demands +another piece of land, again there is a compromise, and so forth. By forcing a long series of compromises on the weaker man, +the powerful one eventually gets all of his land. So it goes in the conflict between technology and freedom. +

    + +

    126. Let us explain why technology is a more powerful social force than the aspiration for freedom. +

    + +

    127. A technological advance that appears not to threaten freedom often turns out to threaten it very seriously later on. For +example, consider motorized transport. A walking man formerly could go where he pleased, go at his own pace without +observing any traffic regulations, and was independent of technological support-systems. When motor vehicles were +introduced they appeared to increase man’s freedom. They took no freedom away from the walking man, no one had to have an +automobile if he didn’t want one, and anyone who did choose to buy an automobile could travel much faster and farther than a +walking man. But the introduction of motorized transport soon changed society in such a way as to restrict greatly man’s +freedom of locomotion. When automobiles became numerous, it became necessary to regulate their use extensively. In a car, +especially in densely populated areas, one cannot just go where one likes at one’s own pace one’s movement is governed by the +flow of traffic and by various traffic laws. One is tied down by various obligations: license requirements, driver test, renewing +registration, insurance, maintenance required for safety, monthly payments on purchase price. Moreover, the use of motorized +transport is no longer optional. Since the introduction of motorized transport the arrangement of our cities has changed in such +a way that the majority of people no longer live within walking distance of their place of employment, shopping areas and +recreational opportunities, so that they HAVE TO depend on the automobile for transportation. Or else they must use public +transportation, in which case they have even less control over their own movement than when driving a car. Even the walker’s +freedom is now greatly restricted. In the city he continually has to stop to wait for traffic lights that are designed mainly to +serve auto traffic. In the country, motor traffic makes it dangerous and unpleasant to walk along the highway. (Note this +important point that we have just illustrated with the case of motorized transport: When a new item of technology is introduced +as an option that an individual can accept or not as he chooses, it does not necessarily REMAIN optional. In many cases the +new technology changes society in such a way that people eventually find themselves FORCED to use it.) +

    + +

    128. While technological progress AS A WHOLE continually narrows our sphere of freedom, each new technical advance +CONSIDERED BY ITSELF appears to be desirable. Electricity, indoor plumbing, rapid long-distance communications ... how +could one argue against any of these things, or against any other of the innumerable technical advances that have made modern +society? It would have been absurd to resist the introduction of the telephone, for example. It offered many advantages and no +disadvantages. Yet, as we explained in paragraphs 59-76, all these technical advances taken together have created a world in +which the average man’s fate is no longer in his own hands or in the hands of his neighbors and friends, but in those of +politicians, corporation executives and remote, anonymous technicians and bureaucrats whom he as an individual has no power +to influence. [21] The same process will continue in the future. Take genetic engineering, for example. Few people will resist +the introduction of a genetic technique that eliminates a hereditary disease. It does no apparent harm and prevents much +suffering. Yet a large number of genetic improvements taken together will make the human being into an engineered product +rather than a free creation of chance (or of God, or whatever, depending on your religious beliefs). +

    + +

    129. Another reason why technology is such a powerful social force is that, within the context of a given society, technological +progress marches in only one direction; it can never be reversed. Once a technical innovation has been introduced, people +usually become dependent on it, so that they can never again do without it, unless it is replaced by some still more advanced +innovation. Not only do people become dependent as individuals on a new item of technology, but, even more, the system as a +whole becomes dependent on it. (Imagine what would happen to the system today if computers, for example, were eliminated.) +Thus the system can move in only one direction, toward greater technologization. Technology repeatedly forces freedom to +take a step back, but technology can never take a step back -- short of the overthrow of the whole technological system. +

    + +

    130. Technology advances with great rapidity and threatens freedom at many different points at the same time (crowding, rules +and regulations, increasing dependence of individuals on large organizations, propaganda and other psychological techniques, +genetic engineering, invasion of privacy through surveillance devices and computers, etc.). To hold back any ONE of the +threats to freedom would require a long and difficult social struggle. Those who want to protect freedom are overwhelmed by +the sheer number of new attacks and the rapidity with which they develop, hence they become apathetic and no longer resist. +To fight each of the threats separately would be futile. Success can be hoped for only by fighting the technological system as a +whole; but that is revolution, not reform. +

    + +

    131. Technicians (we use this term in its broad sense to describe all those who perform a specialized task that requires training) +tend to be so involved in their work (their surrogate activity) that when a conflict arises between their technical work and +freedom, they almost always decide in favor of their technical work. This is obvious in the case of scientists, but it also appears +elsewhere: Educators, humanitarian groups, conservation organizations do not hesitate to use propaganda or other psychological +techniques to help them achieve their laudable ends. Corporations and government agencies, when they find it useful, do not +hesitate to collect information about individuals without regard to their privacy. Law enforcement agencies are frequently +inconvenienced by the constitutional rights of suspects and often of completely innocent persons, and they do whatever they +can do legally (or sometimes illegally) to restrict or circumvent those rights. Most of these educators, government officials and +law officers believe in freedom, privacy and constitutional rights, but when these conflict with their work, they usually feel that +their work is more important. +

    + +

    132. It is well known that people generally work better and more persistently when striving for a reward than when attempting +to avoid a punishment or negative outcome. Scientists and other technicians are motivated mainly by the rewards they get +through their work. But those who oppose technological invasions of freedom are working to avoid a negative outcome, +consequently there are few who work persistently and well at this discouraging task. If reformers ever achieved a signal victory +that seemed to set up a solid barrier against further erosion of freedom through technical progress, most would tend to relax and +turn their attention to more agreeable pursuits. But the scientists would remain busy in their laboratories, and technology as it +progresses would find ways, in spite of any barriers, to exert more and more control over individuals and make them always +more dependent on the system. +

    + +

    133. No social arrangements, whether laws, institutions, customs or ethical codes, can provide permanent protection against +technology. History shows that all social arrangements are transitory; they all change or break down eventually. But +technological advances are permanent within the context of a given civilization. Suppose for example that it were possible to +arrive at some social arrangements that would prevent genetic engineering from being applied to human beings, or prevent it +from being applied in such a way as to threaten freedom and dignity. Still, the technology would remain waiting. Sooner or +later the social arrangement would break down. Probably sooner, given the pace of change in our society. Then genetic +engineering would begin to invade our sphere of freedom, and this invasion would be irreversible (short of a breakdown of +technological civilization itself). Any illusions about achieving anything permanent through social arrangements should be +dispelled by what is currently happening with environmental legislation. A few years ago its seemed that there were secure +legal barriers preventing at least SOME of the worst forms of environmental degradation. A change in the political wind, and +those barriers begin to crumble. +

    + +

    134. For all of the foregoing reasons, technology is a more powerful social force than the aspiration for freedom. But this +statement requires an important qualification. It appears that during the next several decades the industrial-technological +system will be undergoing severe stresses due to economic and environmental problems, and especially due to problems of +human behavior (alienation, rebellion, hostility, a variety of social and psychological difficulties). We hope that the stresses +through which the system is likely to pass will cause it to break down, or at least will weaken it sufficiently so that a revolution +against it becomes possible. If such a revolution occurs and is successful, then at that particular moment the aspiration for +freedom will have proved more powerful than technology. +

    + +

    135. In paragraph 125 we used an analogy of a weak neighbor who is left destitute by a strong neighbor who takes all his land +by forcing on him a series of compromises. But suppose now that the strong neighbor gets sick, so that he is unable to defend +himself. The weak neighbor can force the strong one to give him his land back, or he can kill him. If he lets the strong man +survive and only forces him to give the land back, he is a fool, because when the strong man gets well he will again take all the +land for himself. The only sensible alternative for the weaker man is to kill the strong one while he has the chance. In the same +way, while the industrial system is sick we must destroy it. If we compromise with it and let it recover from its sickness, it will +eventually wipe out all of our freedom. +

    + +

    Simpler Social Problems Have Proved Intractable

    + +

    136. If anyone still imagines that it would be possible to reform the system in such a way as to protect freedom from +technology, let him consider how clumsily and for the most part unsuccessfully our society has dealt with other social problems +that are far more simple and straightforward. Among other things, the system has failed to stop environmental degradation, +political corruption, drug trafficking or domestic abuse. +

    + +

    137. Take our environmental problems, for example. Here the conflict of values is straightforward: economic expedience now +versus saving some of our natural resources for our grandchildren. [22] But on this subject we get only a lot of blather and +obfuscation from the people who have power, and nothing like a clear, consistent line of action, and we keep on piling up +environmental problems that our grandchildren will have to live with. Attempts to resolve the environmental issue consist of +struggles and compromises between different factions, some of which are ascendant at one moment, others at another moment. +The line of struggle changes with the shifting currents of public opinion. This is not a rational process, nor is it one that is +likely to lead to a timely and successful solution to the problem. Major social problems, if they get “solved” at all, are rarely or +never solved through any rational, comprehensive plan. They just work themselves out through a process in which various +competing groups pursuing their own (usually short-term) self-interest [23] arrive (mainly by luck) at some more or less stable +modus vivendi. In fact, the principles we formulated in paragraphs 100-106 make it seem doubtful that rational long-term +social planning can EVER be successful. +

    + +

    138. Thus it is clear that the human race has at best a very limited capacity for solving even relatively straightforward social +problems. How then is it going to solve the far more difficult and subtle problem of reconciling freedom with technology? +Technology presents clear-cut material advantages, whereas freedom is an abstraction that means different things to different +people, and its loss is easily obscured by propaganda and fancy talk. +

    + +

    139. And note this important difference: It is conceivable that our environmental problems (for example) may some day be +settled through a rational, comprehensive plan, but if this happens it will be only because it is in the long-term interest of the +system to solve these problems. But it is NOT in the interest of the system to preserve freedom or small-group autonomy. On +the contrary, it is in the interest of the system to bring human behavior under control to the greatest possible extent. [24] Thus, +while practical considerations may eventually force the system to take a rational, prudent approach to environmental problems, +equally practical considerations will force the system to regulate human behavior ever more closely (preferably by indirect +means that will disguise the encroachment on freedom). This isn’t just our opinion. Eminent social scientists (e.g. James Q. +Wilson) have stressed the importance of “socializing” people more effectively. +

    + +

    Revolution is Easier than Reform

    + +

    140. We hope we have convinced the reader that the system cannot be reformed in such a way as to reconcile freedom with +technology. The only way out is to dispense with the industrial-technological system altogether. This implies revolution, not +necessarily an armed uprising, but certainly a radical and fundamental change in the nature of society. +

    + +

    141. People tend to +assume that because a revolution involves a much greater change than reform does, it is more difficult to bring about than +reform is. Actually, under certain circumstances revolution is much easier than reform. The reason is that a revolutionary +movement can inspire an intensity of commitment that a reform movement cannot inspire. A reform movement merely offers +to solve a particular social problem. A revolutionary movement offers to solve all problems at one stroke and create a whole +new world; it provides the kind of ideal for which people will take great risks and make great sacrifices. For this reasons it +would be much easier to overthrow the whole technological system than to put effective, permanent restraints on the +development or application of any one segment of technology, such as genetic engineering, for example. Not many people will +devote themselves with single-minded passion to imposing and maintaining restraints on genetic engineering, but under suitable conditions large numbers of people may devote themselves passionately to a revolution against the industrial-technological system. +As we noted in paragraph 132, reformers seeking to limit certain aspects of technology would be +working to avoid a negative outcome. But revolutionaries work to gain a powerful reward – fulfillment of their revolutionary +vision and therefore work harder and more persistently than reformers do. +

    + +

    142. Reform is always restrained by the fear of painful consequences if changes go too far. But once a revolutionary fever has +taken hold of a society, people are willing to undergo unlimited hardships for the sake of their revolution. This was clearly +shown in the French and Russian Revolutions. It may be that in such cases only a minority of the population is really +committed to the revolution, but this minority is sufficiently large and active so that it becomes the dominant force in society. +We will have more to say about revolution in paragraphs 180-205. +

    + +

    Control of Human Behavior

    + +

    143. Since the beginning of civilization, organized societies have had to put pressures on human beings of the sake of the +functioning of the social organism. The kinds of pressures vary greatly from one society to another. Some of the pressures are +physical (poor diet, excessive labor, environmental pollution), some are psychological (noise, crowding, forcing human +behavior into the mold that society requires). In the past, human nature has been approximately constant, or at any rate has +varied only within certain bounds. Consequently, societies have been able to push people only up to certain limits. When the +limit of human endurance has been passed, things start going wrong: rebellion, or crime, or corruption, or evasion of work, or +depression and other mental problems, or an elevated death rate, or a declining birth rate or something else, so that either the +society breaks down, or its functioning becomes too inefficient and it is (quickly or gradually, through conquest, attrition or +evolution) replaced by some more efficient form of society. [25] +

    + +

    144. Thus human nature has in the past put certain limits on the development of societies. People could be pushed only so far +and no farther. But today this may be changing, because modern technology is developing ways of modifying human beings. +

    + +

    145. Imagine a society that subjects people to conditions that make them terribly unhappy, then gives them drugs to take away +their unhappiness. Science fiction? It is already happening to some extent in our own society. It is well known that the rate of +clinical depression has been greatly increasing in recent decades. We believe that this is due to disruption of the power process, +as explained in paragraphs 59-76. But even if we are wrong, the increasing rate of depression is certainly the result of SOME +conditions that exist in today’s society. Instead of removing the conditions that make people depressed, modern society gives +them antidepressant drugs. In effect, antidepressants are a means of modifying an individual’s internal state in such a way as to +enable him to tolerate social conditions that he would otherwise find intolerable. (Yes, we know that depression is often of +purely genetic origin. We are referring here to those cases in which environment plays the predominant role.) +

    + +

    146. Drugs that affect the mind are only one example of the new methods of controlling human behavior that modern society is +developing. Let us look at some of the other methods. +

    + +

    147. To start with, there are the techniques of surveillance. Hidden video cameras are now used in most stores and in many +other places, computers are used to collect and process vast amounts of information about individuals. Information so obtained +greatly increases the effectiveness of physical coercion (i.e., law enforcement). [26] Then there are the methods of propaganda, +for which the mass communication media provide effective vehicles. Efficient techniques have been developed for winning +elections, selling products, influencing public opinion. The entertainment industry serves as an important psychological tool of +the system, possibly even when it is dishing out large amounts of sex and violence. Entertainment provides modern man with +an essential means of escape. While absorbed in television, videos, etc., he can forget stress, anxiety, frustration, +dissatisfaction. Many primitive peoples, when they don’t have work to do, are quite content to sit for hours at a time doing +nothing at all, because they are at peace with themselves and their world. But most modern people must be constantly occupied +or entertained, otherwise they get “bored,” i.e., they get fidgety, uneasy, irritable. +

    + +

    148. Other techniques strike deeper than the foregoing. Education is no longer a simple affair of paddling a kid’s behind when +he doesn’t know his lessons and patting him on the head when he does know them. It is becoming a scientific technique for +controlling the child’s development. Sylvan Learning Centers, for example, have had great success in motivating children to +study, and psychological techniques are also used with more or less success in many conventional schools. “Parenting” +techniques that are taught to parents are designed to make children accept fundamental values of the system and behave in +ways that the system finds desirable. “Mental health” programs, “intervention” techniques, psychotherapy and so forth are +ostensibly designed to benefit individuals, but in practice they usually serve as methods for inducing individuals to think and behave as the system requires. (There is no contradiction here; an individual whose attitudes or behavior bring him into conflict +with the system is up against a force that is too powerful for him to conquer or escape from, hence he is likely to suffer from +stress, frustration, defeat. His path will be much easier if he thinks and behaves as the system requires. In that sense the system +is acting for the benefit of the individual when it brainwashes him into conformity.) Child abuse in its gross and obvious forms +is disapproved in most if not all cultures. Tormenting a child for a trivial reason or no reason at all is something that appalls +almost everyone. But many psychologists interpret the concept of abuse much more broadly. Is spanking, when used as part of +a rational and consistent system of discipline, a form of abuse? The question will ultimately be decided by whether or not +spanking tends to produce behavior that makes a person fit in well with the existing system of society. In practice, the word +“abuse” tends to be interpreted to include any method of child-rearing that produces behavior inconvenient for the system. +Thus, when they go beyond the prevention of obvious, senseless cruelty, programs for preventing “child abuse” are directed +toward the control of human behavior on behalf of the system. +

    + +

    149. Presumably, research will continue to increase the effectiveness of psychological techniques for controlling human +behavior. But we think it is unlikely that psychological techniques alone will be sufficient to adjust human beings to the kind of +society that technology is creating. Biological methods probably will have to be used. We have already mentioned the use of +drugs in this connection. Neurology may provide other avenues for modifying the human mind. Genetic engineering of human +beings is already beginning to occur in the form of “gene therapy,” and there is no reason to assume that such methods will not +eventually be used to modify those aspects of the body that affect mental functioning. +

    + +

    150. As we mentioned in paragraph 134, industrial society seems likely to be entering a period of severe stress, due in part to +problems of human behavior and in part to economic and environmental problems. And a considerable proportion of the +system’s economic and environmental problems result from the way human beings behave. Alienation, low self-esteem, +depression, hostility, rebellion; children who won’t study, youth gangs, illegal drug use, rape, child abuse, other crimes, unsafe +sex, teen pregnancy, population growth, political corruption, race hatred, ethnic rivalry, bitter ideological conflict (e.g., pro-choice vs. pro-life), political extremism, terrorism, sabotage, anti-government groups, hate groups. All these threaten the very +survival of the system. The system will therefore be FORCED to use every practical means of controlling human behavior. +

    + +

    151. The social disruption that we see today is certainly not the result of mere chance. It can only be a result of the conditions +of life that the system imposes on people. (We have argued that the most important of these conditions is disruption of the +power process.) If the systems succeeds in imposing sufficient control over human behavior to assure its own survival, a new +watershed in human history will have been passed. Whereas formerly the limits of human endurance have imposed limits on +the development of societies (as we explained in paragraphs 143, 144), industrial-technological society will be able to pass +those limits by modifying human beings, whether by psychological methods or biological methods or both. In the future, social +systems will not be adjusted to suit the needs of human beings. Instead, human being will be adjusted to suit the needs of the +system. [27] +

    + +

    152. Generally speaking, technological control over human behavior will probably not be introduced with a totalitarian +intention or even through a conscious desire to restrict human freedom. [28] Each new step in the assertion of control over the +human mind will be taken as a rational response to a problem that faces society, such as curing alcoholism, reducing the crime +rate or inducing young people to study science and engineering. In many cases there will be a humanitarian justification. For +example, when a psychiatrist prescribes an anti-depressant for a depressed patient, he is clearly doing that individual a favor. It +would be inhumane to withhold the drug from someone who needs it. When Parents send their children to Sylvan Learning +Centers to have them manipulated into becoming enthusiastic about their studies, they do so from concern for their children’s +welfare. It may be that some of these parents wish that one didn’t have to have specialized training to get a job and that their +kid didn’t have to be brainwashed into becoming a computer nerd. But what can they do? They can’t change society, and their +child may be unemployable if he doesn’t have certain skills. So they send him to Sylvan. +

    + +

    153. Thus control over human behavior will be introduced not by a calculated decision of the authorities but through a process +of social evolution (RAPID evolution, however). The process will be impossible to resist, because each advance, considered by +itself, will appear to be beneficial, or at least the evil involved in making the advance will appear to be beneficial, or at least the +evil involved in making the advance will seem to be less than that which would result from not making it (see paragraph 127). +Propaganda for example is used for many good purposes, such as discouraging child abuse or race hatred. [14] Sex education is +obviously useful, yet the effect of sex education (to the extent that it is successful) is to take the shaping of sexual attitudes +away from the family and put it into the hands of the state as represented by the public school system. +

    + +

    154. Suppose a biological trait is discovered that increases the likelihood that a child will grow up to be a criminal, and suppose +some sort of gene therapy can remove this trait. [29] Of course most parents whose children possess the trait will have them +undergo the therapy. It would be inhumane to do otherwise, since the child would probably have a miserable life if he grew up +to be a criminal. But many or most primitive societies have a low crime rate in comparison with that of our society, even though they have neither high-tech methods of child-rearing nor harsh systems of punishment. Since there is no reason to +suppose that more modern men than primitive men have innate predatory tendencies, the high crime rate of our society must be +due to the pressures that modern conditions put on people, to which many cannot or will not adjust. Thus a treatment designed +to remove potential criminal tendencies is at least in part a way of re-engineering people so that they suit the requirements of +the system. +

    + +

    155. Our society tends to regard as a “sickness” any mode of thought or behavior that is inconvenient for the system, and this is +plausible because when an individual doesn’t fit into the system it causes pain to the individual as well as problems for the +system. Thus the manipulation of an individual to adjust him to the system is seen as a “cure” for a “sickness” and therefore as +good. +

    + +

    156. In paragraph 127 we pointed out that if the use of a new item of technology is INITIALLY optional, it does not +necessarily REMAIN optional, because the new technology tends to change society in such a way that it becomes difficult or +impossible for an individual to function without using that technology. This applies also to the technology of human behavior. +In a world in which most children are put through a program to make them enthusiastic about studying, a parent will almost be +forced to put his kid through such a program, because if he does not, then the kid will grow up to be, comparatively speaking, +an ignoramus and therefore unemployable. Or suppose a biological treatment is discovered that, without undesirable sideeffects, will greatly reduce the psychological stress from which so many people suffer in our society. If large numbers of +people choose to undergo the treatment, then the general level of stress in society will be reduced, so that it will be possible for +the system to increase the stress-producing pressures. +This will lead more people to undergo the treatment and so forth so that eventually the pressures may become so heavy that few people will be able to survive without undergoing the stress-reducing treatment. +In fact, something like this seems to have happened already with one of +our society’s most important psychological tools for enabling people to reduce (or at least temporarily escape from) stress, +namely, mass entertainment (see paragraph 147). Our use of mass entertainment is “optional”: No law requires us to watch +television, listen to the radio, read magazines. Yet mass entertainment is a means of escape and stress-reduction on which most +of us have become dependent. Everyone complains about the trashiness of television, but almost everyone watches it. A few +have kicked the TV habit, but it would be a rare person who could get along today without using ANY form of mass +entertainment. (Yet until quite recently in human history most people got along very nicely with no other entertainment than +that which each local community created for itself.) Without the entertainment industry the system probably would not have +been able to get away with putting as much stress-producing pressure on us as it does. +

    + +

    157. Assuming that industrial society survives, it is likely that technology will eventually acquire something approaching +complete control over human behavior. It has been established beyond any rational doubt that human thought and behavior +have a largely biological basis. As experimenters have demonstrated, feelings such as hunger, pleasure, anger and fear can be +turned on and off by electrical stimulation of appropriate parts of the brain. Memories can be destroyed by damaging parts of +the brain or they can be brought to the surface by electrical stimulation. Hallucinations can be induced or moods changed by +drugs. There may or may not be an immaterial human soul, but if there is one it clearly is less powerful that the biological +mechanisms of human behavior. For if that were not the case then researchers would not be able so easily to manipulate human +feelings and behavior with drugs and electrical currents. +

    + +

    158. It presumably would be impractical for all people to have electrodes inserted in their heads so that they could be +controlled by the authorities. But the fact that human thoughts and feelings are so open to biological intervention shows that the +problem of controlling human behavior is mainly a technical problem; a problem of neurons, hormones and complex +molecules; the kind of problem that is accessible to scientific attack. Given the outstanding record of our society in solving +technical problems, it is overwhelmingly probable that great advances will be made in the control of human behavior. +

    + +

    159. Will public resistance prevent the introduction of technological control of human behavior? It certainly would if an +attempt were made to introduce such control all at once. But since technological control will be introduced through a long +sequence of small advances, there will be no rational and effective public resistance. (See paragraphs 127, 132, 153.) +

    + +

    160. To those who think that all this sounds like science fiction, we point out that yesterday’s science fiction is today’s fact. +The Industrial Revolution has radically altered man’s environment and way of life, and it is only to be expected that as +technology is increasingly applied to the human body and mind, man himself will be altered as radically as his environment +and way of life have been. +

    + +

    Human Race at a Crossroads

    + +

    161. But we have gotten ahead of our story. It is one thing to develop in the laboratory a series of psychological or biological +techniques for manipulating human behavior and quite another to integrate these techniques into a functioning social system. +The latter problem is the more difficult of the two. For example, while the techniques of educational psychology doubtless +work quite well in the “lab schools” where they are developed, it is not necessarily easy to apply them effectively throughout +our educational system. We all know what many of our schools are like. The teachers are too busy taking knives and guns +away from the kids to subject them to the latest techniques for making them into computer nerds. Thus, in spite of all its +technical advances relating to human behavior, the system to date has not been impressively successful in controlling human +beings. The people whose behavior is fairly well under the control of the system are those of the type that might be called +“bourgeois.” But there are growing numbers of people who in one way or another are rebels against the system: welfare +leaches, youth gangs, cultists. Satanists, Nazis, radical environmentalists, militiamen, etc. +

    + +

    162. The system is currently engaged in a desperate struggle to overcome certain problems that threaten its survival, among +which the problems of human behavior are the most important. If the system succeeds in acquiring sufficient control over +human behavior quickly enough, it will probably survive. Otherwise it will break down. We think the issue will most likely be +resolved within the next several decades, say 40 to 100 years. +

    + +

    163. Suppose the system survives the crisis of the next several decades. By that time it will have to have solved, or at least +brought under control, the principal problems that confront it, in particular that of “socializing” human beings; that is, making +people sufficiently docile so that heir behavior no longer threatens the system. That being accomplished, it does not appear that +there would be any further obstacle to the development of technology, and it would presumably advance toward its logical +conclusion, which is complete control over everything on Earth, including human beings and all other important organisms. +The system may become a unitary, monolithic organization, or it may be more or less fragmented and consist of a number of +organizations coexisting in a relationship that includes elements of both cooperation and competition, just as today the +government, the corporations and other large organizations both cooperate and compete with one another. Human freedom +mostly will have vanished, because individuals and small groups will be impotent vis-a-vis large organizations armed with +super-technology and an arsenal of advanced psychological and biological tools for manipulating human beings, besides +instruments of surveillance and physical coercion. Only a small number of people will have any real power, and even these +probably will have only very limited freedom, because their behavior too will be regulated; just as today our politicians and +corporation executives can retain their positions of power only as long as their behavior remains within certain fairly narrow +limits. +

    + +

    164. Don’t imagine that the systems will stop developing further techniques for controlling human beings and nature once the +crisis of the next few decades is over and increasing control is no longer necessary for the system’s survival. On the contrary, +once the hard times are over the system will increase its control over people and nature more rapidly, because it will no longer +be hampered by difficulties of the kind that it is currently experiencing. Survival is not the principal motive for extending +control. As we explained in paragraphs 87-90, technicians and scientists carry on their work largely as a surrogate activity; that +is, they satisfy their need for power by solving technical problems. They will continue to do this with unabated enthusiasm, and +among the most interesting and challenging problems for them to solve will be those of understanding the human body and +mind and intervening in their development. For the “good of humanity,” of course. +

    + +

    165. But suppose on the other hand that the stresses of the coming decades prove to be too much for the system. If the system +breaks down there may be a period of chaos, a “time of troubles” such as those that history has recorded at various epochs in +the past. It is impossible to predict what would emerge from such a time of troubles, but at any rate the human race would be +given a new chance. The greatest danger is that industrial society may begin to reconstitute itself within the first few years after +the breakdown. Certainly there will be many people (power-hungry types especially) who will be anxious to get the factories +running again. +

    + +

    166. Therefore two tasks confront those who hate the servitude to which the industrial system is reducing the human race. First, +we must work to heighten the social stresses within the system so as to increase the likelihood that it will break down or be +weakened sufficiently so that a revolution against it becomes possible. Second, it is necessary to develop and propagate an +ideology that opposes technology and the industrial society if and when the system becomes sufficiently weakened. And such +an ideology will help to assure that, if and when industrial society breaks down, its remnants will be smashed beyond repair, so +that the system cannot be reconstituted. The factories should be destroyed, technical books burned, etc. +

    + +

    Human Suffering

    + +

    167. The industrial system will not break down purely as a result of revolutionary action. It will not be vulnerable to +revolutionary attack unless its own internal problems of development lead it into very serious difficulties. So if the system +breaks down it will do so either spontaneously, or through a process that is in part spontaneous but helped along by +revolutionaries. If the breakdown is sudden, many people will die, since the world’s population has become so over-grown that +it cannot even feed itself any longer without advanced technology. Even if the breakdown is gradual enough so that reduction +of the population can occur more through lowering of the birth rate than through elevation of the death rate, the process of deindustrialization probably will be very chaotic and involve much suffering. It is naive to think it likely that technology can be +phased out in a smoothly managed, orderly way, especially since the technophiles will fight stubbornly at every step. Is it +therefore cruel to work for the breakdown of the system? Maybe, but maybe not. In the first place, revolutionaries will not be +able to break the system down unless it is already in enough trouble so that there would be a good chance of its eventually +breaking down by itself anyway; and the bigger the system grows, the more disastrous the consequences of its breakdown will +be; so it may be that revolutionaries, by hastening the onset of the breakdown, will be reducing the extent of the disaster. +

    + +

    168. In the second place, one has to balance struggle and death against the loss of freedom and dignity. To many of us, freedom +and dignity are more important than a long life or avoidance of physical pain. Besides, we all have to die some time, and it may +be better to die fighting for survival, or for a cause, than to live a long but empty and purposeless life. +

    + +

    169. In the third place, it is not at all certain that survival of the system will lead to less suffering than breakdown of the system +would. The system has already caused, and is continuing to cause, immense suffering all over the world. Ancient cultures, that +for hundreds of years gave people a satisfactory relationship with each other and with their environment, have been shattered +by contact with industrial society, and the result has been a whole catalogue of economic, environmental, social and +psychological problems. One of the effects of the intrusion of industrial society has been that over much of the world +traditional controls on population have been thrown out of balance. Hence the population explosion, with all that that implies. +Then there is the psychological suffering that is widespread throughout the supposedly fortunate countries of the West (see +paragraphs 44, 45). No one knows what will happen as a result of ozone depletion, the greenhouse effect and other +environmental problems that cannot yet be foreseen. And, as nuclear proliferation has shown, new technology cannot be kept +out of the hands of dictators and irresponsible Third World nations. Would you like to speculate about what Iraq or North +Korea will do with genetic engineering? +

    + +

    170. “Oh!” say the technophiles, “Science is going to fix all that! We will conquer famine, eliminate psychological suffering, +make everybody healthy and happy!” Yeah, sure. That’s what they said 200 years ago. The Industrial Revolution was supposed +to eliminate poverty, make everybody happy, etc. The actual result has been quite different. The technophiles are hopelessly +naive (or self-deceiving) in their understanding of social problems. They are unaware of (or choose to ignore) the fact that +when large changes, even seemingly beneficial ones, are introduced into a society, they lead to a long sequence of other +changes, most of which are impossible to predict (paragraph 103). The result is disruption of the society. So it is very probable +that in their attempts to end poverty and disease, engineer docile, happy personalities and so forth, the technophiles will create +social systems that are terribly troubled, even more so than the present once. For example, the scientists boast that they will end +famine by creating new, genetically engineered food plants. But this will allow the human population to keep expanding +indefinitely, and it is well known that crowding leads to increased stress and aggression. This is merely one example of the +PREDICTABLE problems that will arise. We emphasize that, as past experience has shown, technical progress will lead to +other new problems that CANNOT be predicted in advance (paragraph 103). In fact, ever since the Industrial Revolution, +technology has been creating new problems for society far more rapidly than it has been solving old ones. Thus it will take a +long and difficult period of trial and error for the technophiles to work the bugs out of their Brave New World (if they every +do). In the meantime there will be great suffering. So it is not at all clear that the survival of industrial society would involve +less suffering than the breakdown of that society would. Technology has gotten the human race into a fix from which there is +not likely to be any easy escape. +

    + +

    The Future

    + +

    171. But suppose now that industrial society does survive the next several decades and that the bugs do eventually get worked +out of the system, so that it functions smoothly. What kind of system will it be? We will consider several possibilities. +

    + +

    172. First let us postulate that the computer scientists succeed in developing intelligent machines that can do all things better +than human beings can do them. In that case presumably all work will be done by vast, highly organized systems of machines +and no human effort will be necessary. Either of two cases might occur. The machines might be permitted to make all of their +own decisions without human oversight, or else human control over the machines might be retained. +

    + +

    173. If the machines are permitted to make all their own decisions, we can’t make any conjectures as to the results, because it is +impossible to guess how such machines might behave. We only point out that the fate of the human race would be at the mercy +of the machines. It might be argued that the human race would never be foolish enough to hand over all power to the machines. +But we are suggesting neither that the human race would voluntarily turn power over to the machines nor that the machines +would willfully seize power. What we do suggest is that the human race might easily permit itself to drift into a position of +such dependence on the machines that it would have no practical choice but to accept all of the machines’ decisions. As society +and the problems that face it become more and more complex and as machines become more and more intelligent, people will +let machines make more and more of their decisions for them, simply because machine-made decisions will bring better results +than man-made ones. Eventually a stage may be reached at which the decisions necessary to keep the system running will be so +complex that human beings will be incapable of making them intelligently. At that stage the machines will be in effective +control. People won’t be able to just turn the machine off, because they will be so dependent on them that turning them off +would amount to suicide. +

    + +

    174. On the other hand it is possible that human control over the machines may be retained. In that case the average man may +have control over certain private machines of his own, such as his car or his personal computer, but control over large systems +of machines will be in the hands of a tiny elite – just as it is today, but with two differences. Due to improved techniques the +elite will have greater control over the masses; and because human work will no longer be necessary the masses will be +superfluous, a useless burden on the system. If the elite is ruthless they may simply decide to exterminate the mass of +humanity. If they are humane they may use propaganda or other psychological or biological techniques to reduce the birth rate +until the mass of humanity becomes extinct, leaving the world to the elite. Or, if the elite consists of soft-hearted liberals, they +may decide to play the role of good shepherds to the rest of the human race. They will see to it that everyone’s physical needs +are satisfied, that all children are raised under psychologically hygienic conditions, that everyone has a wholesome hobby to +keep him busy, and that anyone who may become dissatisfied undergoes “treatment” to cure his “problem.” Of course, life will +be so purposeless that people will have to be biologically or psychologically engineered either to remove their need for the +power process or to make them “sublimate” their drive for power into some harmless hobby. These engineered human beings +may be happy in such a society, but they most certainly will not be free. They will have been reduced to the status of domestic +animals. +

    + +

    175. But suppose now that the computer scientists do not succeed in developing artificial intelligence, so that human work +remains necessary. Even so, machines will take care of more and more of the simpler tasks so that there will be an increasing +surplus of human workers at the lower levels of ability. (We see this happening already. There are many people who find it +difficult or impossible to get work, because for intellectual or psychological reasons they cannot acquire the level of training +necessary to make themselves useful in the present system.) On those who are employed, ever-increasing demands will be +placed: They will need more and more training, more and more ability, and will have to be ever more reliable, conforming and +docile, because they will be more and more like cells of a giant organism. Their tasks will be increasingly specialized, so that +their work will be, in a sense, out of touch with the real world, being concentrated on one tiny slice of reality. The system will +have to use any means that it can, whether psychological or biological, to engineer people to be docile, to have the abilities that +the system requires and to “sublimate” their drive for power into some specialized task. But the statement that the people of +such a society will have to be docile may require qualification. The society may find competitiveness useful, provided that +ways are found of directing competitiveness into channels that serve the needs of the system. We can imagine a future society +in which there is endless competition for positions of prestige and power. But no more than a very few people will ever reach +the top, where the only real power is (see end of paragraph 163). Very repellent is a society in which a person can satisfy his +need for power only by pushing large numbers of other people out of the way and depriving them of THEIR opportunity for +power. +

    + +

    176. One can envision scenarios that incorporate aspects of more than one of the possibilities that we have just discussed. For +instance, it may be that machines will take over most of the work that is of real, practical importance, but that human beings +will be kept busy by being given relatively unimportant work. It has been suggested, for example, that a great development of +the service industries might provide work for human beings. Thus people would spent their time shining each other’s shoes, +driving each other around in taxicabs, making handicrafts for one another, waiting on each other’s tables, etc. This seems to us +a thoroughly contemptible way for the human race to end up, and we doubt that many people would find fulfilling lives in such +pointless busy-work. They would seek other, dangerous outlets (drugs, crime, “cults,” hate groups) unless they were +biologically or psychologically engineered to adapt them to such a way of life. +

    + +

    177. Needless to say, the scenarios outlined above do not exhaust all the possibilities. They only indicate the kinds of outcomes +that seem to us most likely. But we can envision no plausible scenarios that are any more palatable than the ones we’ve just +described. It is overwhelmingly probable that if the industrial-technological system survives the next 40 to 100 years, it will by +that time have developed certain general characteristics: Individuals (at least those of the “bourgeois” type, who are integrated into the system and make it run, and who therefore have all the power) will be more dependent than ever on large +organizations; they will be more “socialized” than ever and their physical and mental qualities to a significant extent (possibly +to a very great extent) will be those that are engineered into them rather than being the results of chance (or of God’s will, or +whatever); and whatever may be left of wild nature will be reduced to remnants preserved for scientific study and kept under +the supervision and management of scientists (hence it will no longer be truly wild). In the long run (say a few centuries from +now) it is likely that neither the human race nor any other important organisms will exist as we know them today, because once +you start modifying organisms through genetic engineering there is no reason to stop at any particular point, so that the +modifications will probably continue until man and other organisms have been utterly transformed. +

    + +

    178. Whatever else may be the case, it is certain that technology is creating for human beings a new physical and social +environment radically different from the spectrum of environments to which natural selection has adapted the human race +physically and psychologically. If man is not adjusted to this new environment by being artificially re-engineered, then he will +be adapted to it through a long and painful process of natural selection. The former is far more likely than the latter. +

    + +

    179. It would be better to dump the whole stinking system and take the consequences. +

    + +

    Strategy

    + +

    180. The technophiles are taking us all on an utterly reckless ride into the unknown. Many people understand something of +what technological progress is doing to us yet take a passive attitude toward it because they think it is inevitable. But we (FC) +don’t think it is inevitable. We think it can be stopped, and we will give here some indications of how to go about stopping it. +

    + +

    181. As we stated in paragraph 166, the two main tasks for the present are to promote social stress and instability in industrial +society and to develop and propagate an ideology that opposes technology and the industrial system. When the system becomes +sufficiently stressed and unstable, a revolution against technology may be possible. The pattern would be similar to that of the +French and Russian Revolutions. French society and Russian society, for several decades prior to their respective revolutions, +showed increasing signs of stress and weakness. Meanwhile, ideologies were being developed that offered a new world view +that was quite different from the old one. In the Russian case, revolutionaries were actively working to undermine the old +order. Then, when the old system was put under sufficient additional stress (by financial crisis in France, by military defeat in +Russia) it was swept away by revolution. What we propose is something along the same lines. +

    + +

    182. It will be objected that the French and Russian Revolutions were failures. But most revolutions have two goals. One is to +destroy an old form of society and the other is to set up the new form of society envisioned by the revolutionaries. The French +and Russian revolutionaries failed (fortunately!) to create the new kind of society of which they dreamed, but they were quite +successful in destroying the old society. We have no illusions about the feasibility of creating a new, ideal form of society. Our +goal is only to destroy the existing form of society. +

    + +

    183. But an ideology, in order to gain enthusiastic support, must have a positive ideal as well as a negative one; it must be FOR +something as well as AGAINST something. The positive ideal that we propose is Nature. That is, WILD nature: those aspects +of the functioning of the Earth and its living things that are independent of human management and free of human interference +and control. And with wild nature we include human nature, by which we mean those aspects of the functioning of the human +individual that are not subject to regulation by organized society but are products of chance, or free will, or God (depending on +your religious or philosophical opinions). +

    + +

    184. Nature makes a perfect counter-ideal to technology for several reasons. Nature (that which is outside the power of the +system) is the opposite of technology (which seeks to expand indefinitely the power of the system). Most people will agree that +nature is beautiful; certainly it has tremendous popular appeal. The radical environmentalists ALREADY hold an ideology that +exalts nature and opposes technology. [30] It is not necessary for the sake of nature to set up some chimerical utopia or any +new kind of social order. Nature takes care of itself: It was a spontaneous creation that existed long before any human society, +and for countless centuries many different kinds of human societies coexisted with nature without doing it an excessive amount +of damage. Only with the Industrial Revolution did the effect of human society on nature become really devastating. To relieve +the pressure on nature it is not necessary to create a special kind of social system, it is only necessary to get rid of industrial +society. Granted, this will not solve all problems. Industrial society has already done tremendous damage to nature and it will +take a very long time for the scars to heal. Besides, even pre-industrial societies can do significant damage to nature. +Nevertheless, getting rid of industrial society will accomplish a great deal. It will relieve the worst of the pressure on nature so +that the scars can begin to heal. It will remove the capacity of organized society to keep increasing its control over nature (including human nature). Whatever kind of society may exist after the demise of the industrial system, it is certain that most +people will live close to nature, because in the absence of advanced technology there is no other way that people CAN live. To +feed themselves they must be peasants or herdsmen or fishermen or hunters, etc. And, generally speaking, local autonomy +should tend to increase, because lack of advanced technology and rapid communications will limit the capacity of governments +or other large organizations to control local communities. +

    + +

    185. As for the negative consequences of eliminating industrial society – well, you can’t eat your cake and have it too. To gain +one thing you have to sacrifice another. +

    + +

    186. Most people hate psychological conflict. For this reason they avoid doing any serious thinking about difficult social +issues, and they like to have such issues presented to them in simple, black-and-white terms: THIS is all good and THAT is all +bad. The revolutionary ideology should therefore be developed on two levels. +

    + +

    187. On the more sophisticated level the ideology should address itself to people who are intelligent, thoughtful and rational. +The object should be to create a core of people who will be opposed to the industrial system on a rational, thought-out basis, +with full appreciation of the problems and ambiguities involved, and of the price that has to be paid for getting rid of the +system. It is particularly important to attract people of this type, as they are capable people and will be instrumental in +influencing others. These people should be addressed on as rational a level as possible. Facts should never intentionally be +distorted and intemperate language should be avoided. This does not mean that no appeal can be made to the emotions, but in +making such appeal care should be taken to avoid misrepresenting the truth or doing anything else that would destroy the +intellectual respectability of the ideology. +

    + +

    188. On a second level, the ideology should be propagated in a simplified form that will enable the unthinking majority to see +the conflict of technology vs. nature in unambiguous terms. But even on this second level the ideology should not be expressed +in language that is so cheap, intemperate or irrational that it alienates people of the thoughtful and rational type. Cheap, +intemperate propaganda sometimes achieves impressive short-term gains, but it will be more advantageous in the long run to +keep the loyalty of a small number of intelligently committed people than to arouse the passions of an unthinking, fickle mob +who will change their attitude as soon as someone comes along with a better propaganda gimmick. However, propaganda of +the rabble-rousing type may be necessary when the system is nearing the point of collapse and there is a final struggle between +rival ideologies to determine which will become dominant when the old world-view goes under. +

    + +

    189. Prior to that final struggle, the revolutionaries should not expect to have a majority of people on their side. History is +made by active, determined minorities, not by the majority, which seldom has a clear and consistent idea of what it really +wants. Until the time comes for the final push toward revolution [31], the task of revolutionaries will be less to win the shallow +support of the majority than to build a small core of deeply committed people. As for the majority, it will be enough to make +them aware of the existence of the new ideology and remind them of it frequently; though of course it will be desirable to get +majority support to the extent that this can be done without weakening the core of seriously committed people. +

    + +

    190. Any kind of social conflict helps to destabilize the system, but one should be careful about what kind of conflict one +encourages. The line of conflict should be drawn between the mass of the people and the power-holding elite of industrial +society (politicians, scientists, upper-level business executives, government officials, etc.). It should NOT be drawn between +the revolutionaries and the mass of the people. For example, it would be bad strategy for the revolutionaries to condemn +Americans for their habits of consumption. Instead, the average American should be portrayed as a victim of the advertising +and marketing industry, which has suckered him into buying a lot of junk that he doesn’t need and that is very poor +compensation for his lost freedom. Either approach is consistent with the facts. It is merely a matter of attitude whether you +blame the advertising industry for manipulating the public or blame the public for allowing itself to be manipulated. As a +matter of strategy one should generally avoid blaming the public. +

    + +

    191. One should think twice before encouraging any other social conflict than that between the power-holding elite (which +wields technology) and the general public (over which technology exerts its power). For one thing, other conflicts tend to +distract attention from the important conflicts (between power-elite and ordinary people, between technology and nature); for +another thing, other conflicts may actually tend to encourage technologization, because each side in such a conflict wants to +use technological power to gain advantages over its adversary. This is clearly seen in rivalries between nations. It also appears +in ethnic conflicts within nations. For example, in America many black leaders are anxious to gain power for African +Americans by placing back individuals in the technological power-elite. They want there to be many black government +officials, scientists, corporation executives and so forth. In this way they are helping to absorb the African American subculture +into the technological system. Generally speaking, one should encourage only those social conflicts that can be fitted into the +framework of the conflicts of power-elite vs. ordinary people, technology vs. nature. +

    + +

    192. But the way to discourage ethnic conflict is NOT through militant advocacy of minority rights (see paragraphs 21, 29). +Instead, the revolutionaries should emphasize that although minorities do suffer more or less disadvantage, this disadvantage is +of peripheral significance. Our real enemy is the industrial-technological system, and in the struggle against the system, ethnic +distinctions are of no importance. +

    + +

    193. The kind of revolution we have in mind will not necessarily involve an armed uprising against any government. It may or +may not involve physical violence, but it will not be a POLITICAL revolution. Its focus will be on technology and economics, +not politics. [32] +

    + +

    194. Probably the revolutionaries should even AVOID assuming political power, whether by legal or illegal means, until the +industrial system is stressed to the danger point and has proved itself to be a failure in the eyes of most people. Suppose for +example that some “green” party should win control of the United States Congress in an election. In order to avoid betraying or +watering down their own ideology they would have to take vigorous measures to turn economic growth into economic +shrinkage. To the average man the results would appear disastrous: There would be massive unemployment, shortages of +commodities, etc. Even if the grosser ill effects could be avoided through superhumanly skillful management, still people would +have to begin giving up the luxuries to which they have become addicted. Dissatisfaction would grow, the “green” party would +be voted out of office and the revolutionaries would have suffered a severe setback. For this reason the revolutionaries should +not try to acquire political power until the system has gotten itself into such a mess that any hardships will be seen as resulting +from the failures of the industrial system itself and not from the policies of the revolutionaries. The revolution against +technology will probably have to be a revolution by outsiders, a revolution from below and not from above. +

    + +

    195. The revolution must be international and worldwide. It cannot be carried out on a nation-by-nation basis. Whenever it is +suggested that the United States, for example, should cut back on technological progress or economic growth, people get +hysterical and start screaming that if we fall behind in technology the Japanese will get ahead of us. Holy robots! The world +will fly off its orbit if the Japanese ever sell more cars than we do! (Nationalism is a great promoter of technology.) More +reasonably, it is argued that if the relatively democratic nations of the world fall behind in technology while nasty, dictatorial +nations like China, Vietnam and North Korea continue to progress, eventually the dictators may come to dominate the world. +That is why the industrial system should be attacked in all nations simultaneously, to the extent that this may be possible. True, +there is no assurance that the industrial system can be destroyed at approximately the same time all over the world, and it is +even conceivable that the attempt to overthrow the system could lead instead to the domination of the system by dictators. That +is a risk that has to be taken. And it is worth taking, since the difference between a “democratic” industrial system and one +controlled by dictators is small compared with the difference between an industrial system and a non-industrial one. [33] It +might even be argued that an industrial system controlled by dictators would be preferable, because dictator-controlled systems +usually have proved inefficient, hence they are presumably more likely to break down. Look at Cuba. +

    + +

    196. Revolutionaries might consider favoring measures that tend to bind the world economy into a unified whole. Free trade +agreements like NAFTA and GATT are probably harmful to the environment in the short run, but in the long run they may +perhaps be advantageous because they foster economic interdependence between nations. It will be easier to destroy the +industrial system on a worldwide basis if the world economy is so unified that its breakdown in any one major nation will lead +to its breakdown in all industrialized nations. +

    + +

    197. Some people take the line that modern man has too much power, too much control over nature; they argue for a more +passive attitude on the part of the human race. At best these people are expressing themselves unclearly, because they fail to +distinguish between power for LARGE ORGANIZATIONS and power for INDIVIDUALS and SMALL GROUPS. It is a +mistake to argue for powerlessness and passivity, because people NEED power. Modern man as a collective entity – that is, +the industrial system – has immense power over nature, and we (FC) regard this as evil. But modern INDIVIDUALS and +SMALL GROUPS OF INDIVIDUALS have far less power than primitive man ever did. Generally speaking, the vast power of +“modern man” over nature is exercised not by individuals or small groups but by large organizations. To the extent that the +average modern INDIVIDUAL can wield the power of technology, he is permitted to do so only within narrow limits and only +under the supervision and control of the system. (You need a license for everything and with the license come rules and +regulations.) The individual has only those technological powers with which the system chooses to provide him. His +PERSONAL power over nature is slight. +

    + +

    198. Primitive INDIVIDUALS and SMALL GROUPS actually had considerable power over nature; or maybe it would be +better to say power WITHIN nature. When primitive man needed food he knew how to find and prepare edible roots, how to +track game and take it with homemade weapons. He knew how to protect himself from heat cold, rain, dangerous animals, etc. +But primitive man did relatively little damage to nature because the COLLECTIVE power of primitive society was negligible +compared to the COLLECTIVE power of industrial society. +

    + +

    199. Instead of arguing for powerlessness and passivity, one should argue that the power of the INDUSTRIAL SYSTEM +should be broken, and that this will greatly INCREASE the power and freedom of INDIVIDUALS and SMALL GROUPS. +

    + +

    200. Until the industrial system has been thoroughly wrecked, the destruction of that system must be the revolutionaries’ +ONLY goal. Other goals would distract attention and energy from the main goal. More importantly if the revolutionaries +permit themselves to have any other goal than the destruction of technology, they will be tempted to use technology as a tool +for reaching that other goal. If they give in to that temptation, they will fall right back into the technological trap, because +modern technology is a unified, tightly organized system, so that, in order to retain SOME technology, one finds oneself +obliged to retain MOST technology, hence one ends up sacrificing only token amounts of technology. +

    + +

    201. Suppose for example that the revolutionaries took “social justice” as a goal. Human nature being what it is, social justice +would not come about spontaneously; it would have to be enforced. In order to enforce it the revolutionaries would have to +retain central organization and control. For that they would need rapid long-distance transportation and communication, and +therefore all the technology needed to support the transportation and communication systems. To feed and clothe poor people +they would have to use agricultural and manufacturing technology. And so forth. So that the attempt to insure social justice +would force them to retain most parts of the technological system. Not that we have anything against social justice, but it must +not be allowed to interfere with the effort to get rid of the technological system. +

    + +

    202. It would be hopeless for revolutionaries to try to attack the system without using SOME modern technology. If nothing +else they must use the communications media to spread their message. But they should use modern technology for only ONE +purpose: to attack the technological system. +

    + +

    203. Imagine an alcoholic sitting with a barrel of wine in front of him. Suppose he starts saying to himself, “Wine isn’t bad for +you if used in moderation. Why, they say small amounts of wine are even good for you! It won’t do me any harm if I take just +one little drink....” Well you know what is going to happen. Never forget that the human race with technology is just like an +alcoholic with a barrel of wine. +

    + +

    204. Revolutionaries should have as many children as they can. There is strong scientific evidence that social attitudes are to a +significant extent inherited. No one suggests that a social attitude is a direct outcome of a person’s genetic constitution, but it +appears that personality traits are partly inherited and that certain personality traits tend, within the context of our society, to +make a person more likely to hold this or that social attitude. Objections to these findings have been raised, but the objections +are feeble and seem to be ideologically motivated. In any event, no one denies that children tend on the average to hold social +attitudes similar to those of their parents. From our point of view it doesn’t matter all that much whether the attitudes are +passed on genetically or through childhood training. In either case they ARE passed on. +

    + +

    205. The trouble is that many of the people who are inclined to rebel against the industrial system are also concerned about the +population problems, hence they are apt to have few or no children. In this way they may be handing the world over to the sort +of people who support or at least accept the industrial system. To insure the strength of the next generation of revolutionaries +the present generation should reproduce itself abundantly. In doing so they will be worsening the population problem only +slightly. And the important problem is to get rid of the industrial system, because once the industrial system is gone the world’s +population necessarily will decrease (see paragraph 167); whereas, if the industrial system survives, it will continue developing +new techniques of food production that may enable the world’s population to keep increasing almost indefinitely. +

    + +

    206. With regard to revolutionary strategy, the only points on which we absolutely insist are that the single overriding goal +must be the elimination of modern technology, and that no other goal can be allowed to compete with this one. For the rest, +revolutionaries should take an empirical approach. If experience indicates that some of the recommendations made in the +foregoing paragraphs are not going to give good results, then those recommendations should be discarded. +

    + +

    Two Kinds of Technology

    + +

    207. An argument likely to be raised against our proposed revolution is that it is bound to fail, because (it is claimed) +throughout history technology has always progressed, never regressed, hence technological regression is impossible. But this +claim is false. +

    + +

    208. We distinguish between two kinds of technology, which we will call small-scale technology and organization-dependent +technology. Small-scale technology is technology that can be used by small-scale communities without outside assistance. +Organization-dependent technology is technology that depends on large-scale social organization. We are aware of no +significant cases of regression in small-scale technology. But organization-dependent technology DOES regress when the +social organization on which it depends breaks down. Example: When the Roman Empire fell apart the Romans’ small-scale +technology survived because any clever village craftsman could build, for instance, a water wheel, any skilled smith could +make steel by Roman methods, and so forth. But the Romans’ organization- dependent technology DID regress. Their +aqueducts fell into disrepair and were never rebuilt. Their techniques of road construction were lost. The Roman system of +urban sanitation was forgotten, so that not until rather recent times did the sanitation of European cities equal that of Ancient +Rome. +

    + +

    209. The reason why technology has seemed always to progress is that, until perhaps a century or two before the Industrial +Revolution, most technology was small-scale technology. But most of the technology developed since the Industrial +Revolution is organization-dependent technology. Take the refrigerator for example. Without factory-made parts or the +facilities of a postindustrial machine shop it would be virtually impossible for a handful of local craftsmen to build a +refrigerator. If by some miracle they did succeed in building one it would be useless to them without a reliable source of +electric power. So they would have to dam a stream and build a generator. Generators require large amounts of copper wire. +Imagine trying to make that wire without modern machinery. And where would they get a gas suitable for refrigeration? It +would be much easier to build an icehouse or preserve food by drying or picking, as was done before the invention of the +refrigerator. +

    + +

    210. So it is clear that if the industrial system were once thoroughly broken down, refrigeration technology would quickly be +lost. The same is true of other organization-dependent technology. And once this technology had been lost for a generation or +so it would take centuries to rebuild it, just as it took centuries to build it the first time around. Surviving technical books would +be few and scattered. An industrial society, if built from scratch without outside help, can only be built in a series of stages: +You need tools to make tools to make tools to make tools ... . A long process of economic development and progress in social +organization is required. And, even in the absence of an ideology opposed to technology, there is no reason to believe that +anyone would be interested in rebuilding industrial society. The enthusiasm for “progress” is a phenomenon peculiar to the +modern form of society, and it seems not to have existed prior to the 17th century or thereabouts. +

    + +

    211. In the late Middle Ages there were four main civilizations that were about equally “advanced”: Europe, the Islamic world, +India, and the Far East (China, Japan, Korea). Three of those civilizations remained more or less stable, and only Europe +became dynamic. No one knows why Europe became dynamic at that time; historians have their theories but these are only +speculation. At any rate, it is clear that rapid development toward a technological form of society occurs only under special +conditions. So there is no reason to assume that a long-lasting technological regression cannot be brought about. +

    + +

    212. Would society EVENTUALLY develop again toward an industrial-technological form? Maybe, but there is no use in +worrying about it, since we can’t predict or control events 500 or 1,000 years in the future. Those problems must be dealt with +by the people who will live at that time. +

    + +

    The Danger of Leftism

    + +

    213. Because of their need for rebellion and for membership in a movement, leftists or persons of similar psychological type +often are attracted to a rebellious or activist movement whose goals and membership are not initially leftist. The resulting +influx of leftish types can easily turn a non-leftist movement into a leftist one, so that leftist goals replace or distort the original +goals of the movement. +

    + +

    214. To avoid this, a movement that exalts nature and opposes technology must take a resolutely anti-leftist stance and must +avoid all collaboration with leftists. Leftism is in the long run inconsistent with wild nature, with human freedom and with the +elimination of modern technology. Leftism is collectivist; it seeks to bind together the entire world (both nature and the human +race) into a unified whole. But this implies management of nature and of human life by organized society, and it requires +advanced technology. You can’t have a united world without rapid transportation and communication, you can’t make all +people love one another without sophisticated psychological techniques, you can’t have a “planned society” without the +necessary technological base. Above all, leftism is driven by the need for power, and the leftist seeks power on a collective +basis, through identification with a mass movement or an organization. Leftism is unlikely ever to give up technology, because +technology is too valuable a source of collective power. +

    + +

    215. The anarchist [34] too seeks power, but he seeks it on an individual or small-group basis; he wants individuals and small +groups to be able to control the circumstances of their own lives. He opposes technology because it makes small groups +dependent on large organizations. +

    + +

    216. Some leftists may seem to oppose technology, but they will oppose it only so long as they are outsiders and the +technological system is controlled by non-leftists. If leftism ever becomes dominant in society, so that the technological system +becomes a tool in the hands of leftists, they will enthusiastically use it and promote its growth. In doing this they will be +repeating a pattern that leftism has shown again and again in the past. When the Bolsheviks in Russia were outsiders, they +vigorously opposed censorship and the secret police, they advocated self-determination for ethnic minorities, and so forth; but +as soon as they came into power themselves, they imposed a tighter censorship and created a more ruthless secret police than +any that had existed under the tsars, and they oppressed ethnic minorities at least as much as the tsars had done. In the United +States, a couple of decades ago when leftists were a minority in our universities, leftist professors were vigorous proponents of +academic freedom, but today, in those of our universities where leftists have become dominant, they have shown themselves +ready to take away from everyone else’s academic freedom. (This is “political correctness.”) The same will happen with leftists +and technology: They will use it to oppress everyone else if they ever get it under their own control. +

    + +

    217. In earlier revolutions, leftists of the most power-hungry type, repeatedly, have first cooperated with non-leftist +revolutionaries, as well as with leftists of a more libertarian inclination, and later have double-crossed them to seize power for +themselves. Robespierre did this in the French Revolution, the Bolsheviks did it in the Russian Revolution, the communists did +it in Spain in 1938 and Castro and his followers did it in Cuba. Given the past history of leftism, it would be utterly foolish for +non-leftist revolutionaries today to collaborate with leftists. +

    + +

    218. Various thinkers have pointed out that leftism is a kind of religion. Leftism is not a religion in the strict sense because +leftist doctrine does not postulate the existence of any supernatural being. But, for the leftist, leftism plays a psychological role +much like that which religion plays for some people. The leftist NEEDS to believe in leftism; it plays a vital role in his +psychological economy. His beliefs are not easily modified by logic or facts. He has a deep conviction that leftism is morally +Right with a capital R, and that he has not only a right but a duty to impose leftist morality on everyone. (However, many of +the people we are referring to as “leftists” do not think of themselves as leftists and would not describe their system of beliefs +as leftism. We use the term “leftism” because we don’t know of any better words to designate the spectrum of related creeds +that includes the feminist, gay rights, political correctness, etc. movements, and because these movements have a strong +affinity with the old left. See paragraphs 227-230.) +

    + +

    219. Leftism is a totalitarian force. Wherever leftism is in a position of power it tends to invade every private corner and force +every thought into a leftist mold. In part this is because of the quasi-religious character of leftism; everything contrary to leftist +beliefs represents Sin. More importantly, leftism is a totalitarian force because of the leftists’ drive for power. The leftist seeks +to satisfy his need for power through identification with a social movement and he tries to go through the power process by +helping to pursue and attain the goals of the movement (see paragraph 83). But no matter how far the movement has gone in +attaining its goals the leftist is never satisfied, because his activism is a surrogate activity (see paragraph 41). That is, the +leftist’s real motive is not to attain the ostensible goals of leftism; in reality he is motivated by the sense of power he gets from +struggling for and then reaching a social goal. [35] Consequently the leftist is never satisfied with the goals he has already +attained; his need for the power process leads him always to pursue some new goal. The leftist wants equal opportunities for +minorities. When that is attained he insists on statistical equality of achievement by minorities. And as long as anyone harbors +in some corner of his mind a negative attitude toward some minority, the leftist has to re-educate him. And ethnic minorities +are not enough; no one can be allowed to have a negative attitude toward homosexuals, disabled people, fat people, old people, +ugly people, and on and on and on. It’s not enough that the public should be informed about the hazards of smoking; a warning +has to be stamped on every package of cigarettes. Then cigarette advertising has to be restricted if not banned. The activists +will never be satisfied until tobacco is outlawed, and after that it will be alcohol, then junk food, etc. Activists have fought +gross child abuse, which is reasonable. But now they want to stop all spanking. When they have done that they will want to ban +something else they consider unwholesome, then another thing and then another. They will never be satisfied until they have +complete control over all child rearing practices. And then they will move on to another cause. +

    + +

    220. Suppose you asked leftists to make a list of ALL the things that were wrong with society, and then suppose you instituted +EVERY social change that they demanded. It is safe to say that within a couple of years the majority of leftists would find +something new to complain about, some new social “evil” to correct because, once again, the leftist is motivated less by +distress at society’s ills than by the need to satisfy his drive for power by imposing his solutions on society. +

    + +

    221. Because of the restrictions placed on their thoughts and behavior by their high level of socialization, many leftists of the +oversocialized type cannot pursue power in the ways that other people do. For them the drive for power has only one morally +acceptable outlet, and that is in the struggle to impose their morality on everyone. +

    + +

    222. Leftists, especially those of the oversocialized type, are True Believers in the sense of Eric Hoffer’s book, “The True +Believer.” But not all True Believers are of the same psychological type as leftists. Presumably a true-believing Nazi, for +instance, is very different psychologically from a true-believing leftist. Because of their capacity for single-minded devotion to +a cause, True Believers are a useful, perhaps a necessary, ingredient of any revolutionary movement. This presents a problem +with which we must admit we don’t know how to deal. We aren’t sure how to harness the energies of the True Believer to a +revolution against technology. At present all we can say is that no True Believer will make a safe recruit to the revolution +unless his commitment is exclusively to the destruction of technology. If he is committed also to another ideal, he may want to +use technology as a tool for pursuing that other ideal (see paragraphs 220, 221). +

    + +

    223. Some readers may say, “This stuff about leftism is a lot of crap. I know John and Jane who are leftish types and they don’t +have all these totalitarian tendencies.” It’s quite true that many leftists, possibly even a numerical majority, are decent people +who sincerely believe in tolerating others’ values (up to a point) and wouldn’t want to use high-handed methods to reach their +social goals. Our remarks about leftism are not meant to apply to every individual leftist but to describe the general character of +leftism as a movement. And the general character of a movement is not necessarily determined by the numerical proportions of +the various kinds of people involved in the movement. +

    + +

    224. The people who rise to positions of power in leftist movements tend to be leftists of the most power-hungry type, because +power-hungry people are those who strive hardest to get into positions of power. Once the power-hungry types have captured +control of the movement, there are many leftists of a gentler breed who inwardly disapprove of many of the actions of the +leaders, but cannot bring themselves to oppose them. They NEED their faith in the movement, and because they cannot give up +this faith they go along with the leaders. True, SOME leftists do have the guts to oppose the totalitarian tendencies that emerge, +but they generally lose, because the power-hungry types are better organized, are more ruthless and Machiavellian and have +taken care to build themselves a strong power base. +

    + +

    225. These phenomena appeared clearly in Russia and other countries that were taken over by leftists. Similarly, before the +breakdown of communism in the, USSR, leftish types in the West would, seldom criticize that country. If prodded they would +admit that the USSR did many wrong things, but then they would try to find excuses for the communists and begin talking +about the faults of the West. They always opposed Western military resistance to communist aggression. Leftish types all over +the world vigorously protested the U.S. military action in Vietnam, but when the USSR invaded Afghanistan they did nothing. +Not that they approved of the Soviet actions; but because of their leftist faith, they just couldn’t bear to put themselves in +opposition to communism. Today, in those of our universities where “political correctness” has become dominant, there are +probably many leftish types who privately disapprove of the suppression of academic freedom, but they go along with it +anyway. +

    + +

    226. Thus the fact that many individual leftists are personally mild and fairly tolerant people by no means prevents leftism as a +whole form having a totalitarian tendency. +

    + +

    227. Our discussion of leftism has a serious weakness. It is still far from clear what we mean by the word “leftist.” There +doesn’t seem to be much we can do about this. Today leftism is fragmented into a whole spectrum of activist movements. Yet +not all activist movements are leftist, and some activist movements (e.g. radical environmentalism) seem to include both +personalities of the leftist type and personalities of thoroughly un-leftist types who ought to know better than to collaborate +with leftists. Varieties of leftists fade out gradually into varieties of non-leftists and we ourselves would often be hard-pressed +to decide whether a given individual is or is not a leftist. To the extent that it is defined at all, our conception of leftism is +defined by the discussion of it that we have given in this article, and we can only advise the reader to use his own judgment in +deciding who is a leftist. +

    + +

    228. But it will be helpful to list some criteria for diagnosing leftism. These criteria cannot be applied in a cut and dried +manner. Some individuals may meet some of the criteria without being leftists, some leftists may not meet any of the criteria. +Again, you just have to use your judgment. +

    + +

    229. The leftist is oriented toward large-scale collectivism. He emphasizes the duty of the individual to serve society and the +duty of society to take care of the individual. He has a negative attitude toward individualism. He often takes a moralistic tone. +He tends to be for gun control, for sex education and other psychologically “enlightened” educational methods, for social +planning, for affirmative action, for multiculturalism. He tends to identify with victims. He tends to be against competition +and against violence, but he often finds excuses for those leftists who do commit violence. He is fond of using the common +catch-phrases of the left, like “racism,” “sexism,” “homophobia,” “capitalism,” “imperialism,” “neocolonialism,” “genocide,” +“social change,” “social justice,” “social responsibility.” Maybe the best diagnostic trait of the leftist is his tendency to sympathize with the following movements: feminism, gay rights, ethnic rights, disability rights, animal rights, political +correctness. Anyone who strongly sympathizes with ALL of these movements is almost certainly a leftist. [36] +

    + +

    230. The more dangerous leftists, that is, those who are most power-hungry, are often characterized by arrogance or by a +dogmatic approach to ideology. However, the most dangerous leftists of all may be certain oversocialized types who avoid +irritating displays of aggressiveness and refrain from advertising their leftism, but work quietly and unobtrusively to promote +collectivist values, “enlightened” psychological techniques for socializing children, dependence of the individual on the +system, and so forth. These crypto-leftists (as we may call them) approximate certain bourgeois types as far as practical action +is concerned, but differ from them in psychology, ideology and motivation. The ordinary bourgeois tries to bring people under +control of the system in order to protect his way of life, or he does so simply because his attitudes are conventional. The +crypto-leftist tries to bring people under control of the system because he is a True Believer in a collectivist ideology. The +crypto-leftist is differentiated from the average leftist of the oversocialized type by the fact that his rebellious impulse is +weaker and he is more securely socialized. He is differentiated from the ordinary well-socialized bourgeois by the fact that +there is some deep lack within him that makes it necessary for him to devote himself to a cause and immerse himself in a +collectivity. And maybe his (well-sublimated) drive for power is stronger than that of the average bourgeois. + +

    + +

    Final Note

    + +

    231. Throughout this article we’ve made imprecise statements and statements that ought to have had all sorts of qualifications +and reservations attached to them; and some of our statements may be flatly false. Lack of sufficient information and the need +for brevity made it impossible for us to formulate our assertions more precisely or add all the necessary qualifications. And of +course in a discussion of this kind one must rely heavily on intuitive judgment, and that can sometimes be wrong. So we don’t +claim that this article expresses more than a crude approximation to the truth. +

    + +

    232. All the same, we are reasonably confident that the general outlines of the picture we have painted here are roughly correct. +Just one possible weak point needs to be mentioned. We have portrayed leftism in its modern form as a phenomenon peculiar +to our time and as a symptom of the disruption of the power process. But we might possibly be wrong about this. +Oversocialized types who try to satisfy their drive for power by imposing their morality on everyone have certainly been +around for a long time. But we THINK that the decisive role played by feelings of inferiority, low self-esteem, powerlessness, +identification with victims by people who are not themselves victims, is a peculiarity of modern leftism. Identification with +victims by people not themselves victims can be seen to some extent in 19th century leftism and early Christianity but as far as +we can make out, symptoms of low self-esteem, etc., were not nearly so evident in these movements, or in any other +movements, as they are in modern leftism. But we are not in a position to assert confidently that no such movements have +existed prior to modern leftism. This is a significant question to which historians ought to give their attention. +

    + +

    Notes

    + +
      +
    1. 1. (Paragraph 19) We are asserting that ALL, or even most, bullies and ruthless competitors suffer from feelings of inferiority. +

    2. + +
    3. 2. (Paragraph 25) During the Victorian period many oversocialized people suffered from serious psychological problems as a +result of repressing or trying to repress their sexual feelings. Freud apparently based his theories on people of this type. Today +the focus of socialization has shifted from sex to aggression. +

    4. + +
    5. 3. (Paragraph 27) Not necessarily including specialists in engineering or the “hard” sciences. +

    6. + +
    7. 4. (Paragraph 28) There are many individuals of the middle and upper classes who resist some of these values, but usually their +resistance is more or less covert. Such resistance appears in the mass media only to a very limited extent. The main thrust of +propaganda in our society is in favor of the stated values. The main reason why these values have become, so to speak, the +official values of our society is that they are useful to the industrial system. Violence is discouraged because it disrupts the +functioning of the system. Racism is discouraged because ethnic conflicts also disrupt the system, and discrimination wastes +the talents of minority-group members who could be useful to the system. Poverty must be “cured” because the underclass +causes problems for the system and contact with the underclass lowers the morale of the other classes. Women are encouraged +to have careers because their talents are useful to the system and, more importantly, because by having regular jobs women +become better integrated into the system and tied directly to it rather than to their families. This helps to weaken family solidarity. (The leaders of the system say they want to strengthen the family, but they really mean is that they want the family +to serve as an effective tool for socializing children in accord with the needs of the system. We argue in paragraphs 51, 52 that +the system cannot afford to let the family or other small-scale social groups be strong or autonomous.) +

    8. + +
    9. 5. (Paragraph 42) It may be argued that the majority of people don’t want to make their own decisions but want leaders to do +their thinking for them. There is an element of truth in this. People like to make their own decisions in small matters, but +making decisions on difficult, fundamental questions requires facing up to psychological conflict, and most people hate +psychological conflict. Hence they tend to lean on others in making difficult decisions. But it does not follow that they like to +have decisions imposed upon them without having any opportunity to influence those decisions. The majority of people are +natural followers, not leaders, but they like to have direct personal access to their leaders, they want to be able to influence the +leaders and participate to some extent in making even the difficult decisions. At least to that degree they need autonomy. +

    10. + +
    11. 6. (Paragraph 44) Some of the symptoms listed are similar to those shown by caged animals. To explain how these symptoms +arise from deprivation with respect to the power process: common-sense understanding of human nature tells one that lack of +goals whose attainment requires effort leads to boredom and that boredom, long continued, often leads eventually to +depression. Failure to attain goals leads to frustration and lowering of self-esteem. Frustration leads to anger, anger to +aggression, often in the form of spouse or child abuse. It has been shown that long-continued frustration commonly leads to +depression and that depression tends to cause guilt, sleep disorders, eating disorders and bad feelings about oneself. Those who +are tending toward depression seek pleasure as an antidote; hence insatiable hedonism and excessive sex, with perversions as a +means of getting new kicks. Boredom too tends to cause excessive pleasure-seeking since, lacking other goals, people often use +pleasure as a goal. See accompanying diagram. +The foregoing is a simplification. Reality is more complex, and of course, deprivation with respect to the power process is not +the ONLY cause of the symptoms described. By the way, when we mention depression we do not necessarily mean depression +that is severe enough to be treated by a psychiatrist. Often only mild forms of depression are involved. And when we speak of +goals we do not necessarily mean long-term, thought-out goals. For many or most people through much of human history, the +goals of a hand-to-mouth existence (merely providing oneself and one’s family with food from day to day) have been quite +sufficient. +

    12. + +
    13. 7. (Paragraph 52) A partial exception may be made for a few passive, inward-looking groups, such as the Amish, which have +little effect on the wider society. Apart from these, some genuine small-scale communities do exist in America today. For +instance, youth gangs and “cults.” Everyone regards them as dangerous, and so they are, because the members of these groups +are loyal primarily to one another rather than to the system, hence the system cannot control them. Or take the gypsies. The +gypsies commonly get away with theft and fraud because their loyalties are such that they can always get other gypsies to give +testimony that “proves” their innocence. Obviously the system would be in serious trouble if too many people belonged to such +groups. Some of the early-20th century Chinese thinkers who were concerned with modernizing China recognized the +necessity breaking down small-scale social groups such as the family: “(According to Sun Yat-sen) the Chinese people needed +a new surge of patriotism, which would lead to a transfer of loyalty from the family to the state.... (According to Li Huang) +traditional attachments, particularly to the family had to be abandoned if nationalism were to develop in China.” (Chester C. +Tan, “Chinese Political Thought in the Twentieth Century,” page 125, page 297.) +

    14. + +
    15. 8. (Paragraph 56) Yes, we know that 19th century America had its problems, and serious ones, but for the sake of brevity we +have to express ourselves in simplified terms. +

    16. + +
    17. 9. (Paragraph 61) We leave aside the “underclass.” We are speaking of the mainstream. +

    18. + +
    19. 10. (Paragraph 62) Some social scientists, educators, “mental health” professionals and the like are doing their best to push the +social drives into group 1 by trying to see to it that everyone has a satisfactory social life. +

    20. + +
    21. 11. (Paragraphs 63, 82) Is the drive for endless material acquisition really an artificial creation of the advertising and marketing +industry? Certainly there is no innate human drive for material acquisition. There have been many cultures in which people +have desired little material wealth beyond what was necessary to satisfy their basic physical needs (Australian aborigines, +traditional Mexican peasant culture, some African cultures). On the other hand there have also been many pre-industrial +cultures in which material acquisition has played an important role. So we can’t claim that today’s acquisition-oriented culture +is exclusively a creation of the advertising and marketing industry. But it is clear that the advertising and marketing industry +has had an important part in creating that culture. The big corporations that spend millions on advertising wouldn’t be spending +that kind of money without solid proof that they were getting it back in increased sales. One member of FC met a sales +manager a couple of years ago who was frank enough to tell him, “Our job is to make people buy things they don’t want and +don’t need.” He then described how an untrained novice could present people with the facts about a product, and make no sales at all, while a trained and experienced professional salesman would make lots of sales to the same people. This shows that +people are manipulated into buying things they don’t really want. +

    22. + +
    23. 12. (Paragraph 64) The problem of purposelessness seems to have become less serious during the last 15 years or so, because +people now feel less secure physically and economically than they did earlier, and the need for security provides them with a +goal. But purposelessness has been replaced by frustration over the difficulty of attaining security. We emphasize the problem +of purposelessness because the liberals and leftists would wish to solve our social problems by having society guarantee +everyone’s security; but if that could be done it would only bring back the problem of purposelessness. The real issue is not +whether society provides well or poorly for people’s security; the trouble is that people are dependent on the system for their +security rather than having it in their own hands. This, by the way, is part of the reason why some people get worked up about +the right to bear arms; possession of a gun puts that aspect of their security in their own hands. +

    24. + +
    25. 13. (Paragraph 66) Conservatives’ efforts to decrease the amount of government regulation are of little benefit to the average +man. For one thing, only a fraction of the regulations can be eliminated because most regulations are necessary. For another +thing, most of the deregulation affects business rather than the average individual, so that its main effect is to take power from +the government and give it to private corporations. What this means for the average man is that government interference in his +life is replaced by interference from big corporations, which may be permitted, for example, to dump more chemicals that get +into his water supply and give him cancer. The conservatives are just taking the average man for a sucker, exploiting his +resentment of Big Government to promote the power of Big Business. +

    26. + +
    27. 14. (Paragraph 73) When someone approves of the purpose for which propaganda is being used in a given case, he generally +calls it “education” or applies to it some similar euphemism. But propaganda is propaganda regardless of the purpose for which +it is used. +

    28. + +
    29. 15. (Paragraph 83) We are not expressing approval or disapproval of the Panama invasion. We only use it to illustrate a point. +

    30. + +
    31. 16. (Paragraph 95) When the American colonies were under British rule there were fewer and less effective legal guarantees of +freedom than there were after the American Constitution went into effect, yet there was more personal freedom in preindustrial America, both before and after the War of Independence, than there was after the Industrial Revolution took hold in +this country. We quote from “Violence in America: Historical and Comparative Perspectives,” edited by Hugh Davis Graham +and Ted Robert Gurr, Chapter 12 by Roger Lane, pages 476-478: “The progressive heightening of standards of propriety, and +with it the increasing reliance on official law enforcement (in l9th century America) ... were common to the whole society.... +The change in social behavior is so long term and so widespread as to suggest a connection with the most fundamental of +contemporary social processes; that of industrial urbanization itself....”Massachusetts in 1835 had a population of some +660,940, 81 percent rural, overwhelmingly pre-industrial and native born. It’s citizens were used to considerable personal +freedom. Whether teamsters, farmers or artisans, they were all accustomed to setting their own schedules, and the nature of +their work made them physically independent of each other.... Individual problems, sins or even crimes, were not generally +cause for wider social concern....” But the impact of the twin movements to the city and to the factory, both just gathering force +in 1835, had a progressive effect on personal behavior throughout the 19th century and into the 20th. The factory demanded +regularity of behavior, a life governed by obedience to the rhythms of clock and calendar, the demands of foreman and +supervisor. In the city or town, the needs of living in closely packed neighborhoods inhibited many actions previously +unobjectionable. Both blue- and white-collar employees in larger establishments were mutually dependent on their fellows; as +one man’s work fit into anther’s, so one man’s business was no longer his own. “The results of the new organization of life and +work were apparent by 1900, when some 76 percent of the 2,805,346 inhabitants of Massachusetts were classified as urbanites. +Much violent or irregular behavior which had been tolerable in a casual, independent society was no longer acceptable in the +more formalized, cooperative atmosphere of the later period.... The move to the cities had, in short, produced a more tractable, +more socialized, more ‘civilized’ generation than its predecessors.” +If copyright problems make it impossible for this long quotation to be printed, then please change Note 16 to read as follows: 16. (Paragraph 95) When the American colonies were under British rule there were fewer and less effective legal guarantees of +freedom than there were after the American Constitution went into effect, yet there was more personal freedom in preindustrial America, both before and after the War of Independence, than there was after the Industrial Revolution took hold in +this country. In “Violence in America: Historical and Comparative Perspectives,” edited by Hugh Davis Graham and Ted +Robert Gurr, Chapter 12 by Roger Lane, it is explained how in pre-industrial America the average person had greater +independence and autonomy than he does today, and how the process of industrialization necessarily led to the restriction of +personal freedom. +

    32. + +
    33. 17. (Paragraph 117) Apologists for the system are fond of citing cases in which elections have been decided by one or two +votes, but such cases are rare. +

    34. + +
    35. 18. (Paragraph 119) “Today, in technologically advanced lands, men live very similar lives in spite of geographical, religious, +and political differences. The daily lives of a Christian bank clerk in Chicago, a Buddhist bank clerk in Tokyo, and a +Communist bank clerk in Moscow are far more alike than the life of any one of them is like that of any single man who lived a +thousand years ago. These similarities are the result of a common technology....” L. Sprague de Camp, “The Ancient +Engineers,” Ballantine edition, page 17. The lives of the three bank clerks are not IDENTICAL. Ideology does have SOME +effect. But all technological societies, in order to survive, must evolve along APPROXIMATELY the same trajectory. +

    36. + +
    37. 19. (Paragraph 123) Just think an irresponsible genetic engineer might create a lot of terrorists. +

    38. + +
    39. 20. (Paragraph 124) For a further example of undesirable consequences of medical progress, suppose a reliable cure for cancer +is discovered. Even if the treatment is too expensive to be available to any but the elite, it will greatly reduce their incentive to +stop the escape of carcinogens into the environment. +

    40. + +
    41. 21. (Paragraph 128) Since many people may find paradoxical the notion that a large number of good things can add up to a bad +thing, we illustrate with an analogy. Suppose Mr. A is playing chess with Mr. B. Mr. C, a Grand Master, is looking over Mr. +A’s shoulder. Mr. A of course wants to win his game, so if Mr. C points out a good move for him to make, he is doing Mr. A a +favor. But suppose now that Mr. C tells Mr. A how to make ALL of his moves. In each particular instance he does Mr. A a +favor by showing him his best move, but by making ALL of his moves for him he spoils his game, since there is not point in +Mr. A’s playing the game at all if someone else makes all his moves. The situation of modern man is analogous to that of Mr. +A. The system makes an individual’s life easier for him in innumerable ways, but in doing so it deprives him of control over +his own fate. +

    42. + +
    43. 22. (Paragraph 137) Here we are considering only the conflict of values within the mainstream. For the sake of simplicity we +leave out of the picture “outsider” values like the idea that wild nature is more important than human economic welfare. +

    44. + +
    45. 23. (Paragraph 137) Self-interest is not necessarily MATERIAL self-interest. It can consist in fulfillment of some +psychological need, for example, by promoting one’s own ideology or religion. +

    46. + +
    47. 24. (Paragraph 139) A qualification: It is in the interest of the system to permit a certain prescribed degree of freedom in some +areas. For example, economic freedom (with suitable limitations and restraints) has proved effective in promoting economic +growth. But only planned, circumscribed, limited freedom is in the interest of the system. The individual must always be kept +on a leash, even if the leash is sometimes long (see paragraphs 94, 97). +

    48. + +
    49. 25. (Paragraph 143) We don’t mean to suggest that the efficiency or the potential for survival of a society has always been +inversely proportional to the amount of pressure or discomfort to which the society subjects people. That certainly is not the +case. There is good reason to believe that many primitive societies subjected people to less pressure than European society did, +but European society proved far more efficient than any primitive society and always won out in conflicts with such societies +because of the advantages conferred by technology. +

    50. + +
    51. 26. (Paragraph 147) If you think that more effective law enforcement is unequivocally good because it suppresses crime, then +remember that crime as defined by the system is not necessarily what YOU would call crime. Today, smoking marijuana is a +“crime,” and, in some places in the U.S., so is possession of an unregistered handgun. Tomorrow, possession of ANY firearm, +registered or not, may be made a crime, and the same thing may happen with disapproved methods of child-rearing, such as +spanking. In some countries, expression of dissident political opinions is a crime, and there is no certainty that this will never +happen in the U.S., since no constitution or political system lasts forever. If a society needs a large, powerful law enforcement +establishment, then there is something gravely wrong with that society; it must be subjecting people to severe pressures if so +many refuse to follow the rules, or follow them only because forced. Many societies in the past have gotten by with little or no +formal law-enforcement. +

    52. + +
    53. 27. (Paragraph 151) To be sure, past societies have had means of influencing human behavior, but these have been primitive +and of low effectiveness compared with the technological means that are now being developed. +

    54. + +
    55. 28. (Paragraph 152) However, some psychologists have publicly expressed opinions indicating their contempt for human +freedom. And the mathematician Claude Shannon was quoted in Omni (August 1987) as saying, “I visualize a time when we +will be to robots what dogs are to humans, and I’m rooting for the machines.” +

    56. + +
    57. 29. (Paragraph 154) This is no science fiction! After writing paragraph 154 we came across an article in Scientific American +according to which scientists are actively developing techniques for identifying possible future criminals and for treating them +by a combination of biological and psychological means. Some scientists advocate compulsory application of the treatment, +which may be available in the near future. (See “Seeking the Criminal Element,” by W. Wayt Gibbs, Scientific American, +March 1995.) Maybe you think this is OK because the treatment would be applied to those who might become violent +criminals. But of course it won’t stop there. Next, a treatment will be applied to those who might become drunk drivers (they +endanger human life too), then perhaps to peel who spank their children, then to environmentalists who sabotage logging +equipment, eventually to anyone whose behavior is inconvenient for the system. +

    58. + +
    59. 30. (Paragraph 184) A further advantage of nature as a counter-ideal to technology is that, in many people, nature inspires the +kind of reverence that is associated with religion, so that nature could perhaps be idealized on a religious basis. It is true that in +many societies religion has served as a support and justification for the established order, but it is also true that religion has +often provided a basis for rebellion. Thus it may be useful to introduce a religious element into the rebellion against +technology, the more so because Western society today has no strong religious foundation. Religion, nowadays either is used +as cheap and transparent support for narrow, short-sighted selfishness (some conservatives use it this way), or even is cynically +exploited to make easy money (by many evangelists), or has degenerated into crude irrationalism (fundamentalist protestant +sects, “cults”), or is simply stagnant (Catholicism, main-line Protestantism). The nearest thing to a strong, widespread, dynamic +religion that the West has seen in recent times has been the quasi-religion of leftism, but leftism today is fragmented and has no +clear, unified, inspiring goal. Thus there is a religious vacuum in our society that could perhaps be filled by a religion focused +on nature in opposition to technology. But it would be a mistake to try to concoct artificially a religion to fill this role. Such an +invented religion would probably be a failure. Take the “Gaia” religion for example. Do its adherents REALLY believe in it or +are they just play-acting? If they are just play-acting their religion will be a flop in the end. It is probably best not to try to +introduce religion into the conflict of nature vs. technology unless you REALLY believe in that religion yourself and find that +it arouses a deep, strong, genuine response in many other people. +

    60. + +
    61. 31. (Paragraph 189) Assuming that such a final push occurs. Conceivably the industrial system might be eliminated in a +somewhat gradual or piecemeal fashion (see paragraphs 4, 167 and Note 4). +

    62. + +
    63. 32. (Paragraph 193) It is even conceivable (remotely) that the revolution might consist only of a massive change of attitudes +toward technology resulting in a relatively gradual and painless disintegration of the industrial system. But if this happens we’ll +be very lucky. It’s far more probably that the transition to a non-technological society will be very difficult and full of conflicts +and disasters. +

    64. + +
    65. 33. (Paragraph 195) The economic and technological structure of a society are far more important than its political structure in +determining the way the average man lives (see paragraphs 95, 119 and Notes 16, 18). +

    66. + +
    67. 34. (Paragraph 215) This statement refers to our particular brand of anarchism. A wide variety of social attitudes have been +called “anarchist,” and it may be that many who consider themselves anarchists would not accept our statement of paragraph 215. +It should be noted, by the way, that there is a nonviolent anarchist movement whose members probably would not accept +FC as anarchist and certainly would not approve of FC’s violent methods. +

    68. + +
    69. 35. (Paragraph 219) Many leftists are motivated also by hostility, but the hostility probably results in part from a frustrated +need for power. +

    70. + +
    71. 36. (Paragraph 229) It is important to understand that we mean someone who sympathizes with these MOVEMENTS as they +exist today in our society. One who believes that women, homosexuals, etc., should have equal rights is not necessary a leftist. +The feminist, gay rights, etc., movements that exist in our society have the particular ideological tone that characterizes leftism, +and if one believes, for example, that women should have equal rights it does not necessarily follow that one must sympathize +with the feminist movement as it exists today. +

    72. +
    + +

    My Footnotes

    + +

    A1. Paragraph 96 +FC is "Freedom Club"; Kaczynski used this to refer to himself. +

    + +

    A2. (Paragraph 116) A scan (slideshow of images) of the original manuscript is available at: https://archive.org/details/fc.manifesto/page/n107/mode/2up

    + + +
    +
    +
    + + diff --git a/_site/links/index.html b/_site/links/index.html new file mode 100644 index 0000000..a5d1f11 --- /dev/null +++ b/_site/links/index.html @@ -0,0 +1,335 @@ + + + + + Links | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    + +
    +
    +

    Find Me Other Places

    +
    + + + + + + + + + + + + + + + +
    + +
    +
    +

    Secure Service Links

    +
    + + + + + + + +
    + +
    +
    +

    Linux Links

    +
    + + + + + + + + + +
    + +
    +
    +

    Interesting Links

    +
    + + + + + + + +
    + +
    +
    +

    Inspiration Comes From

    +
    + + + +
    + +
    +
    +

    The "This Is A Website" Series

    +
    + + + + + + + +
    + +
    +
    +

    Friends' Websites

    +
    + + + +
    + +

    All my big projects are on my homepage.

    + +
    +
    + +
    + + diff --git a/_site/md/index.html b/_site/md/index.html new file mode 100644 index 0000000..c7af828 --- /dev/null +++ b/_site/md/index.html @@ -0,0 +1,73 @@ + + + + + Simple Markdown Converter w/ MathML Support | tait.tech + + + + + + + + + + + + + + + + + + + +
    +
    + +

    Tait’s Simple Markdown Tool

    + +

    Info

    + +
      +
    • You can use $...$ for inline math.
    • +
    • You can use $$...$$ for “display mode” math.
    • +
    • Display mode will make the equation take up an entire line for itself, and it will display larger and more “formally”. It also allows you to have newlines without breaking up the expression, and/or the use of \newline to have a multi-line formula.
    • +
    • You can save the HTML or markdown for future use.
    • +
    • You can download the HTML or markdown for sharing with others.
    • +
    • Saving will save it to your browser’s local storage so when you load the page again–even after a reboot–it will be pre-populated with what you saved last.
    • +
    • Converting to HTML automatically saves both the markdown and the HTML.
    • +
    • This page is fully functional without an internet connection. Ctrl+s to save the page with its CSS and Javascript and you can use this tool offline.
    • +
    + +

    Editor

    + +

    +OK

    + +

    + + +


    +

    Actions

    + +
      +
    • +
    • +
    • +
    • +
    • +
    + +

    HTML is below this section break:

    + +
    + +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/225/01-intro/index.html b/_site/melody/225/01-intro/index.html new file mode 100644 index 0000000..a301df7 --- /dev/null +++ b/_site/melody/225/01-intro/index.html @@ -0,0 +1,151 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    CMPT 225 - Introduction

    + +

    CMPT: 225, D100 — Data Structures & Programming

    + +

    Graded Work

    + +
      +
    • 4 Assignments, 32% +
        +
      • Programming & Written Components
      • +
      • Programs: +
          +
        • In C++
        • +
        • running on ESIL Linux machines
        • +
        • using command line tools
        • +
        +
      • +
      +
    • +
    • 3 Tests, 33% +
        +
      • In class
      • +
      +
    • +
    • 1 Final Exam, 35%
    • +
    + +

    Lectures

    + +
      +
    • Slides + audio recording will be posted after lectures.
    • +
    + +

    Labs

    + +
      +
    • There are help times (first labs are next week)
    • +
    + +

    Instructor Office Hours

    + +
      +
    • Will be primarily online — times TBA.
    • +
    + +

    Instructor Contact

    + +
      +
    • Via Canvas or email (for email: write from your SFU account, use subject “225: …”)
    • +
    + +

    Some Basic Rules

    + +
      +
    • In class, do not distract others.
    • +
    • In class, no taking photos/videos/recording
    • +
    • Help each other, but submit your own work.
    • +
    • Masks are mandatory in class: +
        +
      • may be removed while asking a question
      • +
      • if you cannot wear a mask, please email me
      • +
      +
    • +
    • Please stay on “your” side of the table +
        +
      • and stay 6’ away if you don’t have a mask on
      • +
      +
    • +
    + +

    Course Content

    + +
      +
    • Algorithms: processes that operate on data
    • +
    • Programs: implementations of algorithms
    • +
    • Data in programs: stored in data structures +
        +
      • simple D.S.: variables
      • +
      • most algorithms required compound D.S.s: e.g. arrays, lists, …
      • +
      • most non-trivial applications require non-trivial D.S.s
      • +
      +
    • +
    • Data Type: collection of values + operations on these values
    • +
    • Multiple data types: +
        +
      • Cleaner/simpler slg. design
      • +
      • better code
      • +
      +
    • +
    • Abstract Data Type: defined by values + operations without reference to how things are implemented
    • +
    • ADTs provide an abstract view of data: +
        +
      • let us reason about algorithms at a high level, ignoring implementation details
      • +
      +
    • +
    • Good ADT choices reduce program complexity & increase likelihood of success ( which is good).
    • +
    • a[i] vs. a(i)
    • +
    + +

    We will look at:

    +
      +
    • fundamental ADTs
    • +
    • fundamental data structures (to implement them)
    • +
    • sorting algorithms
    • +
    • with attention to: +
        +
      • correctness
      • +
      • efficiency/speed
      • +
      • how to implement
      • +
      • how to choose (for an application)
      • +
      +
    • +
    + +

    Pre-regs:

    + +
      +
    • Basic Programming (e.g. CMPT 125)
    • +
    • Discrete Math (e.g. MACM 101)
    • +
    + +

    (students get these in many different ways)

    + +

    To Do

    + +
      +
    • Read chapters 1 + 3 of text
    • +
    • Lear about CSIL
    • +
    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/225/02-stacks-and-queues/index.html b/_site/melody/225/02-stacks-and-queues/index.html new file mode 100644 index 0000000..4a90449 --- /dev/null +++ b/_site/melody/225/02-stacks-and-queues/index.html @@ -0,0 +1,866 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    Stacks & Queues - 1

    + +

    CMPT 225, Fall 2021, Lecture 2

    + +

    Stack

    + +
      +
    • ADT that stores a collection of objects/values
    • +
    • Insertions and removals follow last-in-first-out pattern
    • +
    • Fundamental Operations: +
        +
      • push: inserts an element on the “top”
      • +
      • pop: removes + returns the top element
      • +
      +
    • +
    + +

    e.g.

    + + + + + + + + +
    Stack indexValue
    + +

    push a

    + + + + + + + + + + + + +
    Stack indexValue
    0a
    + +

    push b

    + + + + + + + + + + + + + + + + +
    Stack indexValue
    0b
    1a
    + +

    push c

    + + + + + + + + + + + + + + + + + + + + +
    Stack indexValue
    0c
    1b
    2a
    + +

    pop

    + + + + + + + + + + + + + + + + +
    Stack indexValue
    0b
    1a
    + +
      +
    • conventient optional operations +
        +
      • size: return # of elements on stack // encapsulation
      • +
      • empty: check for emptiness // better than “size=0?”
      • +
      • top: return top element, but don’t remove it // better than x = pop(); push(x) then use x
      • +
      +
    • +
    + +

    Algorithm Applications

    + +
      +
    • parasing/evaluation/transformation of expressions
    • +
    • speech recognition coding/decoding
    • +
    • shared network access control
    • +
    • programming languages & execution +
        +
      • Postscript, Forth, the call stack
      • +
      +
    • +
    • things we do daily with computers
    • +
    + +

    Parenthasies Checking

    + +

    Parenthasies: (()(((()())()()(()))

    + +

    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.

    + +

    Can be done by counting unmached left parenthasies:

    + +

    Successful example:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
     0
    (1
    (2
    )1
    (2
    (3
    )2
    )1
    )0
    Yay!0
    + +

    Failed example:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCountry
     0
    (1
    (2
    )1
    (2
    )1
     Oh no it’s not zero!
    + +

    Another fail:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
     0
    (1
    )0
    (1
    )0
    )-1
     Negative 1 this time!
    + +

    Do this one yourself:

    + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    ( 
    ) 
    ) 
    ( 
    + +

    But consider multiple grouping symbols: (\{\{), (\{)\{, (\{[( ... ]. +Counting is not enough.

    + +

    Parenthasies Checking

    + +

    Diagram showing failure on the last character of this sequence: (()((()()))()(())))

    + +

    Can be done by counting unmatched left parenthasies:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    (1
    (2
    )1
    (2
    (3
    )2
    )1
    )0
    + +

    Example 2:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    (1
    (2
    )1
    (2
    )1
    + +

    Example 3:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    (1
    )0
    (1
    )0
    )-1
    + +

    Example 4:

    + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    (1
    )0
    )-1
    (0
    + +

    But consider multiple grouping symbols:

    + +
      +
    • sequence (\{\}) will and should work
    • +
    • sequence (\{)\} will work and should not
    • +
    • sequence (\{[( ... ???
    • +
    + +

    Counting is not enough.

    + +

    Stack-based Algorithm for Checking Grouping Symbols

    + +

    Pseudo-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
    +
    + +

    Diagram showing how each set of parenthasies are matched, innermost first torwards the outermost.

    + +

    Stack: Partially-Filled Array Implementation

    + +

    Variables:

    + +
      +
    • A - an array of stack elements
    • +
    • capacity - size of array
    • +
    • top - index in array of top stack element +
        +
      • -1 if stack is empty
      • +
      +
    • +
    + +

    A starts out as:

    + + + + + + + + + + + + + + + + + + + + +
    Index012capacity-1
    Valueabc 
    + +
      +
    • Capacity = <size of A>
    • +
    • top = 2
    • +
    + +

    push e

    + +

    A now equals

    + + + + + + + + + + + + + + + + + + + + + + + + +
    Index01234capacity-1
    Valueabcde 
    + +
      +
    • capacity = <size of A>
    • +
    • top = 3
    • +
    + +

    The Queue ADT

    + +

    A container that stores a collection of items with insertion and removal in “first-in-first-out” order.

    + +

    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.

    + +

    Queue: a diagram showing circles being unqueued in on the left and dequeued on the right. Like waiting in line for food.

    + +

    Basic operations:

    +
      +
    • enqueue: add an item to the back of a list
    • +
    • dequeue: remove & return the item at the front of the list
    • +
    + +

    Example: Palindrome Checking

    + +

    Sequence aba should succeed. +Sequence aab should not.

    + +

    In an array: a diagram showing the following pattern with the variable i set to the leftmost element and j set to the rightmost element.

    + + + + + + + + + + + + + + +
    Valuesabcsba
    + +

    With a stack and queue:

    +
      +
    1. read symbols, starting each in a stack and a queue
    2. +
    3. while the stack, queue are not empty: +
        +
      • pop and element from the stack + delete an element from the queue
      • +
      • if different -> not a palindrome
      • +
      +
    4. +
    5. palindrome
    6. +
    + +

    Queue: Array Implementations

    + +

    Enqueue: a,b,c,d. +Front is now a, last is now d.

    + + + + + + + + + + + + +
    Valuesabcd
    + +

    Dequeue returns and deletes first element. +Front is now b, last is now d.

    + + + + + + + + + + + + +
    Values?bcd
    + +

    Many enquques/dequeues later:

    + + + + + + + + + + + + +
    Valuessmno
    + +

    Front element is now m, last element is now s.

    + + + + + + + + + + + + + +
    Values?tuvw
    + +

    A similar slide is here, but I can’t see the differnece.

    + +

    Variables:

    +
      +
    • array A is array of size capacity
    • +
    • front is 0
    • +
    • back is 0
    • +
    • size is 0
    • +
    • capacity = <size of array>
    • +
    + +

    Pseudo-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 = <size of array> // ???(can't read) queue size supported
    +
    +enqueue(x) { // adds x to the back of the queue
    +  // requires size<capacity>
    +  A[back] = x
    +  size = size + 1
    +  back = (back + 1) % capacity
    +}
    +
    +dequeue() { // removes front element of queue and returns it
    +  // required size > 0
    +  temp = A[front]
    +  front = (front + 1) % capacity
    +  size = size - 1
    +  return temp
    +}
    +
    + +

    Is size == front - back?

    + +

    Diagram showing only true if front and back haven’t been switched around due to wrapping around back to the beginning o the array.

    + +

    “Circular Array” Queue Example

    + +
      +
    • size = 0
    • +
    • front = 0
    • +
    • back = 0
    • +
    + + + + + + + + + + + + + +
    Valuesemptyemptyemptyemptyemptyempty
    + +

    enqueue: a,b,c,d

    + +
      +
    • front = 0
    • +
    • back = 4
    • +
    + + + + + + + + + + + + + +
    Valuesabcdemptyempty
    + +

    dequque 3 times

    + +
      +
    • front = 3
    • +
    • back = 4
    • +
    + + + + + + + + + + + + + +
    Valuesemptyemptyemptydemptyempty
    + +

    enqueue e,f,g,h

    + +
      +
    • front = 3
    • +
    • back = 2
    • +
    + + + + + + + + + + + + + +
    Valuesghemptydef
    + +

    unqueue j

    + +
      +
    • size = 6
    • +
    • font = 3
    • +
    • back = 3
    • +
    + + + + + + + + + + + + + +
    Valuesghjdef
    + +

    dequeue 3 times

    + +
      +
    • back = 3
    • +
    • front = 0
    • +
    + + + + + + + + + + + + + +
    Valuesghjemptyemptyempty
    + +

    dequeue 3 times

    + +
      +
    • back = 3
    • +
    • front = 3
    • +
    + + + + + + + + + + + + + +
    Valuesemptyemptyemptyemptyemptyempty
    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/01/01-intro.html b/_site/melody/cmpt-225/01/01-intro.html new file mode 100644 index 0000000..5c16f35 --- /dev/null +++ b/_site/melody/cmpt-225/01/01-intro.html @@ -0,0 +1,149 @@ + + + + + | tait.tech + + + + +
    +
    +

    CMPT 225 - Introduction

    + +

    CMPT: 225, D100 — Data Structures & Programming

    + +

    Graded Work

    + +
      +
    • 4 Assignments, 32% +
        +
      • Programming & Written Components
      • +
      • Programs: +
          +
        • In C++
        • +
        • running on ESIL Linux machines
        • +
        • using command line tools
        • +
        +
      • +
      +
    • +
    • 3 Tests, 33% +
        +
      • In class
      • +
      +
    • +
    • 1 Final Exam, 35%
    • +
    + +

    Lectures

    + +
      +
    • Slides + audio recording will be posted after lectures.
    • +
    + +

    Labs

    + +
      +
    • There are help times (first labs are next week)
    • +
    + +

    Instructor Office Hours

    + +
      +
    • Will be primarily online — times TBA.
    • +
    + +

    Instructor Contact

    + +
      +
    • Via Canvas or email (for email: write from your SFU account, use subject “225: …”)
    • +
    + +

    Some Basic Rules

    + +
      +
    • In class, do not distract others.
    • +
    • In class, no taking photos/videos/recording
    • +
    • Help each other, but submit your own work.
    • +
    • Masks are mandatory in class: +
        +
      • may be removed while asking a question
      • +
      • if you cannot wear a mask, please email me
      • +
      +
    • +
    • Please stay on “your” side of the table +
        +
      • and stay 6’ away if you don’t have a mask on
      • +
      +
    • +
    + +

    Course Content

    + +
      +
    • Algorithms: processes that operate on data
    • +
    • Programs: implementations of algorithms
    • +
    • Data in programs: stored in data structures +
        +
      • simple D.S.: variables
      • +
      • most algorithms required compound D.S.s: e.g. arrays, lists, …
      • +
      • most non-trivial applications require non-trivial D.S.s
      • +
      +
    • +
    • Data Type: collection of values + operations on these values
    • +
    • Multiple data types: +
        +
      • Cleaner/simpler slg. design
      • +
      • better code
      • +
      +
    • +
    • Abstract Data Type: defined by values + operations without reference to how things are implemented
    • +
    • ADTs provide an abstract view of data: +
        +
      • let us reason about algorithms at a high level, ignoring implementation details
      • +
      +
    • +
    • Good ADT choices reduce program complexity & increase likelihood of success ( which is good).
    • +
    • a[i] vs. a(i)
    • +
    + +

    We will look at:

    +
      +
    • fundamental ADTs
    • +
    • fundamental data structures (to implement them)
    • +
    • sorting algorithms
    • +
    • with attention to: +
        +
      • correctness
      • +
      • efficiency/speed
      • +
      • how to implement
      • +
      • how to choose (for an application)
      • +
      +
    • +
    + +

    Pre-regs:

    + +
      +
    • Basic Programming (e.g. CMPT 125)
    • +
    • Discrete Math (e.g. MACM 101)
    • +
    + +

    (students get these in many different ways)

    + +

    To Do

    + +
      +
    • Read chapters 1 + 3 of text
    • +
    • Lear about CSIL
    • +
    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/01/01-intro/index.html b/_site/melody/cmpt-225/01/01-intro/index.html new file mode 100644 index 0000000..a301df7 --- /dev/null +++ b/_site/melody/cmpt-225/01/01-intro/index.html @@ -0,0 +1,151 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    CMPT 225 - Introduction

    + +

    CMPT: 225, D100 — Data Structures & Programming

    + +

    Graded Work

    + +
      +
    • 4 Assignments, 32% +
        +
      • Programming & Written Components
      • +
      • Programs: +
          +
        • In C++
        • +
        • running on ESIL Linux machines
        • +
        • using command line tools
        • +
        +
      • +
      +
    • +
    • 3 Tests, 33% +
        +
      • In class
      • +
      +
    • +
    • 1 Final Exam, 35%
    • +
    + +

    Lectures

    + +
      +
    • Slides + audio recording will be posted after lectures.
    • +
    + +

    Labs

    + +
      +
    • There are help times (first labs are next week)
    • +
    + +

    Instructor Office Hours

    + +
      +
    • Will be primarily online — times TBA.
    • +
    + +

    Instructor Contact

    + +
      +
    • Via Canvas or email (for email: write from your SFU account, use subject “225: …”)
    • +
    + +

    Some Basic Rules

    + +
      +
    • In class, do not distract others.
    • +
    • In class, no taking photos/videos/recording
    • +
    • Help each other, but submit your own work.
    • +
    • Masks are mandatory in class: +
        +
      • may be removed while asking a question
      • +
      • if you cannot wear a mask, please email me
      • +
      +
    • +
    • Please stay on “your” side of the table +
        +
      • and stay 6’ away if you don’t have a mask on
      • +
      +
    • +
    + +

    Course Content

    + +
      +
    • Algorithms: processes that operate on data
    • +
    • Programs: implementations of algorithms
    • +
    • Data in programs: stored in data structures +
        +
      • simple D.S.: variables
      • +
      • most algorithms required compound D.S.s: e.g. arrays, lists, …
      • +
      • most non-trivial applications require non-trivial D.S.s
      • +
      +
    • +
    • Data Type: collection of values + operations on these values
    • +
    • Multiple data types: +
        +
      • Cleaner/simpler slg. design
      • +
      • better code
      • +
      +
    • +
    • Abstract Data Type: defined by values + operations without reference to how things are implemented
    • +
    • ADTs provide an abstract view of data: +
        +
      • let us reason about algorithms at a high level, ignoring implementation details
      • +
      +
    • +
    • Good ADT choices reduce program complexity & increase likelihood of success ( which is good).
    • +
    • a[i] vs. a(i)
    • +
    + +

    We will look at:

    +
      +
    • fundamental ADTs
    • +
    • fundamental data structures (to implement them)
    • +
    • sorting algorithms
    • +
    • with attention to: +
        +
      • correctness
      • +
      • efficiency/speed
      • +
      • how to implement
      • +
      • how to choose (for an application)
      • +
      +
    • +
    + +

    Pre-regs:

    + +
      +
    • Basic Programming (e.g. CMPT 125)
    • +
    • Discrete Math (e.g. MACM 101)
    • +
    + +

    (students get these in many different ways)

    + +

    To Do

    + +
      +
    • Read chapters 1 + 3 of text
    • +
    • Lear about CSIL
    • +
    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/02/02-stacks-and-queues.html b/_site/melody/cmpt-225/02/02-stacks-and-queues.html new file mode 100644 index 0000000..1afdc81 --- /dev/null +++ b/_site/melody/cmpt-225/02/02-stacks-and-queues.html @@ -0,0 +1,864 @@ + + + + + | tait.tech + + + + +
    +
    +

    Stacks & Queues - 1

    + +

    CMPT 225, Fall 2021, Lecture 2

    + +

    Stack

    + +
      +
    • ADT that stores a collection of objects/values
    • +
    • Insertions and removals follow last-in-first-out pattern
    • +
    • Fundamental Operations: +
        +
      • push: inserts an element on the “top”
      • +
      • pop: removes + returns the top element
      • +
      +
    • +
    + +

    e.g.

    + + + + + + + + +
    Stack indexValue
    + +

    push a

    + + + + + + + + + + + + +
    Stack indexValue
    0a
    + +

    push b

    + + + + + + + + + + + + + + + + +
    Stack indexValue
    0b
    1a
    + +

    push c

    + + + + + + + + + + + + + + + + + + + + +
    Stack indexValue
    0c
    1b
    2a
    + +

    pop

    + + + + + + + + + + + + + + + + +
    Stack indexValue
    0b
    1a
    + +
      +
    • conventient optional operations +
        +
      • size: return # of elements on stack // encapsulation
      • +
      • empty: check for emptiness // better than “size=0?”
      • +
      • top: return top element, but don’t remove it // better than x = pop(); push(x) then use x
      • +
      +
    • +
    + +

    Algorithm Applications

    + +
      +
    • parasing/evaluation/transformation of expressions
    • +
    • speech recognition coding/decoding
    • +
    • shared network access control
    • +
    • programming languages & execution +
        +
      • Postscript, Forth, the call stack
      • +
      +
    • +
    • things we do daily with computers
    • +
    + +

    Parenthasies Checking

    + +

    Parenthasies: (()(((()())()()(()))

    + +

    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.

    + +

    Can be done by counting unmached left parenthasies:

    + +

    Successful example:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
     0
    (1
    (2
    )1
    (2
    (3
    )2
    )1
    )0
    Yay!0
    + +

    Failed example:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCountry
     0
    (1
    (2
    )1
    (2
    )1
     Oh no it’s not zero!
    + +

    Another fail:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
     0
    (1
    )0
    (1
    )0
    )-1
     Negative 1 this time!
    + +

    Do this one yourself:

    + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    ( 
    ) 
    ) 
    ( 
    + +

    But consider multiple grouping symbols: (\{\{), (\{)\{, (\{[( ... ]. +Counting is not enough.

    + +

    Parenthasies Checking

    + +

    Diagram showing failure on the last character of this sequence: (()((()()))()(())))

    + +

    Can be done by counting unmatched left parenthasies:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    (1
    (2
    )1
    (2
    (3
    )2
    )1
    )0
    + +

    Example 2:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    (1
    (2
    )1
    (2
    )1
    + +

    Example 3:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    (1
    )0
    (1
    )0
    )-1
    + +

    Example 4:

    + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    (1
    )0
    )-1
    (0
    + +

    But consider multiple grouping symbols:

    + +
      +
    • sequence (\{\}) will and should work
    • +
    • sequence (\{)\} will work and should not
    • +
    • sequence (\{[( ... ???
    • +
    + +

    Counting is not enough.

    + +

    Stack-based Algorithm for Checking Grouping Symbols

    + +

    Pseudo-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
    +
    + +

    Diagram showing how each set of parenthasies are matched, innermost first torwards the outermost.

    + +

    Stack: Partially-Filled Array Implementation

    + +

    Variables:

    + +
      +
    • A - an array of stack elements
    • +
    • capacity - size of array
    • +
    • top - index in array of top stack element +
        +
      • -1 if stack is empty
      • +
      +
    • +
    + +

    A starts out as:

    + + + + + + + + + + + + + + + + + + + + +
    Index012capacity-1
    Valueabc 
    + +
      +
    • Capacity = <size of A>
    • +
    • top = 2
    • +
    + +

    push e

    + +

    A now equals

    + + + + + + + + + + + + + + + + + + + + + + + + +
    Index01234capacity-1
    Valueabcde 
    + +
      +
    • capacity = <size of A>
    • +
    • top = 3
    • +
    + +

    The Queue ADT

    + +

    A container that stores a collection of items with insertion and removal in “first-in-first-out” order.

    + +

    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.

    + +

    Queue: a diagram showing circles being unqueued in on the left and dequeued on the right. Like waiting in line for food.

    + +

    Basic operations:

    +
      +
    • enqueue: add an item to the back of a list
    • +
    • dequeue: remove & return the item at the front of the list
    • +
    + +

    Example: Palindrome Checking

    + +

    Sequence aba should succeed. +Sequence aab should not.

    + +

    In an array: a diagram showing the following pattern with the variable i set to the leftmost element and j set to the rightmost element.

    + + + + + + + + + + + + + + +
    Valuesabcsba
    + +

    With a stack and queue:

    +
      +
    1. read symbols, starting each in a stack and a queue
    2. +
    3. while the stack, queue are not empty: +
        +
      • pop and element from the stack + delete an element from the queue
      • +
      • if different -> not a palindrome
      • +
      +
    4. +
    5. palindrome
    6. +
    + +

    Queue: Array Implementations

    + +

    Enqueue: a,b,c,d. +Front is now a, last is now d.

    + + + + + + + + + + + + +
    Valuesabcd
    + +

    Dequeue returns and deletes first element. +Front is now b, last is now d.

    + + + + + + + + + + + + +
    Values?bcd
    + +

    Many enquques/dequeues later:

    + + + + + + + + + + + + +
    Valuessmno
    + +

    Front element is now m, last element is now s.

    + + + + + + + + + + + + + +
    Values?tuvw
    + +

    A similar slide is here, but I can’t see the differnece.

    + +

    Variables:

    +
      +
    • array A is array of size capacity
    • +
    • front is 0
    • +
    • back is 0
    • +
    • size is 0
    • +
    • capacity = <size of array>
    • +
    + +

    Pseudo-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 = <size of array> // ???(can't read) queue size supported
    +
    +enqueue(x) { // adds x to the back of the queue
    +  // requires size<capacity>
    +  A[back] = x
    +  size = size + 1
    +  back = (back + 1) % capacity
    +}
    +
    +dequeue() { // removes front element of queue and returns it
    +  // required size > 0
    +  temp = A[front]
    +  front = (front + 1) % capacity
    +  size = size - 1
    +  return temp
    +}
    +
    + +

    Is size == front - back?

    + +

    Diagram showing only true if front and back haven’t been switched around due to wrapping around back to the beginning o the array.

    + +

    “Circular Array” Queue Example

    + +
      +
    • size = 0
    • +
    • front = 0
    • +
    • back = 0
    • +
    + + + + + + + + + + + + + +
    Valuesemptyemptyemptyemptyemptyempty
    + +

    enqueue: a,b,c,d

    + +
      +
    • front = 0
    • +
    • back = 4
    • +
    + + + + + + + + + + + + + +
    Valuesabcdemptyempty
    + +

    dequque 3 times

    + +
      +
    • front = 3
    • +
    • back = 4
    • +
    + + + + + + + + + + + + + +
    Valuesemptyemptyemptydemptyempty
    + +

    enqueue e,f,g,h

    + +
      +
    • front = 3
    • +
    • back = 2
    • +
    + + + + + + + + + + + + + +
    Valuesghemptydef
    + +

    unqueue j

    + +
      +
    • size = 6
    • +
    • font = 3
    • +
    • back = 3
    • +
    + + + + + + + + + + + + + +
    Valuesghjdef
    + +

    dequeue 3 times

    + +
      +
    • back = 3
    • +
    • front = 0
    • +
    + + + + + + + + + + + + + +
    Valuesghjemptyemptyempty
    + +

    dequeue 3 times

    + +
      +
    • back = 3
    • +
    • front = 3
    • +
    + + + + + + + + + + + + + +
    Valuesemptyemptyemptyemptyemptyempty
    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/02/02-stacks-and-queues/index.html b/_site/melody/cmpt-225/02/02-stacks-and-queues/index.html new file mode 100644 index 0000000..4a90449 --- /dev/null +++ b/_site/melody/cmpt-225/02/02-stacks-and-queues/index.html @@ -0,0 +1,866 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    Stacks & Queues - 1

    + +

    CMPT 225, Fall 2021, Lecture 2

    + +

    Stack

    + +
      +
    • ADT that stores a collection of objects/values
    • +
    • Insertions and removals follow last-in-first-out pattern
    • +
    • Fundamental Operations: +
        +
      • push: inserts an element on the “top”
      • +
      • pop: removes + returns the top element
      • +
      +
    • +
    + +

    e.g.

    + + + + + + + + +
    Stack indexValue
    + +

    push a

    + + + + + + + + + + + + +
    Stack indexValue
    0a
    + +

    push b

    + + + + + + + + + + + + + + + + +
    Stack indexValue
    0b
    1a
    + +

    push c

    + + + + + + + + + + + + + + + + + + + + +
    Stack indexValue
    0c
    1b
    2a
    + +

    pop

    + + + + + + + + + + + + + + + + +
    Stack indexValue
    0b
    1a
    + +
      +
    • conventient optional operations +
        +
      • size: return # of elements on stack // encapsulation
      • +
      • empty: check for emptiness // better than “size=0?”
      • +
      • top: return top element, but don’t remove it // better than x = pop(); push(x) then use x
      • +
      +
    • +
    + +

    Algorithm Applications

    + +
      +
    • parasing/evaluation/transformation of expressions
    • +
    • speech recognition coding/decoding
    • +
    • shared network access control
    • +
    • programming languages & execution +
        +
      • Postscript, Forth, the call stack
      • +
      +
    • +
    • things we do daily with computers
    • +
    + +

    Parenthasies Checking

    + +

    Parenthasies: (()(((()())()()(()))

    + +

    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.

    + +

    Can be done by counting unmached left parenthasies:

    + +

    Successful example:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
     0
    (1
    (2
    )1
    (2
    (3
    )2
    )1
    )0
    Yay!0
    + +

    Failed example:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCountry
     0
    (1
    (2
    )1
    (2
    )1
     Oh no it’s not zero!
    + +

    Another fail:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
     0
    (1
    )0
    (1
    )0
    )-1
     Negative 1 this time!
    + +

    Do this one yourself:

    + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    ( 
    ) 
    ) 
    ( 
    + +

    But consider multiple grouping symbols: (\{\{), (\{)\{, (\{[( ... ]. +Counting is not enough.

    + +

    Parenthasies Checking

    + +

    Diagram showing failure on the last character of this sequence: (()((()()))()(())))

    + +

    Can be done by counting unmatched left parenthasies:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    (1
    (2
    )1
    (2
    (3
    )2
    )1
    )0
    + +

    Example 2:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    (1
    (2
    )1
    (2
    )1
    + +

    Example 3:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    (1
    )0
    (1
    )0
    )-1
    + +

    Example 4:

    + + + + + + + + + + + + + + + + + + + + + + + + +
    CharacterCounter
    (1
    )0
    )-1
    (0
    + +

    But consider multiple grouping symbols:

    + +
      +
    • sequence (\{\}) will and should work
    • +
    • sequence (\{)\} will work and should not
    • +
    • sequence (\{[( ... ???
    • +
    + +

    Counting is not enough.

    + +

    Stack-based Algorithm for Checking Grouping Symbols

    + +

    Pseudo-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
    +
    + +

    Diagram showing how each set of parenthasies are matched, innermost first torwards the outermost.

    + +

    Stack: Partially-Filled Array Implementation

    + +

    Variables:

    + +
      +
    • A - an array of stack elements
    • +
    • capacity - size of array
    • +
    • top - index in array of top stack element +
        +
      • -1 if stack is empty
      • +
      +
    • +
    + +

    A starts out as:

    + + + + + + + + + + + + + + + + + + + + +
    Index012capacity-1
    Valueabc 
    + +
      +
    • Capacity = <size of A>
    • +
    • top = 2
    • +
    + +

    push e

    + +

    A now equals

    + + + + + + + + + + + + + + + + + + + + + + + + +
    Index01234capacity-1
    Valueabcde 
    + +
      +
    • capacity = <size of A>
    • +
    • top = 3
    • +
    + +

    The Queue ADT

    + +

    A container that stores a collection of items with insertion and removal in “first-in-first-out” order.

    + +

    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.

    + +

    Queue: a diagram showing circles being unqueued in on the left and dequeued on the right. Like waiting in line for food.

    + +

    Basic operations:

    +
      +
    • enqueue: add an item to the back of a list
    • +
    • dequeue: remove & return the item at the front of the list
    • +
    + +

    Example: Palindrome Checking

    + +

    Sequence aba should succeed. +Sequence aab should not.

    + +

    In an array: a diagram showing the following pattern with the variable i set to the leftmost element and j set to the rightmost element.

    + + + + + + + + + + + + + + +
    Valuesabcsba
    + +

    With a stack and queue:

    +
      +
    1. read symbols, starting each in a stack and a queue
    2. +
    3. while the stack, queue are not empty: +
        +
      • pop and element from the stack + delete an element from the queue
      • +
      • if different -> not a palindrome
      • +
      +
    4. +
    5. palindrome
    6. +
    + +

    Queue: Array Implementations

    + +

    Enqueue: a,b,c,d. +Front is now a, last is now d.

    + + + + + + + + + + + + +
    Valuesabcd
    + +

    Dequeue returns and deletes first element. +Front is now b, last is now d.

    + + + + + + + + + + + + +
    Values?bcd
    + +

    Many enquques/dequeues later:

    + + + + + + + + + + + + +
    Valuessmno
    + +

    Front element is now m, last element is now s.

    + + + + + + + + + + + + + +
    Values?tuvw
    + +

    A similar slide is here, but I can’t see the differnece.

    + +

    Variables:

    +
      +
    • array A is array of size capacity
    • +
    • front is 0
    • +
    • back is 0
    • +
    • size is 0
    • +
    • capacity = <size of array>
    • +
    + +

    Pseudo-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 = <size of array> // ???(can't read) queue size supported
    +
    +enqueue(x) { // adds x to the back of the queue
    +  // requires size<capacity>
    +  A[back] = x
    +  size = size + 1
    +  back = (back + 1) % capacity
    +}
    +
    +dequeue() { // removes front element of queue and returns it
    +  // required size > 0
    +  temp = A[front]
    +  front = (front + 1) % capacity
    +  size = size - 1
    +  return temp
    +}
    +
    + +

    Is size == front - back?

    + +

    Diagram showing only true if front and back haven’t been switched around due to wrapping around back to the beginning o the array.

    + +

    “Circular Array” Queue Example

    + +
      +
    • size = 0
    • +
    • front = 0
    • +
    • back = 0
    • +
    + + + + + + + + + + + + + +
    Valuesemptyemptyemptyemptyemptyempty
    + +

    enqueue: a,b,c,d

    + +
      +
    • front = 0
    • +
    • back = 4
    • +
    + + + + + + + + + + + + + +
    Valuesabcdemptyempty
    + +

    dequque 3 times

    + +
      +
    • front = 3
    • +
    • back = 4
    • +
    + + + + + + + + + + + + + +
    Valuesemptyemptyemptydemptyempty
    + +

    enqueue e,f,g,h

    + +
      +
    • front = 3
    • +
    • back = 2
    • +
    + + + + + + + + + + + + + +
    Valuesghemptydef
    + +

    unqueue j

    + +
      +
    • size = 6
    • +
    • font = 3
    • +
    • back = 3
    • +
    + + + + + + + + + + + + + +
    Valuesghjdef
    + +

    dequeue 3 times

    + +
      +
    • back = 3
    • +
    • front = 0
    • +
    + + + + + + + + + + + + + +
    Valuesghjemptyemptyempty
    + +

    dequeue 3 times

    + +
      +
    • back = 3
    • +
    • front = 3
    • +
    + + + + + + + + + + + + + +
    Valuesemptyemptyemptyemptyemptyempty
    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/03/03-memory-and-pointers.html b/_site/melody/cmpt-225/03/03-memory-and-pointers.html new file mode 100644 index 0000000..0e7cb60 --- /dev/null +++ b/_site/melody/cmpt-225/03/03-memory-and-pointers.html @@ -0,0 +1,168 @@ + + + + + | tait.tech + + + + +
    +
    +

    Memory & Pointers 1

    + +

    CMPT-225, Fall 2021

    + +

    Computer Memory

    + +
      +
    • A sequence of locations
    • +
    • Indexed by address: 0,1,2,…
    • +
    • Each location stores a data byte
    • +
    • Processor can read or write the byte at each address.
    • +
    • Regions of memory are allocated to processes as needed, according to some scheme.
    • +
    + +

    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.

    + +

    Variables & Memory

    + +

    A variable is (roughly) and names & tagged collection of bytes:

    + +

    Diagram showing:

    +
      +
    • “int x; x = 6” taking 4 bytes of memory,
    • +
    • “char c; c = ‘x’;” taking 1 byte of memory
    • +
    • +

      “bool b; b = ‘true’;” taking 1 byte of memory

      +
    • +
    • So, at run time, each variable has an address in memory.
    • +
    • In C, C++ we can: +
        +
      • access the address of a variable
      • +
      • access a variable or memory location by its address
      • +
      • declare variables by storing addresses (pointers).
      • +
      +
    • +
    + +

    Addresses & Pointers - By Example

    + +
      +
    • “int i = 5;” +
        +
      • allocate space for an int,
      • +
      • name the space “i”,
      • +
      • store 5 there
      • +
      +
    • +
    • “int *p;” +
        +
      • allocate space for an address,
      • +
      • name it p,
      • +
      • record its type as “pointer to int”
      • +
      +
    • +
    • “p = ∧i;” +
        +
      • “&i” is the address of “i”
      • +
      • store ∧i in p
      • +
      • so, p becomes a pointer to i
      • +
      +
    • +
    • “cout « i;” +
        +
      • outputs the value stored in i, 5
      • +
      +
    • +
    • “cout « p;” +
        +
      • outputs the address of i: 0xbffffbbc
      • +
      +
    • +
    • “cout « *p;” +
        +
      • ”*” dereferences p. That is, *p is the value pointed to by p. In this case 5
      • +
      +
    • +
    + +

    Diagram of memory:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressNameValue
    254i5
    923p254
    + +

    Second diagram of memory:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressNameValueSize
    0x6fffbei54 bytes
    ???p0x6fffbe4 bytesa
    + +

    Slide showing program code: see pointers.c

    + +

    Slide showing output: see output.txt

    + +

    Slide showing same program with different highlights.

    + +

    Slide showing same output with different highlights.

    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/03/03-memory-and-pointers/index.html b/_site/melody/cmpt-225/03/03-memory-and-pointers/index.html new file mode 100644 index 0000000..6a88dfa --- /dev/null +++ b/_site/melody/cmpt-225/03/03-memory-and-pointers/index.html @@ -0,0 +1,170 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    Memory & Pointers 1

    + +

    CMPT-225, Fall 2021

    + +

    Computer Memory

    + +
      +
    • A sequence of locations
    • +
    • Indexed by address: 0,1,2,…
    • +
    • Each location stores a data byte
    • +
    • Processor can read or write the byte at each address.
    • +
    • Regions of memory are allocated to processes as needed, according to some scheme.
    • +
    + +

    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.

    + +

    Variables & Memory

    + +

    A variable is (roughly) and names & tagged collection of bytes:

    + +

    Diagram showing:

    +
      +
    • “int x; x = 6” taking 4 bytes of memory,
    • +
    • “char c; c = ‘x’;” taking 1 byte of memory
    • +
    • +

      “bool b; b = ‘true’;” taking 1 byte of memory

      +
    • +
    • So, at run time, each variable has an address in memory.
    • +
    • In C, C++ we can: +
        +
      • access the address of a variable
      • +
      • access a variable or memory location by its address
      • +
      • declare variables by storing addresses (pointers).
      • +
      +
    • +
    + +

    Addresses & Pointers - By Example

    + +
      +
    • “int i = 5;” +
        +
      • allocate space for an int,
      • +
      • name the space “i”,
      • +
      • store 5 there
      • +
      +
    • +
    • “int *p;” +
        +
      • allocate space for an address,
      • +
      • name it p,
      • +
      • record its type as “pointer to int”
      • +
      +
    • +
    • “p = ∧i;” +
        +
      • “&i” is the address of “i”
      • +
      • store ∧i in p
      • +
      • so, p becomes a pointer to i
      • +
      +
    • +
    • “cout « i;” +
        +
      • outputs the value stored in i, 5
      • +
      +
    • +
    • “cout « p;” +
        +
      • outputs the address of i: 0xbffffbbc
      • +
      +
    • +
    • “cout « *p;” +
        +
      • ”*” dereferences p. That is, *p is the value pointed to by p. In this case 5
      • +
      +
    • +
    + +

    Diagram of memory:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressNameValue
    254i5
    923p254
    + +

    Second diagram of memory:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressNameValueSize
    0x6fffbei54 bytes
    ???p0x6fffbe4 bytesa
    + +

    Slide showing program code: see pointers.c

    + +

    Slide showing output: see output.txt

    + +

    Slide showing same program with different highlights.

    + +

    Slide showing same output with different highlights.

    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/03/output.txt b/_site/melody/cmpt-225/03/output.txt new file mode 100644 index 0000000..fa087ce --- /dev/null +++ b/_site/melody/cmpt-225/03/output.txt @@ -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 diff --git a/_site/melody/cmpt-225/03/pointers.cpp b/_site/melody/cmpt-225/03/pointers.cpp new file mode 100644 index 0000000..09940ec --- /dev/null +++ b/_site/melody/cmpt-225/03/pointers.cpp @@ -0,0 +1,43 @@ +/* a small program to demonstrate C++ addresses, pointers and arrays */ + +#include /* Tait: missing name for import */ +#include /* 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; +} diff --git a/_site/melody/cmpt-225/04/04.html b/_site/melody/cmpt-225/04/04.html new file mode 100644 index 0000000..a411f7c --- /dev/null +++ b/_site/melody/cmpt-225/04/04.html @@ -0,0 +1,97 @@ + + + + + | tait.tech + + + + + +
    +
    +

    Vector Implementation Basics

    + +
      +
    • CMPT 225
    • +
    • Fall 2021
    • +
    • Lecture 4
    • +
    + +

    Example Using a Simple “Vector” Class

    + +

    Take a look at the simple_vector.cpp file.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeValue
    yfloat(empty)
    xint5
    pint *address
    gint *p+1
    + +

    What is *g?

    + +

    Implementation in IVector.h

    + +

    See IVector.h

    + +

    Templates

    + +
      +
    • Often, we have algorithms that will work on many data types, with few or no changes.
    • +
    • In strongly typed languages, we need a way to produce “generic” code–code that can work on different types in different places.
    • +
    • In C++, templates let us write generic code.
    • +
    • A template function or class defineition has a placeholder for one or more data types that is instanciated at compile time.
    • +
    • The instanciation may be different at different places in the same code.
    • +
    + +

    Test Program

    + +

    See test_program.cpp

    + +

    See simple_vector2.cpp

    + +

    TVector is a templated version of IVector

    + +

    See TVector.h

    + +

    See Vector.h

    + +

    See TestVector.cpp

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/04/04/index.html b/_site/melody/cmpt-225/04/04/index.html new file mode 100644 index 0000000..2dc5423 --- /dev/null +++ b/_site/melody/cmpt-225/04/04/index.html @@ -0,0 +1,99 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Vector Implementation Basics

    + +
      +
    • CMPT 225
    • +
    • Fall 2021
    • +
    • Lecture 4
    • +
    + +

    Example Using a Simple “Vector” Class

    + +

    Take a look at the simple_vector.cpp file.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeValue
    yfloat(empty)
    xint5
    pint *address
    gint *p+1
    + +

    What is *g?

    + +

    Implementation in IVector.h

    + +

    See IVector.h

    + +

    Templates

    + +
      +
    • Often, we have algorithms that will work on many data types, with few or no changes.
    • +
    • In strongly typed languages, we need a way to produce “generic” code–code that can work on different types in different places.
    • +
    • In C++, templates let us write generic code.
    • +
    • A template function or class defineition has a placeholder for one or more data types that is instanciated at compile time.
    • +
    • The instanciation may be different at different places in the same code.
    • +
    + +

    Test Program

    + +

    See test_program.cpp

    + +

    See simple_vector2.cpp

    + +

    TVector is a templated version of IVector

    + +

    See TVector.h

    + +

    See Vector.h

    + +

    See TestVector.cpp

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/04/225 L4 - Vector Implementation .pdf b/_site/melody/cmpt-225/04/225 L4 - Vector Implementation .pdf new file mode 100644 index 0000000..706e583 Binary files /dev/null and b/_site/melody/cmpt-225/04/225 L4 - Vector Implementation .pdf differ diff --git a/_site/melody/cmpt-225/04/IVector.h b/_site/melody/cmpt-225/04/IVector.h new file mode 100644 index 0000000..4115630 --- /dev/null +++ b/_site/melody/cmpt-225/04/IVector.h @@ -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 + +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 + +template //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 +#include +using namespace std; + +void print(const Vector> arr) +{ + int N=arr.size(); + for(int i=0;i& lhs, const Vector& rhs)const + {return lhs.size()> arr(N); + Vector 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)< +#include + +template +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;ktheSize){ + Object *newArray = new int[newCapacity]; //CHANGE + for (int k=0;k +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 +using namespace std; + +int main() +{ + const int N = 20; + + TVector v; // make an int vector + v.display(); // prints its contents + + // Stores N ints in the Vector + for (int i=0; i // 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 s1; + MinimalStack s2; + + cout<<"s1.isEmpty() = "< + + + + | tait.tech + + + + + +
    +
    +

    Dynamic Data Structures - Linked Lists

    + +

    Transcriber’s 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”, it’s not just a pointer to the value.

    + +

    Dynamic Data Structures

    + +
      +
    • Basic types (e.g. int, char, bool, …), and arrays of these store a fixed amount of data.
    • +
    • We want implementations of ADTs like stacks + queues to grow & shrink (their memory use) as needed. +
        +
      • e.g. like Vector, Array-something? (can’t read), String classes
      • +
      +
    • +
    + +

    Basic Idea:

    + +

    Store data in a collection of (simple) objects. +add/delete these as needed; link them all together to make the main object.

    + +

    Linked Lists

    + +
      +
    • A sequence of simple objects (nodes): each storing one datum (plus a link…) linked together in a chain
    • +
    • E.g., to store the list <3, 5, 7>:
    • +
    + + + + + + + + + + + + + + + + + + + + + + +
    DatumLink
    3link
    5link
    7link to nowhere
    + +
      +
    • These objects have no names, (in contrast to declared values) +
        +
      • we access them by following links +
          +
        • in Java, references (implemented as pointers)
        • +
        • in C++, pointers
        • +
        +
      • +
      +
    • +
    + +

    Need one named place to start like so:

    + +

    A normal variable of type “pointer to a node”: First

    + + + + + + + + + + + + + + + + + + + + + + +
    DataLink
    3pointer
    5pointer
    7null pointer
    + +

    Implementing a Stack with a Linked List (By example)

    + +

    Code to run, followed by the current stack.

    + +

    stack s;

    + +

    +node

    + +

    s.push(3)

    + +

    front

    + + + + + + + + + + + + + + +
    DataPointer
    3null pointer
    + +

    etc…

    + +

    + +

    s.push(5)

    + +

    front

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    DataPointer
    5pointer
    8pointer
    … (pointer)
    3null pointer
    + +

    s.push(7)

    + +

    front

    + + + + + + + + + + + + + + + + + + + + + + + + +
    ValuePointer
    7pointer
    5pointer
    8pointer
    + +

    To perform the push(7):

    + +
      +
    1. Make a new node to store the 7
    2. +
    3. modify links to insert it correctly
    4. +
    + +

    Original:

    + +

    front

    + + + + + + + + + + + + + + + + + + +
    DataPointer
    5pointer
    8
    + +

    After operation:

    + +

    front now points at node with value 7; node with value 7’s pointer now points to the old front

    + + + + + + + + + + + + + + + + + + + + + + +
    ValuePointer
    7pointer
    5pointer
    8
    + +

    s.pop()

    + +

    front

    + + + + + + + + + + + + + + + + + + +
    ValuePointer
    5pointer
    8
    + +

    How to perform the pop():

    +
      +
    1. Change the “top” link
    2. +
    3. Return the old top node
    4. +
    + +

    Linked list after running operation (note that two links go to the same node):

    + +

    top now points to second row

    + + + + + + + + + + + + + + + + + + + + + + +
    ValuePointer
    7 (return this value)pointer
    5pointer
    8
    + +
      +
    • Caveat 1: don’t lose the old top value
    • +
    • Caveat 2: don’t ignore the old top node (it still consumes space)
    • +
    + +

    “Improved” pop():

    + +
      +
    1. Store the old top value in “temp”
    2. +
    3. make top link to the new top node
    4. +
    5. free the space for the old top node
    6. +
    7. return temp
    8. +
    + +

    The List Class (A doubly-linked list implementation of a list)

    + +

    See list.cpp

    + +

    <…5,6,7,…> in a double linked list:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    PrevValueNext
    5next
    prev6next
    prev7
    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/05/225 L5 - Dynamic DS - LL.pdf b/_site/melody/cmpt-225/05/225 L5 - Dynamic DS - LL.pdf new file mode 100644 index 0000000..5f40097 Binary files /dev/null and b/_site/melody/cmpt-225/05/225 L5 - Dynamic DS - LL.pdf differ diff --git a/_site/melody/cmpt-225/05/225 L5 - Dynamic DS - LL/index.html b/_site/melody/cmpt-225/05/225 L5 - Dynamic DS - LL/index.html new file mode 100644 index 0000000..9d3919d --- /dev/null +++ b/_site/melody/cmpt-225/05/225 L5 - Dynamic DS - LL/index.html @@ -0,0 +1,366 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Dynamic Data Structures - Linked Lists

    + +

    Transcriber’s 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”, it’s not just a pointer to the value.

    + +

    Dynamic Data Structures

    + +
      +
    • Basic types (e.g. int, char, bool, …), and arrays of these store a fixed amount of data.
    • +
    • We want implementations of ADTs like stacks + queues to grow & shrink (their memory use) as needed. +
        +
      • e.g. like Vector, Array-something? (can’t read), String classes
      • +
      +
    • +
    + +

    Basic Idea:

    + +

    Store data in a collection of (simple) objects. +add/delete these as needed; link them all together to make the main object.

    + +

    Linked Lists

    + +
      +
    • A sequence of simple objects (nodes): each storing one datum (plus a link…) linked together in a chain
    • +
    • E.g., to store the list <3, 5, 7>:
    • +
    + + + + + + + + + + + + + + + + + + + + + + +
    DatumLink
    3link
    5link
    7link to nowhere
    + +
      +
    • These objects have no names, (in contrast to declared values) +
        +
      • we access them by following links +
          +
        • in Java, references (implemented as pointers)
        • +
        • in C++, pointers
        • +
        +
      • +
      +
    • +
    + +

    Need one named place to start like so:

    + +

    A normal variable of type “pointer to a node”: First

    + + + + + + + + + + + + + + + + + + + + + + +
    DataLink
    3pointer
    5pointer
    7null pointer
    + +

    Implementing a Stack with a Linked List (By example)

    + +

    Code to run, followed by the current stack.

    + +

    stack s;

    + +

    +node

    + +

    s.push(3)

    + +

    front

    + + + + + + + + + + + + + + +
    DataPointer
    3null pointer
    + +

    etc…

    + +

    + +

    s.push(5)

    + +

    front

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    DataPointer
    5pointer
    8pointer
    … (pointer)
    3null pointer
    + +

    s.push(7)

    + +

    front

    + + + + + + + + + + + + + + + + + + + + + + + + +
    ValuePointer
    7pointer
    5pointer
    8pointer
    + +

    To perform the push(7):

    + +
      +
    1. Make a new node to store the 7
    2. +
    3. modify links to insert it correctly
    4. +
    + +

    Original:

    + +

    front

    + + + + + + + + + + + + + + + + + + +
    DataPointer
    5pointer
    8
    + +

    After operation:

    + +

    front now points at node with value 7; node with value 7’s pointer now points to the old front

    + + + + + + + + + + + + + + + + + + + + + + +
    ValuePointer
    7pointer
    5pointer
    8
    + +

    s.pop()

    + +

    front

    + + + + + + + + + + + + + + + + + + +
    ValuePointer
    5pointer
    8
    + +

    How to perform the pop():

    +
      +
    1. Change the “top” link
    2. +
    3. Return the old top node
    4. +
    + +

    Linked list after running operation (note that two links go to the same node):

    + +

    top now points to second row

    + + + + + + + + + + + + + + + + + + + + + + +
    ValuePointer
    7 (return this value)pointer
    5pointer
    8
    + +
      +
    • Caveat 1: don’t lose the old top value
    • +
    • Caveat 2: don’t ignore the old top node (it still consumes space)
    • +
    + +

    “Improved” pop():

    + +
      +
    1. Store the old top value in “temp”
    2. +
    3. make top link to the new top node
    4. +
    5. free the space for the old top node
    6. +
    7. return temp
    8. +
    + +

    The List Class (A doubly-linked list implementation of a list)

    + +

    See list.cpp

    + +

    <…5,6,7,…> in a double linked list:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    PrevValueNext
    5next
    prev6next
    prev7
    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/05/list.cpp b/_site/melody/cmpt-225/05/list.cpp new file mode 100644 index 0000000..c229e17 --- /dev/null +++ b/_site/melody/cmpt-225/05/list.cpp @@ -0,0 +1,22 @@ +#include // not in original, needed for compilation + +template +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 diff --git a/_site/melody/cmpt-225/06/06.html b/_site/melody/cmpt-225/06/06.html new file mode 100644 index 0000000..0c45323 --- /dev/null +++ b/_site/melody/cmpt-225/06/06.html @@ -0,0 +1,710 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 225–Call Stack & Heap Memory

    + +

    The call stack

    + +
      +
    • Suppose a function A calls a function B, which calls C.
    • +
    • During execution control passes from (the code for) A, to B, then to C.
    • +
    • The execution of C ends, control must return to B and then to A.
    • +
    + +

    ...ABC... +... \leftrightarrow A \leftrightarrow B \leftrightarrow C \leftrightarrow ... +

    + +
      +
    • At each function call, the system records where control should return to be pushing an activation record on the call stack
    • +
    • The call stack also records all local variables, including the arguments to the function call.
    • +
    + +

    Call Stack Illustration

    + +

    Code is in call_stack.cpp

    + +

    Call stack for this process:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameData
    maina=1, b=0
    f(2)c=2, d=3, e=8
    g(4)i=4
    g<code for g>
    f<code for f>
    main<code for main>
    + +

    Stack Code

    + +

    Picture of code, which can be found in stack_pointers.cpp

    + +

    Picture of code output, which can be found in stack_pointers_output.txt

    + +

    Dynamic Memory or Heap

    + +
      +
    • Vairables declared in functions are stored on the call stack
    • +
    • These variables: +
        +
      • are of fixed size
      • +
      • are destoryed when the function they are defined in terminates
      • +
      +
    • +
    • We often want a function f to create data that can be used after it returns. +
        +
      • In particular, dynamic data structures require this!
      • +
      +
    • +
    • This data is stored in “the heap”, a region of memory that is allocated dynamically as needed!
    • +
    + +

    In C++:

    + +
      +
    • Basic (or primitive) types can be stored on the call stack or on the heap.
    • +
    • Objects (e.g. instances of classes) can be stored on the call stack or on the heap.
    • +
    • Variables declared in functions are on the stack
    • +
    • Allocation on the heap is denoted by “new”.
    • +
    + +

    Ex: Basic Types on Call Stack & Heap

    + +

    Code snippet below:

    + +
    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.
    +}
    +
    + +

    Ex: Basic Types on Call Stack & Heap

    + +

    See code above.

    + +

    Call stack:

    + + + + + + + + + + + + + + + + + + + + + +
    NameValueNote
    n6 
    nppointernp is a pointer to the location on the heap.
    + +

    Heap:

    + + + + + + + + + + + + + + + + +
    Value
    ...
    7
    ...
    + +

    Note: When f ends, np is gone (the stack is popped), but the space it pointed to is not.

    + +

    Class Instances on Heap & Stack (combines notes on two slides)

    + +

    Code snippet:

    + +
    List * f(){
    +  List L;
    +  List * Lp;
    +  Lp = new List();
    +  return Lp; // returns pointer
    +}
    +
    +main(){
    +  ...
    +  List * lst = f(); // lst becomes a pointer to the list object
    +  ...
    +}
    +
    + +

    Call Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameValuesNotes
    lstpointer to heapin main
    L<List Object>entire List object; in function f (destroyed when f ends)
    Lppointer to <List Object>in function f (this pointer is also destroyed when f exits); annotation: this instanciates the list class
    + +

    Heap:

    + + + + + + + + + + + + + + + + + + + + +
    ValueNotes
    <List Object>entire list object
    + +

    Accessing Instance Members in C++ (combination of notes in two slides)

    + +

    Suppose a class Store with:

    + +
      +
    • a data member x. (an int)
    • +
    • a function put(v) that stores v in x.
    • +
    • a function get() that returns the value of x.
    • +
    + +

    Consider this code fragment:

    + +
    f(){
    +  ...
    +  Store s1;
    +  s1.put(5);
    +  y=s1.get();//y=5
    +  ...
    +  Store *s2;
    +  s2=new Store();// (annotation: XX)
    +  s2.put(5);// (annotation: X)
    +  y=s2.get()// (annotation: X)
    +  ...
    +  *s2.put(5);// (annotation: X)
    +  y=*s2.get();// (annotation: X)
    +  ...
    +  (*s2).put(5);// (annotation: check mark)
    +  y=(*s2).get();// (annotation: check mark)
    +  ...
    +  s2->put(5); // equiv. to (*s).put(5)// (annotation: check mark)
    +  y=s2->get(); // equiv. to y=(*s).get()// (annotation: check mark)
    +}
    +
    + +

    Call Stack:

    + + + + + + + + + + + + + + + + + + + + + +
    NameValueNotes
    s2pointer to <Store Object> 
    s1<Store Object [x=5]> 
    + +

    Heap:

    + + + + + + + + + + + + + + +
    ValueNotes
    <Store Object [x=5]> 
    + +

    Using the Textbook List Class

    + +

    See textbook_list.cpp +and List.h.

    + +

    Call stack during the run of the program:

    + + + + + + + + + + + + + + + + + + + + + +
    NameValueNotes
    N5within main
    lst<List Object [size=5, head=0, tail=4]>within main
    + +

    The heap is empty.

    + +

    See “doubly linked list” example in lecture 05.

    + +

    The List Class

    + +

    Two images of the doubly linked list slides from lecture 05 with a big red X written overtop each image.

    + +

    The List Class - Constructor

    + +

    Code snippet:

    + +
    private:
    +  int theSize;
    +  Node *head;
    +  Node *tail;
    +  void init()
    +  {
    +    theSize = 0;
    +    head = new Node;
    +    tail = new Node;
    +    head->next = tail;
    +    tail->prev = head;
    +  }
    +};
    +
    + +

    After constructor:

    + + + + + + + + + + + + + + + + + + + + + + + + +
    NameNextValuePrev
    headpointernullptrnullptr
    tailnullptrnullptrpointer
    + +

    Code snippet form List.h:

    + +
    Node(const Object& d = Object{}, Node* p = nullptr, Node* n = nullptr)
    +  : data{d}, prev{p}, next{n} {}
    +
    + +

    The List Class - The iterators

    + +
      +
    • Data member (Node* current) +
        +
      • a pointer to a Node. (the list iterators are implemented with pointers.)
      • +
      +
    • +
    • Constructors: +
        +
      • turn a pointer into an iterator: +
          +
        • iterator(Node\* p): const_iterator{p}{}
        • +
        • const_iterator(Node\* p): current{p}
        • +
        +
      • +
      +
    • +
    • function: +
        +
      • iterator end(){ return iterator(tail} +
          +
        • turns the tail pointer into the iter to “end”. (???can’t read completely)
        • +
        • it corresponds to “just past the end” of the list
        • +
        +
      • +
      +
    • +
    + +

    The List Class - the push_back function

    + +
    // 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& x){insert(end(), x);}
    +
    +iterator insert(iterator itr, const Object& x){
    +  Node *p = itr.current; // turns the iterator into a pointer
    +  ++theSize; //increments size variable
    +  return iterator(p->prev=p->prev->next=new Node(x, p->prev, p)) // turns the pointer into an iterator
    +  // p->prev=p->prev->next=new Node(x, p->prev, p)) does two things:
    +  // 1. Makes a new node `N`
    +  // 2. stores a pointer to `N` in p->prev and it p->prev->next
    +}
    +
    + +

    The List Class - Inserting the first element (combination of notes from two slides)

    + +

    Code snippet:

    + +
    // itr is the end
    +iterator insert(iterator itr, const Object& x){
    +  Node *p = itr.current;// itr.current is the tail
    +  ++theSize;
    +  return iterator(p->prev=p->prev->next=new Node(x, p->prev, p)); // turns pointer into iterator
    +  // x is new list element
    +  // p->prev is initial value of prev
    +  // p is initial value of next
    +}
    +
    + +

    List pointed to by pointer p (original)

    + + + + + + + + + + + + + + + + + + + + + + + + +
    NameNextValuePrev
    headpointernullptrnullptr
    tailnullptrnullptrpointer
    + +

    The “new Node(…)” object:

    + + + + + + + + + + + + + + + + +
    NextValuePrev
    pointernullptrpointer
    + +

    Transcriber’s addition (implied in diagram): What does p->prev=p->prev->next=New Node(...) do to this list?

    + +

    Transciber’s note: It inserts the new Node(...) into the list like so:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameNextValuePrev
    headpointernullptrnullptr
    new Node(...)pointernullptrpointer
    tailnullptrnullptrpointer
    + +

    Transciber’s note: This cancels the previous connection between head and tail directly.

    + +

    Using the Textbook List Class

    + +

    Code snippet:

    + +
    #include "dsexceptions.h"
    +#include "List.h"
    +int main(){
    +  const int N = 5;
    +  List<int> = lst;
    +  for(int i=N-1; i<0; --i){
    +    lst.push_front(i);
    +  }
    +  return 0;
    +}
    +
    +

    When lst contains the two values [1, 2]:

    + +

    Call Stack:

    + + + + + + + + + + + + + + + + + + + + + +
    NameValueNotes
    N5 
    lst<List Object [theSize=2, head=pointer, tail=pointer> 
    + +

    Heap (transcriber’s 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):

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueNotes
    Node.nextNode index 0
    Node Value: 1Node index 0
    Node.prevNode index 0
    Node.next (nullptr)Ending Node
    Node Value: ?Ending Node
    Node.prevEnding Node
    Node.nextNode index 1
    Node Value: 2Node index 1
    Node.prevNode index 1
    Node.nextBeginning Node
    Node Value: ?Beginning Node
    Node.prev (nullptr)Beginning Node
    + +

    Linked List Ends

    + +

    Transcriber’s note: tell me if you like this way of transcribing simplified linked lists where the individual values don’t matter, but only a linear relationship between the nodes:

    + +

    For [4, 5, 6]:

    + +

    headbeginning node456ending nodetail +\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{ending node} \leftrightarrow \text{tail} +

    + +

    versus

    + +

    head456tail +\text{head} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{tail} +

    + +

    For [5]:

    + +

    headbeginning node5ending nodetail +\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 5 \leftrightarrow \text{ending node} \leftrightarrow \text{tail} +

    + +

    versus

    + +head5tail\text{head} \leftrightarrow 5 \leftrightarrow \text{tail} + +

    For []:

    + +headbeginning nodeending nodetail\text{head} \leftrightarrow \text{beginning node} \leftrightarrow \text{ending node} \leftrightarrow \text{tail} + +

    versus (transcriber’s note: the lack of connection is intentional)

    + +head,tail\text{head} , \text{tail} + +

    End

    + +

    After the “End” slide, some previous slides are repeated for no apparent reason.

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/06/06/index.html b/_site/melody/cmpt-225/06/06/index.html new file mode 100644 index 0000000..ce06cff --- /dev/null +++ b/_site/melody/cmpt-225/06/06/index.html @@ -0,0 +1,712 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 225–Call Stack & Heap Memory

    + +

    The call stack

    + +
      +
    • Suppose a function A calls a function B, which calls C.
    • +
    • During execution control passes from (the code for) A, to B, then to C.
    • +
    • The execution of C ends, control must return to B and then to A.
    • +
    + +

    ...ABC... +... \leftrightarrow A \leftrightarrow B \leftrightarrow C \leftrightarrow ... +

    + +
      +
    • At each function call, the system records where control should return to be pushing an activation record on the call stack
    • +
    • The call stack also records all local variables, including the arguments to the function call.
    • +
    + +

    Call Stack Illustration

    + +

    Code is in call_stack.cpp

    + +

    Call stack for this process:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameData
    maina=1, b=0
    f(2)c=2, d=3, e=8
    g(4)i=4
    g<code for g>
    f<code for f>
    main<code for main>
    + +

    Stack Code

    + +

    Picture of code, which can be found in stack_pointers.cpp

    + +

    Picture of code output, which can be found in stack_pointers_output.txt

    + +

    Dynamic Memory or Heap

    + +
      +
    • Vairables declared in functions are stored on the call stack
    • +
    • These variables: +
        +
      • are of fixed size
      • +
      • are destoryed when the function they are defined in terminates
      • +
      +
    • +
    • We often want a function f to create data that can be used after it returns. +
        +
      • In particular, dynamic data structures require this!
      • +
      +
    • +
    • This data is stored in “the heap”, a region of memory that is allocated dynamically as needed!
    • +
    + +

    In C++:

    + +
      +
    • Basic (or primitive) types can be stored on the call stack or on the heap.
    • +
    • Objects (e.g. instances of classes) can be stored on the call stack or on the heap.
    • +
    • Variables declared in functions are on the stack
    • +
    • Allocation on the heap is denoted by “new”.
    • +
    + +

    Ex: Basic Types on Call Stack & Heap

    + +

    Code snippet below:

    + +
    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.
    +}
    +
    + +

    Ex: Basic Types on Call Stack & Heap

    + +

    See code above.

    + +

    Call stack:

    + + + + + + + + + + + + + + + + + + + + + +
    NameValueNote
    n6 
    nppointernp is a pointer to the location on the heap.
    + +

    Heap:

    + + + + + + + + + + + + + + + + +
    Value
    ...
    7
    ...
    + +

    Note: When f ends, np is gone (the stack is popped), but the space it pointed to is not.

    + +

    Class Instances on Heap & Stack (combines notes on two slides)

    + +

    Code snippet:

    + +
    List * f(){
    +  List L;
    +  List * Lp;
    +  Lp = new List();
    +  return Lp; // returns pointer
    +}
    +
    +main(){
    +  ...
    +  List * lst = f(); // lst becomes a pointer to the list object
    +  ...
    +}
    +
    + +

    Call Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameValuesNotes
    lstpointer to heapin main
    L<List Object>entire List object; in function f (destroyed when f ends)
    Lppointer to <List Object>in function f (this pointer is also destroyed when f exits); annotation: this instanciates the list class
    + +

    Heap:

    + + + + + + + + + + + + + + + + + + + + +
    ValueNotes
    <List Object>entire list object
    + +

    Accessing Instance Members in C++ (combination of notes in two slides)

    + +

    Suppose a class Store with:

    + +
      +
    • a data member x. (an int)
    • +
    • a function put(v) that stores v in x.
    • +
    • a function get() that returns the value of x.
    • +
    + +

    Consider this code fragment:

    + +
    f(){
    +  ...
    +  Store s1;
    +  s1.put(5);
    +  y=s1.get();//y=5
    +  ...
    +  Store *s2;
    +  s2=new Store();// (annotation: XX)
    +  s2.put(5);// (annotation: X)
    +  y=s2.get()// (annotation: X)
    +  ...
    +  *s2.put(5);// (annotation: X)
    +  y=*s2.get();// (annotation: X)
    +  ...
    +  (*s2).put(5);// (annotation: check mark)
    +  y=(*s2).get();// (annotation: check mark)
    +  ...
    +  s2->put(5); // equiv. to (*s).put(5)// (annotation: check mark)
    +  y=s2->get(); // equiv. to y=(*s).get()// (annotation: check mark)
    +}
    +
    + +

    Call Stack:

    + + + + + + + + + + + + + + + + + + + + + +
    NameValueNotes
    s2pointer to <Store Object> 
    s1<Store Object [x=5]> 
    + +

    Heap:

    + + + + + + + + + + + + + + +
    ValueNotes
    <Store Object [x=5]> 
    + +

    Using the Textbook List Class

    + +

    See textbook_list.cpp +and List.h.

    + +

    Call stack during the run of the program:

    + + + + + + + + + + + + + + + + + + + + + +
    NameValueNotes
    N5within main
    lst<List Object [size=5, head=0, tail=4]>within main
    + +

    The heap is empty.

    + +

    See “doubly linked list” example in lecture 05.

    + +

    The List Class

    + +

    Two images of the doubly linked list slides from lecture 05 with a big red X written overtop each image.

    + +

    The List Class - Constructor

    + +

    Code snippet:

    + +
    private:
    +  int theSize;
    +  Node *head;
    +  Node *tail;
    +  void init()
    +  {
    +    theSize = 0;
    +    head = new Node;
    +    tail = new Node;
    +    head->next = tail;
    +    tail->prev = head;
    +  }
    +};
    +
    + +

    After constructor:

    + + + + + + + + + + + + + + + + + + + + + + + + +
    NameNextValuePrev
    headpointernullptrnullptr
    tailnullptrnullptrpointer
    + +

    Code snippet form List.h:

    + +
    Node(const Object& d = Object{}, Node* p = nullptr, Node* n = nullptr)
    +  : data{d}, prev{p}, next{n} {}
    +
    + +

    The List Class - The iterators

    + +
      +
    • Data member (Node* current) +
        +
      • a pointer to a Node. (the list iterators are implemented with pointers.)
      • +
      +
    • +
    • Constructors: +
        +
      • turn a pointer into an iterator: +
          +
        • iterator(Node\* p): const_iterator{p}{}
        • +
        • const_iterator(Node\* p): current{p}
        • +
        +
      • +
      +
    • +
    • function: +
        +
      • iterator end(){ return iterator(tail} +
          +
        • turns the tail pointer into the iter to “end”. (???can’t read completely)
        • +
        • it corresponds to “just past the end” of the list
        • +
        +
      • +
      +
    • +
    + +

    The List Class - the push_back function

    + +
    // 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& x){insert(end(), x);}
    +
    +iterator insert(iterator itr, const Object& x){
    +  Node *p = itr.current; // turns the iterator into a pointer
    +  ++theSize; //increments size variable
    +  return iterator(p->prev=p->prev->next=new Node(x, p->prev, p)) // turns the pointer into an iterator
    +  // p->prev=p->prev->next=new Node(x, p->prev, p)) does two things:
    +  // 1. Makes a new node `N`
    +  // 2. stores a pointer to `N` in p->prev and it p->prev->next
    +}
    +
    + +

    The List Class - Inserting the first element (combination of notes from two slides)

    + +

    Code snippet:

    + +
    // itr is the end
    +iterator insert(iterator itr, const Object& x){
    +  Node *p = itr.current;// itr.current is the tail
    +  ++theSize;
    +  return iterator(p->prev=p->prev->next=new Node(x, p->prev, p)); // turns pointer into iterator
    +  // x is new list element
    +  // p->prev is initial value of prev
    +  // p is initial value of next
    +}
    +
    + +

    List pointed to by pointer p (original)

    + + + + + + + + + + + + + + + + + + + + + + + + +
    NameNextValuePrev
    headpointernullptrnullptr
    tailnullptrnullptrpointer
    + +

    The “new Node(…)” object:

    + + + + + + + + + + + + + + + + +
    NextValuePrev
    pointernullptrpointer
    + +

    Transcriber’s addition (implied in diagram): What does p->prev=p->prev->next=New Node(...) do to this list?

    + +

    Transciber’s note: It inserts the new Node(...) into the list like so:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameNextValuePrev
    headpointernullptrnullptr
    new Node(...)pointernullptrpointer
    tailnullptrnullptrpointer
    + +

    Transciber’s note: This cancels the previous connection between head and tail directly.

    + +

    Using the Textbook List Class

    + +

    Code snippet:

    + +
    #include "dsexceptions.h"
    +#include "List.h"
    +int main(){
    +  const int N = 5;
    +  List<int> = lst;
    +  for(int i=N-1; i<0; --i){
    +    lst.push_front(i);
    +  }
    +  return 0;
    +}
    +
    +

    When lst contains the two values [1, 2]:

    + +

    Call Stack:

    + + + + + + + + + + + + + + + + + + + + + +
    NameValueNotes
    N5 
    lst<List Object [theSize=2, head=pointer, tail=pointer> 
    + +

    Heap (transcriber’s 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):

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueNotes
    Node.nextNode index 0
    Node Value: 1Node index 0
    Node.prevNode index 0
    Node.next (nullptr)Ending Node
    Node Value: ?Ending Node
    Node.prevEnding Node
    Node.nextNode index 1
    Node Value: 2Node index 1
    Node.prevNode index 1
    Node.nextBeginning Node
    Node Value: ?Beginning Node
    Node.prev (nullptr)Beginning Node
    + +

    Linked List Ends

    + +

    Transcriber’s note: tell me if you like this way of transcribing simplified linked lists where the individual values don’t matter, but only a linear relationship between the nodes:

    + +

    For [4, 5, 6]:

    + +

    headbeginning node456ending nodetail +\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{ending node} \leftrightarrow \text{tail} +

    + +

    versus

    + +

    head456tail +\text{head} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{tail} +

    + +

    For [5]:

    + +

    headbeginning node5ending nodetail +\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 5 \leftrightarrow \text{ending node} \leftrightarrow \text{tail} +

    + +

    versus

    + +head5tail\text{head} \leftrightarrow 5 \leftrightarrow \text{tail} + +

    For []:

    + +headbeginning nodeending nodetail\text{head} \leftrightarrow \text{beginning node} \leftrightarrow \text{ending node} \leftrightarrow \text{tail} + +

    versus (transcriber’s note: the lack of connection is intentional)

    + +head,tail\text{head} , \text{tail} + +

    End

    + +

    After the “End” slide, some previous slides are repeated for no apparent reason.

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/06/225 L6 - Call Stack And Heap.pdf b/_site/melody/cmpt-225/06/225 L6 - Call Stack And Heap.pdf new file mode 100644 index 0000000..098e7f3 Binary files /dev/null and b/_site/melody/cmpt-225/06/225 L6 - Call Stack And Heap.pdf differ diff --git a/_site/melody/cmpt-225/06/List.h b/_site/melody/cmpt-225/06/List.h new file mode 100644 index 0000000..c229e17 --- /dev/null +++ b/_site/melody/cmpt-225/06/List.h @@ -0,0 +1,22 @@ +#include // not in original, needed for compilation + +template +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 diff --git a/_site/melody/cmpt-225/06/call_stack.cpp b/_site/melody/cmpt-225/06/call_stack.cpp new file mode 100644 index 0000000..80e6df1 --- /dev/null +++ b/_site/melody/cmpt-225/06/call_stack.cpp @@ -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 diff --git a/_site/melody/cmpt-225/06/stack_pointers.cpp b/_site/melody/cmpt-225/06/stack_pointers.cpp new file mode 100644 index 0000000..fcbc4d4 --- /dev/null +++ b/_site/melody/cmpt-225/06/stack_pointers.cpp @@ -0,0 +1,38 @@ +/* A program to demonstrate printing out partial contents of the call stack */ + +#include /* Note: only "#include" shown in image */ +#include /* 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; +} diff --git a/_site/melody/cmpt-225/06/stack_pointers_output.txt b/_site/melody/cmpt-225/06/stack_pointers_output.txt new file mode 100644 index 0000000..1c9d6f1 --- /dev/null +++ b/_site/melody/cmpt-225/06/stack_pointers_output.txt @@ -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 diff --git a/_site/melody/cmpt-225/06/textbook_list.cpp b/_site/melody/cmpt-225/06/textbook_list.cpp new file mode 100644 index 0000000..3ef5ce4 --- /dev/null +++ b/_site/melody/cmpt-225/06/textbook_list.cpp @@ -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 lst; + for (int i=N-1; i>0; --i) + { + lst.push_forward(i); + } + return 0; +} diff --git a/_site/melody/cmpt-225/07.html b/_site/melody/cmpt-225/07.html new file mode 100644 index 0000000..41c4601 --- /dev/null +++ b/_site/melody/cmpt-225/07.html @@ -0,0 +1,351 @@ + + + + + | tait.tech + + + + + +
    +
    +

    L.L. Queues & L.L. Traversal

    + +

    Basic L.L. Queue

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueNext
    apointer
    bpointer
    gpointer
    hnullptr
    + +

    as a stack, this L.L. would be:

    + +abha \rightarrow b \rightarrow \dots \rightarrow h + +

    Pseudo-code:

    + +
    class Node{
    +  Type data
    +  Node * next
    +}
    +
    + +

    Enqueue + Dequeue – First Versions

    + +

    Variables:

    + +
      +
    • float (pointer to stack)
    • +
    • back (pointer to stack)
    • +
    • size (# elements in queue)
    • +
    + +

    Dequeue:

    + +

    Pseudo-code:

    + +
    dequeue(){
    +  temp = front
    +  val = front->data
    +  front = front->next
    +  delete temp
    +  size = size-1
    +  return val
    +}
    +
    + +

    Diagram:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueNext
    apointer
    bpointer
    gpointer
    hnullptr
    + +

    Unqueue

    + +

    Pseudo-code:

    + +
    enqueue(x){
    +  n = new node containing x
    +  back->next = n
    +  back = back->next
    +  size = size+1
    +}
    +
    + +

    Diagram:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueNext
    apointer
    bpointer
    gpointer
    hnullptr pointer
    xnullptr
    + +

    Empty Queue

    + +

    A stack with a

    + +frontaback\text{front} \rightarrow a \leftarrow \text{back} + +

    After dequeue

    + +front,back\text{front}, \text{back} + +

    After enqueue(b)

    + +frontbback\text{front} \rightarrow b \leftarrow \text{back} + +

    Enqueue & Dequeue are different from empty/non-empty queues.

    + +

    Enqueue

    + +

    Example 1: Empty queue

    + +front,back\text{front}, \text{back} + +

    Enqueue b

    + +frontbback\text{front} \rightarrow b \leftarrow \text{back} + +

    Example 2: Starter queue

    + +frontxback\text{front} \rightarrow \dots \rightarrow x \leftarrow \text{back} + +

    enqueue b

    + +frontxbback\text{front} \rightarrow \dots \rightarrow x \rightarrow b \leftarrow \text{back} + +

    Pseudo code:

    + +
    enqueue(x){
    +  if size > 0{
    +    back->next = new Node(x)
    +    back = back->next
    +  } else {
    +    back = new Node(x)
    +    front = back
    +  }
    +  size = size+1
    +}
    +
    + +

    Dequeue

    + +

    Pseudo code:

    + +
    front = front->next
    +return value
    +
    + +

    That code has the following effect upon a queue.

    + +frontabcdback\text{front} \rightarrow a \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back} + +

    becomes

    + +frontbcdback\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back} + +

    while returning a.

    + +

    Example 1:

    + +frontbback\text{front} \rightarrow b \leftarrow \text{back} + +

    dequeue (b)

    + +front,back\text{front}, \text{back} + +

    Example 2:

    + +frontbcdback\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back} + +

    dequeue

    + +frontcdback\text{front} \rightarrow c \rightarrow d \leftarrow \text{back} + +

    Return b

    + +

    Pseudo code

    + +
    dequeue(){
    +  temp = front
    +  val = front->data
    +  front = front->next
    +  delete temp
    +  size = size-1
    +  return val
    +}
    +
    + +

    Traversing the List

    + +

    Pseudo code:

    + +
    displayList(){
    +  Node * cur = front;
    +  while(cur!=nullptr){
    +    output cur->data
    +    cur = cur->next
    +  }
    +}
    +
    + +

    A diagram which shows the cursor going from c, to d to be (in order from front to back).

    + +

    Linked List Ends

    + +

    Transcriber’s 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.

    + +

    For [4, 5, 6]:

    + +

    headbeginning node456ending nodetail +\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{ending node} \leftrightarrow \text{tail} +

    + +

    versus

    + +

    head456tail +\text{head} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{tail} +

    + +

    For [5]:

    + +

    headbeginning node5ending nodetail +\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 5 \leftrightarrow \text{ending node} \leftrightarrow \text{tail} +

    + +

    versus

    + +head5tail\text{head} \leftrightarrow 5 \leftrightarrow \text{tail} + +

    For []:

    + +headbeginning nodeending nodetail\text{head} \leftrightarrow \text{beginning node} \leftrightarrow \text{ending node} \leftrightarrow \text{tail} + +

    versus

    + +head,tail\text{head} , \text{tail} + +

    List Class: Traversing the List

    + +

    Pseudo code:

    + +
    displayList(){
    +  Node * cur = head->next
    +  while(cur!=tail){
    +    output cur->data
    +    cur = cur->next
    +  }
    +}
    +
    + +

    Graphic of cur moving through each (empty) element of the queue.

    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/07/07.html b/_site/melody/cmpt-225/07/07.html new file mode 100644 index 0000000..f54e9ac --- /dev/null +++ b/_site/melody/cmpt-225/07/07.html @@ -0,0 +1,349 @@ + + + + + | tait.tech + + + + + +
    +
    +

    L.L. Queues & L.L. Traversal

    + +

    Basic L.L. Queue

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueNext
    apointer
    bpointer
    gpointer
    hnullptr
    + +

    as a stack, this L.L. would be:

    + +abha \rightarrow b \rightarrow \dots \rightarrow h + +

    Pseudo-code:

    + +
    class Node{
    +  Type data
    +  Node * next
    +}
    +
    + +

    Enqueue + Dequeue – First Versions

    + +

    Variables:

    + +
      +
    • float (pointer to stack)
    • +
    • back (pointer to stack)
    • +
    • size (# elements in queue)
    • +
    + +

    Dequeue:

    + +

    Pseudo-code:

    + +
    dequeue(){
    +  temp = front
    +  val = front->data
    +  front = front->next
    +  delete temp
    +  size = size-1
    +  return val
    +}
    +
    + +

    Diagram:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueNext
    apointer
    bpointer
    gpointer
    hnullptr
    + +

    Unqueue

    + +

    Pseudo-code:

    + +
    enqueue(x){
    +  n = new node containing x
    +  back->next = n
    +  back = back->next
    +  size = size+1
    +}
    +
    + +

    Diagram:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueNext
    apointer
    bpointer
    gpointer
    hnullptr pointer
    xnullptr
    + +

    Empty Queue

    + +

    A stack with a

    + +frontaback\text{front} \rightarrow a \leftarrow \text{back} + +

    After dequeue

    + +front,back\text{front}, \text{back} + +

    After enqueue(b)

    + +frontbback\text{front} \rightarrow b \leftarrow \text{back} + +

    Enqueue & Dequeue are different from empty/non-empty queues.

    + +

    Enqueue

    + +

    Example 1: Empty queue

    + +front,back\text{front}, \text{back} + +

    Enqueue b

    + +frontbback\text{front} \rightarrow b \leftarrow \text{back} + +

    Example 2: Starter queue

    + +frontxback\text{front} \rightarrow \dots \rightarrow x \leftarrow \text{back} + +

    enqueue b

    + +frontxbback\text{front} \rightarrow \dots \rightarrow x \rightarrow b \leftarrow \text{back} + +

    Pseudo code:

    + +
    enqueue(x){
    +  if size > 0{
    +    back->next = new Node(x)
    +    back = back->next
    +  } else {
    +    back = new Node(x)
    +    front = back
    +  }
    +  size = size+1
    +}
    +
    + +

    Dequeue

    + +

    Pseudo code:

    + +
    front = front->next
    +return value
    +
    + +

    That code has the following effect upon a queue.

    + +frontabcdback\text{front} \rightarrow a \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back} + +

    becomes

    + +frontbcdback\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back} + +

    while returning a.

    + +

    Example 1:

    + +frontbback\text{front} \rightarrow b \leftarrow \text{back} + +

    dequeue (b)

    + +front,back\text{front}, \text{back} + +

    Example 2:

    + +frontbcdback\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back} + +

    dequeue

    + +frontcdback\text{front} \rightarrow c \rightarrow d \leftarrow \text{back} + +

    Return b

    + +

    Pseudo code

    + +
    dequeue(){
    +  temp = front
    +  val = front->data
    +  front = front->next
    +  delete temp
    +  size = size-1
    +  return val
    +}
    +
    + +

    Traversing the List

    + +

    Pseudo code:

    + +
    displayList(){
    +  Node * cur = front;
    +  while(cur!=nullptr){
    +    output cur->data
    +    cur = cur->next
    +  }
    +}
    +
    + +

    A diagram which shows the cursor going from c, to d to be (in order from front to back).

    + +

    Linked List Ends

    + +

    Transcriber’s 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.

    + +

    For [4, 5, 6]:

    + +

    headbeginning node456ending nodetail +\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{ending node} \leftrightarrow \text{tail} +

    + +

    versus

    + +

    head456tail +\text{head} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{tail} +

    + +

    For [5]:

    + +

    headbeginning node5ending nodetail +\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 5 \leftrightarrow \text{ending node} \leftrightarrow \text{tail} +

    + +

    versus

    + +head5tail\text{head} \leftrightarrow 5 \leftrightarrow \text{tail} + +

    For []:

    + +headbeginning nodeending nodetail\text{head} \leftrightarrow \text{beginning node} \leftrightarrow \text{ending node} \leftrightarrow \text{tail} + +

    versus

    + +head,tail\text{head} , \text{tail} + +

    List Class: Traversing the List

    + +

    Pseudo code:

    + +
    displayList(){
    +  Node * cur = head->next
    +  while(cur!=tail){
    +    output cur->data
    +    cur = cur->next
    +  }
    +}
    +
    + +

    Graphic of cur moving through each (empty) element of the queue.

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/07/07/index.html b/_site/melody/cmpt-225/07/07/index.html new file mode 100644 index 0000000..276ae35 --- /dev/null +++ b/_site/melody/cmpt-225/07/07/index.html @@ -0,0 +1,353 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    L.L. Queues & L.L. Traversal

    + +

    Basic L.L. Queue

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueNext
    apointer
    bpointer
    gpointer
    hnullptr
    + +

    as a stack, this L.L. would be:

    + +abha \rightarrow b \rightarrow \dots \rightarrow h + +

    Pseudo-code:

    + +
    class Node{
    +  Type data
    +  Node * next
    +}
    +
    + +

    Enqueue + Dequeue – First Versions

    + +

    Variables:

    + +
      +
    • float (pointer to stack)
    • +
    • back (pointer to stack)
    • +
    • size (# elements in queue)
    • +
    + +

    Dequeue:

    + +

    Pseudo-code:

    + +
    dequeue(){
    +  temp = front
    +  val = front->data
    +  front = front->next
    +  delete temp
    +  size = size-1
    +  return val
    +}
    +
    + +

    Diagram:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueNext
    apointer
    bpointer
    gpointer
    hnullptr
    + +

    Unqueue

    + +

    Pseudo-code:

    + +
    enqueue(x){
    +  n = new node containing x
    +  back->next = n
    +  back = back->next
    +  size = size+1
    +}
    +
    + +

    Diagram:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueNext
    apointer
    bpointer
    gpointer
    hnullptr pointer
    xnullptr
    + +

    Empty Queue

    + +

    A stack with a

    + +frontaback\text{front} \rightarrow a \leftarrow \text{back} + +

    After dequeue

    + +front,back\text{front}, \text{back} + +

    After enqueue(b)

    + +frontbback\text{front} \rightarrow b \leftarrow \text{back} + +

    Enqueue & Dequeue are different from empty/non-empty queues.

    + +

    Enqueue

    + +

    Example 1: Empty queue

    + +front,back\text{front}, \text{back} + +

    Enqueue b

    + +frontbback\text{front} \rightarrow b \leftarrow \text{back} + +

    Example 2: Starter queue

    + +frontxback\text{front} \rightarrow \dots \rightarrow x \leftarrow \text{back} + +

    enqueue b

    + +frontxbback\text{front} \rightarrow \dots \rightarrow x \rightarrow b \leftarrow \text{back} + +

    Pseudo code:

    + +
    enqueue(x){
    +  if size > 0{
    +    back->next = new Node(x)
    +    back = back->next
    +  } else {
    +    back = new Node(x)
    +    front = back
    +  }
    +  size = size+1
    +}
    +
    + +

    Dequeue

    + +

    Pseudo code:

    + +
    front = front->next
    +return value
    +
    + +

    That code has the following effect upon a queue.

    + +frontabcdback\text{front} \rightarrow a \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back} + +

    becomes

    + +frontbcdback\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back} + +

    while returning a.

    + +

    Example 1:

    + +frontbback\text{front} \rightarrow b \leftarrow \text{back} + +

    dequeue (b)

    + +front,back\text{front}, \text{back} + +

    Example 2:

    + +frontbcdback\text{front} \rightarrow b \rightarrow c \rightarrow d \leftarrow \text{back} + +

    dequeue

    + +frontcdback\text{front} \rightarrow c \rightarrow d \leftarrow \text{back} + +

    Return b

    + +

    Pseudo code

    + +
    dequeue(){
    +  temp = front
    +  val = front->data
    +  front = front->next
    +  delete temp
    +  size = size-1
    +  return val
    +}
    +
    + +

    Traversing the List

    + +

    Pseudo code:

    + +
    displayList(){
    +  Node * cur = front;
    +  while(cur!=nullptr){
    +    output cur->data
    +    cur = cur->next
    +  }
    +}
    +
    + +

    A diagram which shows the cursor going from c, to d to be (in order from front to back).

    + +

    Linked List Ends

    + +

    Transcriber’s 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.

    + +

    For [4, 5, 6]:

    + +

    headbeginning node456ending nodetail +\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{ending node} \leftrightarrow \text{tail} +

    + +

    versus

    + +

    head456tail +\text{head} \leftrightarrow 4 \leftrightarrow 5 \leftrightarrow 6 \leftrightarrow \text{tail} +

    + +

    For [5]:

    + +

    headbeginning node5ending nodetail +\text{head} \leftrightarrow \text{beginning node} \leftrightarrow 5 \leftrightarrow \text{ending node} \leftrightarrow \text{tail} +

    + +

    versus

    + +head5tail\text{head} \leftrightarrow 5 \leftrightarrow \text{tail} + +

    For []:

    + +headbeginning nodeending nodetail\text{head} \leftrightarrow \text{beginning node} \leftrightarrow \text{ending node} \leftrightarrow \text{tail} + +

    versus

    + +head,tail\text{head} , \text{tail} + +

    List Class: Traversing the List

    + +

    Pseudo code:

    + +
    displayList(){
    +  Node * cur = head->next
    +  while(cur!=tail){
    +    output cur->data
    +    cur = cur->next
    +  }
    +}
    +
    + +

    Graphic of cur moving through each (empty) element of the queue.

    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/07/225 L7 - LL Queue and Traversal.pdf b/_site/melody/cmpt-225/07/225 L7 - LL Queue and Traversal.pdf new file mode 100644 index 0000000..e966d9c Binary files /dev/null and b/_site/melody/cmpt-225/07/225 L7 - LL Queue and Traversal.pdf differ diff --git a/_site/melody/cmpt-225/08/08.html b/_site/melody/cmpt-225/08/08.html new file mode 100644 index 0000000..58af62a --- /dev/null +++ b/_site/melody/cmpt-225/08/08.html @@ -0,0 +1,1121 @@ + + + + + | tait.tech + + + + + +
    +
    +

    Rooted Trees – CMPT 225

    + +

    Graphs

    + +

    Graph is a pair G=<V,E>, with:

    + +
      +
    • V, a set called “vertacies” or “nodes”
    • +
    • E, a set of pairs from V, i.e. EV×VE \subseteq V\times V called edges
    • +
    • Example: G={1,2,3},{(1,2),(1,3)}G=\langle\{1,2,3\},\{(1,2),(1,3)\}\rangle
    • +
    + +

    Example in table format:

    + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    12,3
    21
    31
    + +

    “G is directed”: edges are ordered pairs (often called arcs)

    + +

    {{1,2,3},{(1,2),(1,3)}}{{1,2,3},{(2,1),(1,3)}} +\langle\{\{1,2,3\},\{(1,2),(1,3)\}\}\rangle +\neq +\langle\{\{1,2,3\},\{(2,1),(1,3)\}\}\rangle +

    + +

    i.e. graph one:

    + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    12,3
    2 
    3 
    + +

    does not equal graph two:

    + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    13
    21
    3 
    + +

    “G is undirected” edges are sets

    + +

    {{1,2,3},{(2,1),(1,3)}}={{1,2,3},{(1,2),(3,1)}} +\langle\{\{1,2,3\},\{(2,1),(1,3)\}\}\rangle += +\langle\{\{1,2,3\},\{(1,2),(3,1)\}\}\rangle +

    + +

    i.e. graph one:

    + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    13,2
    21
    31
    + +

    is equal to graph two:

    + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    12,3
    21
    31
    + +

    By default, by “graph” we will mean “undirected graph”.

    + +

    Path

    + +

    Path of G of length n is a sequence V0,V1,V2,Vn\langle V_{0},V_{1},V_{2},\dots V_{n}\rangle of vertacies s-t. (V0,V1),(V1,V2),(V2,V3)(V_{0},V_{1}),(V_{1},V_{2}),(V_{2},V_{3})\dots are edges of G.

    + +

    s-t Path in G: path s,,t\langle s,\dots,t\rangle in G.

    + +

    Example of a s-t path of length 6 for G: s,d,c,e,f,h,t\langle s,d,c,e,f,h,t\rangle

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    ab,s
    ba,c
    cb,d,e,s
    dc,s
    ec,f,g
    fe,h
    ge,h,t
    hf,g,t
    sa,c,d
    tg,h
    + +

    Vertex t is reachable from s in G if ther is an s-t path in G. +G is connected if for every pair u,vVu,v\in V, u is reachable from v.

    + +

    Transcriber’s note: this is much easier to show with graphics and in fact very hard to show easily without graphics. I did my best, but it should be noted that this is very obvious with a visual graph. +It becomes two seperate graphs, in essense. +So for convenience, I will be splitting this up into two tables, even though technically it’s one “graph” with two sets of connected nodes.

    + +

    Connected:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    ab,s
    ba,c
    cb,d,e,s
    dc,s
    ec,f,g
    fe,h
    ge,h,t
    hf,g,t
    sa,c,d
    tg,h
    + +

    Not connected:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    ab,s
    ba,c
    cb,d,e,s
    dc,s
    ec
    sa,c,d
    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    fh
    gh,t
    hf,g,t
    tg,h
    + +

    Cycles & Trees

    + +

    Cycle in G: path V0,Vn1,Vn\langle V_{0},\dots V_{n-1},V_{n}\rangle in G where V0=VnV_{0}=V_{n}

    + +

    Simple path: all vertecies are distinct. +Simple Cycle: cycle V0,Vn1,Vn\langle V_{0},\dots V_{n-1},V_{n}\rangle where V0,,Vn1\langle V_{0},\dots,V_{n-1} is a simple path.

    + +

    Simple cylcle of length 5: (see “connected” graph for confirmation): s,d,c,b,a,s\langle s,d,c,b,a,s\rangle

    + +

    Cycle of length 7 + repeats: s,d,c,b,f,e,c,s\langle s,d,c,b,f,e,c,s\rangle

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    ab,s
    ba,c,f
    cb,d,e,f,s
    dc,s
    ec,f,g
    fc,e,h,b
    ge,h,t
    hf,g,t
    sa,c,d
    tg,h
    + +

    Tree

    + +

    Tree: a connected, acyclic graph:

    + +

    Example 1 (not a tree, due to cycle: s,c,d,s\langle s,c,d,s\rangle):

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    ab
    ba,c
    cb,d,e,s
    dc,s
    ec,g
    fh
    ge,h,t
    hf,g,t
    sc,d
    tg,h
    + +

    Example 2 (a tree, no cycle, all connected):

    + +

    Now using ARIA trees:

    + + +
      +
    • + + s + +
        +
      • + + a + +
          +
        • + + b + +
        • +
        +
      • +
      • + + d + +
      • +
      • + + c + +
          +
        • + + e + +
            +
          • + + f + +
              +
            • + + h + +
            • +
            +
          • +
          • + + g + +
              +
            • + + i + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Example 3 (not a tree) (transcriber’s note: written as two trees for simplicity’s sake)

    + + +
      +
    • + + s + +
        +
      • + + a + +
          +
        • + + b + +
        • +
        +
      • +
      • + + d + +
      • +
      • + + c + +
          +
        • + + e + +
        • +
        +
      • +
      +
    • +
    + + + +
      +
    • + + f + +
        +
      • + + h + +
          +
        • + + g + +
            +
          • + + i + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Fact: every tree with nn verticies has n1n-1 edges.

    + +

    Rooted-tree:

    + +

    Rooted tree: tree with a distringuished vertex called the root.

    + +

    Unrooted tree:

    + + +
      +
    • + + a + +
        +
      • + + b + +
          +
        • + + c + +
        • +
        • + + d + +
        • +
        +
      • +
      • + + e + +
          +
        • + + f + +
            +
          • + + g + +
          • +
          • + + h + +
          • +
          +
        • +
        +
      • +
      • + + i + +
          +
        • + + j + +
            +
          • + + k + +
              +
            • + + l + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Tree T with root:

    + + +
      +
    • + + a (root) + +
        +
      • + + b + +
          +
        • + + c + +
        • +
        • + + d + +
        • +
        +
      • +
      • + + e + +
          +
        • + + f + +
            +
          • + + g + +
          • +
          • + + h + +
          • +
          +
        • +
        +
      • +
      • + + i + +
          +
        • + + j + +
            +
          • + + k + +
              +
            • + + l + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    An alternate drawing of T, with no semantic change.

    + +

    Notice: in a tree there is a unique path between any two verticies. +So: a unique path from any vertex to the root. +Thus: the root indicates a direction on the edges e.g. towards the root.

    + +

    A graph of the same tree, T, with arrows pointing towards the root. +There is no reason for having any semantic markup for this.

    + +

    (sometimes “away from the root”).

    + +

    Rooted Tree Terminology

    + + +
      +
    • + + z (root, anscestor of a) + +
        +
      • + + y + +
          +
        • + + x (leaf) + +
        • +
        • + + w (leaf) + +
        • +
        +
      • +
      • + + v (anscestor of a) + +
          +
        • + + u (leaf) + +
        • +
        +
      • +
      • + + t (anscestor of a, parent of a) + +
          +
        • + + s (sibling of a, leaf) + +
        • +
        • + + r (sibling of a, leaf) + +
        • +
        • + + a (descendant of a, anscestor of a) + +
            +
          • + + q (child of a, desandant of a) + +
              +
            • + + o (decendant of a, leaf) + +
            • +
            • + + n (decendant of a, leaf) + +
            • +
            • + + m (decendant of a, leaf) + +
            • +
            +
          • +
          • + + p (child of a, desendant of a, leaf) + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • The root has no parent.
    • +
    • Leaves have no children.
    • +
    • Internal nodes are the non-leaves (sometimes root ??? [can’t read] too)
    • +
    + +

    Depth & Height

    + + +
      +
    • + + 4 (depth 0) + +
        +
      • + + 1 (depth 1) + +
          +
        • + + 0 (depth 2) + +
        • +
        • + + 0 (depth 2) + +
        • +
        +
      • +
      • + + 3 (depth 1) + +
          +
        • + + 2 (depth 2) + +
            +
          • + + 1 (depth 3) + +
              +
            • + + 0 (depth 4) + +
            • +
            +
          • +
          • + + 0 (depth 3) + +
          • +
          +
        • +
        • + + 0 (depth 2) + +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • Depth of node v = length of path from v to the root.
    • +
    • Height of node v = length of longest path from to a decendant of v (e.g. to a leaf)
    • +
    • Height of Tree T = height of its root = max height of any node in T = max depth of any node in T.
    • +
    + +

    A rooted tree is:

    + +
      +
    • k-ary if no node has >k children.
    • +
    • binary if no node has >2 children.
    • +
    • ordered if the children of every node are ordered.
    • +
    + +

    E.g. A ordered trenary tree:

    + + +
      +
    • + + no name + +
        +
      • + + 1 + +
          +
        • + + 1 + +
        • +
        • + + 2 + +
        • +
        +
      • +
      • + + 2 + +
      • +
      • + + 3 + +
          +
        • + + 1 + +
        • +
        • + + 2 + +
        • +
        • + + 3 + +
        • +
        +
      • +
      +
    • +
    + + +

    Notice: when we draw a tree, or represent it in a data structure, we order it.

    + +

    In an ordered binary tree, every child of a node v is either the “left child of v” or the “right child of v”.

    + +

    Transcriber’s note: nodes’ children are transcribes from left to right. So for a binary tree, the first child is the “left child” and the second child is the “right child”.

    + +

    Subtree rooted at v

    + +

    Subtree rooted at v: tree with root v and containing all decendants of v.

    + +

    In a binary tree:

    +
      +
    • “left subtree of v” means the subtree rooted at the left child of v.
    • +
    • sim. for “right child of v”.
    • +
    + + +
      +
    • + + z + +
        +
      • + + v (subtree rooted at v) + +
          +
        • + + y (subtree rooted at v, left subtree of v) + +
            +
          • + + v (subtree rooted at v, left subtree of v + +
          • +
          • + + u (subtree rooted at v, left subtree of v + +
          • +
          +
        • +
        • + + x (subtree rooted at v, right subtree of v) + +
            +
          • + + w (subtree rooted at v, right subtree of v) + +
          • +
          +
        • +
        +
      • +
      • + + a + +
          +
        • + + b + +
        • +
        • + + c + +
        • +
        +
      • +
      +
    • +
    + + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/08/08/index.html b/_site/melody/cmpt-225/08/08/index.html new file mode 100644 index 0000000..fac4860 --- /dev/null +++ b/_site/melody/cmpt-225/08/08/index.html @@ -0,0 +1,1123 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Rooted Trees – CMPT 225

    + +

    Graphs

    + +

    Graph is a pair G=<V,E>, with:

    + +
      +
    • V, a set called “vertacies” or “nodes”
    • +
    • E, a set of pairs from V, i.e. EV×VE \subseteq V\times V called edges
    • +
    • Example: G={1,2,3},{(1,2),(1,3)}G=\langle\{1,2,3\},\{(1,2),(1,3)\}\rangle
    • +
    + +

    Example in table format:

    + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    12,3
    21
    31
    + +

    “G is directed”: edges are ordered pairs (often called arcs)

    + +

    {{1,2,3},{(1,2),(1,3)}}{{1,2,3},{(2,1),(1,3)}} +\langle\{\{1,2,3\},\{(1,2),(1,3)\}\}\rangle +\neq +\langle\{\{1,2,3\},\{(2,1),(1,3)\}\}\rangle +

    + +

    i.e. graph one:

    + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    12,3
    2 
    3 
    + +

    does not equal graph two:

    + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    13
    21
    3 
    + +

    “G is undirected” edges are sets

    + +

    {{1,2,3},{(2,1),(1,3)}}={{1,2,3},{(1,2),(3,1)}} +\langle\{\{1,2,3\},\{(2,1),(1,3)\}\}\rangle += +\langle\{\{1,2,3\},\{(1,2),(3,1)\}\}\rangle +

    + +

    i.e. graph one:

    + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    13,2
    21
    31
    + +

    is equal to graph two:

    + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    12,3
    21
    31
    + +

    By default, by “graph” we will mean “undirected graph”.

    + +

    Path

    + +

    Path of G of length n is a sequence V0,V1,V2,Vn\langle V_{0},V_{1},V_{2},\dots V_{n}\rangle of vertacies s-t. (V0,V1),(V1,V2),(V2,V3)(V_{0},V_{1}),(V_{1},V_{2}),(V_{2},V_{3})\dots are edges of G.

    + +

    s-t Path in G: path s,,t\langle s,\dots,t\rangle in G.

    + +

    Example of a s-t path of length 6 for G: s,d,c,e,f,h,t\langle s,d,c,e,f,h,t\rangle

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    ab,s
    ba,c
    cb,d,e,s
    dc,s
    ec,f,g
    fe,h
    ge,h,t
    hf,g,t
    sa,c,d
    tg,h
    + +

    Vertex t is reachable from s in G if ther is an s-t path in G. +G is connected if for every pair u,vVu,v\in V, u is reachable from v.

    + +

    Transcriber’s note: this is much easier to show with graphics and in fact very hard to show easily without graphics. I did my best, but it should be noted that this is very obvious with a visual graph. +It becomes two seperate graphs, in essense. +So for convenience, I will be splitting this up into two tables, even though technically it’s one “graph” with two sets of connected nodes.

    + +

    Connected:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    ab,s
    ba,c
    cb,d,e,s
    dc,s
    ec,f,g
    fe,h
    ge,h,t
    hf,g,t
    sa,c,d
    tg,h
    + +

    Not connected:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    ab,s
    ba,c
    cb,d,e,s
    dc,s
    ec
    sa,c,d
    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    fh
    gh,t
    hf,g,t
    tg,h
    + +

    Cycles & Trees

    + +

    Cycle in G: path V0,Vn1,Vn\langle V_{0},\dots V_{n-1},V_{n}\rangle in G where V0=VnV_{0}=V_{n}

    + +

    Simple path: all vertecies are distinct. +Simple Cycle: cycle V0,Vn1,Vn\langle V_{0},\dots V_{n-1},V_{n}\rangle where V0,,Vn1\langle V_{0},\dots,V_{n-1} is a simple path.

    + +

    Simple cylcle of length 5: (see “connected” graph for confirmation): s,d,c,b,a,s\langle s,d,c,b,a,s\rangle

    + +

    Cycle of length 7 + repeats: s,d,c,b,f,e,c,s\langle s,d,c,b,f,e,c,s\rangle

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    ab,s
    ba,c,f
    cb,d,e,f,s
    dc,s
    ec,f,g
    fc,e,h,b
    ge,h,t
    hf,g,t
    sa,c,d
    tg,h
    + +

    Tree

    + +

    Tree: a connected, acyclic graph:

    + +

    Example 1 (not a tree, due to cycle: s,c,d,s\langle s,c,d,s\rangle):

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nodeconnections
    ab
    ba,c
    cb,d,e,s
    dc,s
    ec,g
    fh
    ge,h,t
    hf,g,t
    sc,d
    tg,h
    + +

    Example 2 (a tree, no cycle, all connected):

    + +

    Now using ARIA trees:

    + + +
      +
    • + + s + +
        +
      • + + a + +
          +
        • + + b + +
        • +
        +
      • +
      • + + d + +
      • +
      • + + c + +
          +
        • + + e + +
            +
          • + + f + +
              +
            • + + h + +
            • +
            +
          • +
          • + + g + +
              +
            • + + i + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Example 3 (not a tree) (transcriber’s note: written as two trees for simplicity’s sake)

    + + +
      +
    • + + s + +
        +
      • + + a + +
          +
        • + + b + +
        • +
        +
      • +
      • + + d + +
      • +
      • + + c + +
          +
        • + + e + +
        • +
        +
      • +
      +
    • +
    + + + +
      +
    • + + f + +
        +
      • + + h + +
          +
        • + + g + +
            +
          • + + i + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Fact: every tree with nn verticies has n1n-1 edges.

    + +

    Rooted-tree:

    + +

    Rooted tree: tree with a distringuished vertex called the root.

    + +

    Unrooted tree:

    + + +
      +
    • + + a + +
        +
      • + + b + +
          +
        • + + c + +
        • +
        • + + d + +
        • +
        +
      • +
      • + + e + +
          +
        • + + f + +
            +
          • + + g + +
          • +
          • + + h + +
          • +
          +
        • +
        +
      • +
      • + + i + +
          +
        • + + j + +
            +
          • + + k + +
              +
            • + + l + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Tree T with root:

    + + +
      +
    • + + a (root) + +
        +
      • + + b + +
          +
        • + + c + +
        • +
        • + + d + +
        • +
        +
      • +
      • + + e + +
          +
        • + + f + +
            +
          • + + g + +
          • +
          • + + h + +
          • +
          +
        • +
        +
      • +
      • + + i + +
          +
        • + + j + +
            +
          • + + k + +
              +
            • + + l + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    An alternate drawing of T, with no semantic change.

    + +

    Notice: in a tree there is a unique path between any two verticies. +So: a unique path from any vertex to the root. +Thus: the root indicates a direction on the edges e.g. towards the root.

    + +

    A graph of the same tree, T, with arrows pointing towards the root. +There is no reason for having any semantic markup for this.

    + +

    (sometimes “away from the root”).

    + +

    Rooted Tree Terminology

    + + +
      +
    • + + z (root, anscestor of a) + +
        +
      • + + y + +
          +
        • + + x (leaf) + +
        • +
        • + + w (leaf) + +
        • +
        +
      • +
      • + + v (anscestor of a) + +
          +
        • + + u (leaf) + +
        • +
        +
      • +
      • + + t (anscestor of a, parent of a) + +
          +
        • + + s (sibling of a, leaf) + +
        • +
        • + + r (sibling of a, leaf) + +
        • +
        • + + a (descendant of a, anscestor of a) + +
            +
          • + + q (child of a, desandant of a) + +
              +
            • + + o (decendant of a, leaf) + +
            • +
            • + + n (decendant of a, leaf) + +
            • +
            • + + m (decendant of a, leaf) + +
            • +
            +
          • +
          • + + p (child of a, desendant of a, leaf) + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • The root has no parent.
    • +
    • Leaves have no children.
    • +
    • Internal nodes are the non-leaves (sometimes root ??? [can’t read] too)
    • +
    + +

    Depth & Height

    + + +
      +
    • + + 4 (depth 0) + +
        +
      • + + 1 (depth 1) + +
          +
        • + + 0 (depth 2) + +
        • +
        • + + 0 (depth 2) + +
        • +
        +
      • +
      • + + 3 (depth 1) + +
          +
        • + + 2 (depth 2) + +
            +
          • + + 1 (depth 3) + +
              +
            • + + 0 (depth 4) + +
            • +
            +
          • +
          • + + 0 (depth 3) + +
          • +
          +
        • +
        • + + 0 (depth 2) + +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • Depth of node v = length of path from v to the root.
    • +
    • Height of node v = length of longest path from to a decendant of v (e.g. to a leaf)
    • +
    • Height of Tree T = height of its root = max height of any node in T = max depth of any node in T.
    • +
    + +

    A rooted tree is:

    + +
      +
    • k-ary if no node has >k children.
    • +
    • binary if no node has >2 children.
    • +
    • ordered if the children of every node are ordered.
    • +
    + +

    E.g. A ordered trenary tree:

    + + +
      +
    • + + no name + +
        +
      • + + 1 + +
          +
        • + + 1 + +
        • +
        • + + 2 + +
        • +
        +
      • +
      • + + 2 + +
      • +
      • + + 3 + +
          +
        • + + 1 + +
        • +
        • + + 2 + +
        • +
        • + + 3 + +
        • +
        +
      • +
      +
    • +
    + + +

    Notice: when we draw a tree, or represent it in a data structure, we order it.

    + +

    In an ordered binary tree, every child of a node v is either the “left child of v” or the “right child of v”.

    + +

    Transcriber’s note: nodes’ children are transcribes from left to right. So for a binary tree, the first child is the “left child” and the second child is the “right child”.

    + +

    Subtree rooted at v

    + +

    Subtree rooted at v: tree with root v and containing all decendants of v.

    + +

    In a binary tree:

    +
      +
    • “left subtree of v” means the subtree rooted at the left child of v.
    • +
    • sim. for “right child of v”.
    • +
    + + +
      +
    • + + z + +
        +
      • + + v (subtree rooted at v) + +
          +
        • + + y (subtree rooted at v, left subtree of v) + +
            +
          • + + v (subtree rooted at v, left subtree of v + +
          • +
          • + + u (subtree rooted at v, left subtree of v + +
          • +
          +
        • +
        • + + x (subtree rooted at v, right subtree of v) + +
            +
          • + + w (subtree rooted at v, right subtree of v) + +
          • +
          +
        • +
        +
      • +
      • + + a + +
          +
        • + + b + +
        • +
        • + + c + +
        • +
        +
      • +
      +
    • +
    + + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/08/225 L8 - Rooted Trees.pdf b/_site/melody/cmpt-225/08/225 L8 - Rooted Trees.pdf new file mode 100644 index 0000000..c654d1f Binary files /dev/null and b/_site/melody/cmpt-225/08/225 L8 - Rooted Trees.pdf differ diff --git a/_site/melody/cmpt-225/09/09.html b/_site/melody/cmpt-225/09/09.html new file mode 100644 index 0000000..9a44a18 --- /dev/null +++ b/_site/melody/cmpt-225/09/09.html @@ -0,0 +1,469 @@ + + + + + | tait.tech + + + + + +
    +
    +

    Recursion on Trees

    + +

    CMPT 225

    + +

    Recursion

    + +

    Recursion: A definition of a function is recursive if the body cointains an application of itself.

    + +

    Consider: S(n)=i=0ni +S(n)=\sum_{i=0}^{n} i +

    + +

    or

    + +

    S(n)={0if n=0n+S(n1)if n>0 +S(n) = \begin{cases} +0 & \text{if } n=0\\ +n+S(n-1) & \text{if } n>0 +\end{cases} +

    + +

    These two descriptions of S(n)S(n) suggest two implementations:

    + +

    e.g.:

    + +
    S(n){
    +  s=0
    +  for i=1..n
    +    s=s+i
    +  return s
    +}
    +
    + +

    or

    + +
    S(n){
    +  if n=0
    +    return 0
    +  else
    +    return n+S(n-1)
    +}
    +
    + +

    Recursive Version

    + +
      +
    • -> S(4) +
        +
      • -> S(3) +
          +
        • -> S(2) +
            +
          • -> S(1) +
              +
            • -> S(0)
            • +
            • returns 0
            • +
            +
          • +
          • returns 1+0
          • +
          +
        • +
        • returns 2+1=3
        • +
        +
      • +
      • returns 3+3=6
      • +
      +
    • +
    • returns 4+6=10
    • +
    + +

    Iterative version:

    + +

    S=0+i=1ni=1+i=2ni=3+i=3ni==0+1+2+3+4 +S = 0 + \sum_{i=1}^{n} i = 1 + \sum_{i=2}^{n} i = 3 + \sum_{i=3}^{n} i = \dots\\ += 0+1+2+3+4 +

    + +

    The same computation, but a different control strategy.

    + +

    Recursion & The Call Stack

    + +

    Code:

    + +
    S(n){
    +  if(n=0){
    +    r=0
    +  }else{
    +    r=n+S(n-1)
    +  }
    +  return r
    +}
    +
    + +

    Compute p=S(2):

    + +
      +
    • Call S(2): +
        +
      • n = 2
      • +
      • r = ? -> +
          +
        • Call S(1): +
            +
          • n=1
          • +
          • r=? -> +
              +
            • Call S(0): +
                +
              • N=0
              • +
              • r=0
              • +
              • return 0
              • +
              +
            • +
            +
          • +
          • return 1+S(0)
          • +
          +
        • +
        +
      • +
      • return 2+S(1)
      • +
      +
    • +
    + +

    The call stack at the end:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameValueRemoved
    S<code of S; n=0, r=0>true
    S<code of S; n=1, r=1>true
    S<code of S; n=2, r=3>true
    p3false
    + +

    After the call the S(2) is complete, the entire call stack of S to S to S is gone.

    + +

    There are 2 more slides containing slightly different versions of the same diagrams. +They do not provide any extra information.

    + +

    Recursion on Trees

    + +
      +
    • We will often use recursion & induction on trees. +
        +
      • e.g. the tree rooted a v has some property if its subtrees have some related property
      • +
      +
    • +
    • e.g. the height of node v in a binary tree may be defined by:
    • +
    + +

    h(v)={0 if v is a leaf1+max{h(left(v)),h(right(v))} otherwise +h(v) = \begin{cases} +0 & \text{ if v is a leaf}\\ +1 + \text{max}\{h(\text{left}(v)), h(\text{right}(v))\} & \text{ otherwise} +\end{cases} +

    + +

    (We can define h(left(v)) to be -1 if left(v) does not exist, and sim. for right(v)).

    + +

    Recurssion a Trees Examples

    + +

    height of node v in T:

    + +

    h(v)={0 if v is a leaf1+max{h(left(v)),h(right(v))} ow +h(v) = \begin{cases} +0 & \text{ if v is a leaf}\\ +1+ max\{h(\text{left}(v)),h(\text{right}(v))\} & \text{ ow} +\end{cases} +

    + +

    for the follwing tree: h(v)=?h(v)=?

    + + +
      +
    • + + v + +
        +
      • + + a + +
          +
        • + + e + +
            +
          • + + g + +
          • +
          • + + h + +
          • +
          +
        • +
        • + + f + +
            +
          • + + i + +
          • +
          +
        • +
        +
      • +
      • + + b + +
          +
        • + + c + +
        • +
        • + + d + +
        • +
        +
      • +
      +
    • +
    + + +

    Recursion on Trees Example (pt 2)

    + +

    (See math equation of previous slide)

    + +

    h(v) = 3

    + + +
      +
    • + + v (3) + +
        +
      • + + a (2) + +
          +
        • + + e (1) + +
            +
          • + + g (0) + +
          • +
          • + + h (0) + +
          • +
          +
        • +
        • + + f (1) + +
            +
          • + + i (0) + +
          • +
          +
        • +
        +
      • +
      • + + b (1) + +
          +
        • + + c (0) + +
        • +
        • + + d (0) + +
        • +
        +
      • +
      +
    • +
    + + +

    Pseudo-code version:

    + +
    height(v){
    +  if v is a leaf
    +    return 0
    +  if v has one child u
    +    return 1+height(u)
    +  else
    +    return 1+max(height(left(v)), height(right(v)))
    +}
    +
    + +

    Traversals of Binary Tree

    + +
      +
    • A traversal of a graph is a process that “visits” each node in the graph once.
    • +
    • We consider 4 standard tree traversalt: +
        +
      1. level order
      2. +
      3. pre-order
      4. +
      5. in-order
      6. +
      7. post-order
      8. +
      +
    • +
    • 2,3,4 begin at the root & recursively visit the nodes in each subtree & the root. They vary in the relative ???(can’t read).
    • +
    + +

    (Level order later)

    + +

    Code:

    + +
    pre-order-T(v){
    +  visit v
    +  pre-order-T(left(v))
    +  pre-order-T(right(v))
    +}
    +
    + +

    pre-order-T(v) does nothing if v does not exist.

    + +
      +
    • v is visited before any of its decendants
    • +
    • every node in the left subtree is visited before any node in the right subtree.
    • +
    + +

    Tree to come back to:

    + + +
      +
    • + + A + +
        +
      • + + B + +
          +
        • + + D + +
        • +
        • + + E + +
        • +
        +
      • +
      • + + C + +
          +
        • + + F + +
        • +
        +
      • +
      +
    • +
    + + +

    Pre-order-traversal: A,B,D,E,C,F

    + +

    in-order-T

    + +

    code:

    + +
    in-order-T(v){
    +  in-order-T(left(v))
    +  visit v
    +  in-order-T(right(v))
    +}
    +
    + +

    In order traversal: D,B,E,A,C,F

    + +

    post-order-T

    + +

    code:

    + +
    post-order-T(v){
    +  post-order-T(left(v))
    +  post-order-T(right(v))
    +  visit v
    +}
    +
    + +

    Post order traversal: D,E,B,F,C,A

    + +

    End

    + +

    …some repeated slides… unknown reason

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/09/09/index.html b/_site/melody/cmpt-225/09/09/index.html new file mode 100644 index 0000000..cbf7e6b --- /dev/null +++ b/_site/melody/cmpt-225/09/09/index.html @@ -0,0 +1,471 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Recursion on Trees

    + +

    CMPT 225

    + +

    Recursion

    + +

    Recursion: A definition of a function is recursive if the body cointains an application of itself.

    + +

    Consider: S(n)=i=0ni +S(n)=\sum_{i=0}^{n} i +

    + +

    or

    + +

    S(n)={0if n=0n+S(n1)if n>0 +S(n) = \begin{cases} +0 & \text{if } n=0\\ +n+S(n-1) & \text{if } n>0 +\end{cases} +

    + +

    These two descriptions of S(n)S(n) suggest two implementations:

    + +

    e.g.:

    + +
    S(n){
    +  s=0
    +  for i=1..n
    +    s=s+i
    +  return s
    +}
    +
    + +

    or

    + +
    S(n){
    +  if n=0
    +    return 0
    +  else
    +    return n+S(n-1)
    +}
    +
    + +

    Recursive Version

    + +
      +
    • -> S(4) +
        +
      • -> S(3) +
          +
        • -> S(2) +
            +
          • -> S(1) +
              +
            • -> S(0)
            • +
            • returns 0
            • +
            +
          • +
          • returns 1+0
          • +
          +
        • +
        • returns 2+1=3
        • +
        +
      • +
      • returns 3+3=6
      • +
      +
    • +
    • returns 4+6=10
    • +
    + +

    Iterative version:

    + +

    S=0+i=1ni=1+i=2ni=3+i=3ni==0+1+2+3+4 +S = 0 + \sum_{i=1}^{n} i = 1 + \sum_{i=2}^{n} i = 3 + \sum_{i=3}^{n} i = \dots\\ += 0+1+2+3+4 +

    + +

    The same computation, but a different control strategy.

    + +

    Recursion & The Call Stack

    + +

    Code:

    + +
    S(n){
    +  if(n=0){
    +    r=0
    +  }else{
    +    r=n+S(n-1)
    +  }
    +  return r
    +}
    +
    + +

    Compute p=S(2):

    + +
      +
    • Call S(2): +
        +
      • n = 2
      • +
      • r = ? -> +
          +
        • Call S(1): +
            +
          • n=1
          • +
          • r=? -> +
              +
            • Call S(0): +
                +
              • N=0
              • +
              • r=0
              • +
              • return 0
              • +
              +
            • +
            +
          • +
          • return 1+S(0)
          • +
          +
        • +
        +
      • +
      • return 2+S(1)
      • +
      +
    • +
    + +

    The call stack at the end:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameValueRemoved
    S<code of S; n=0, r=0>true
    S<code of S; n=1, r=1>true
    S<code of S; n=2, r=3>true
    p3false
    + +

    After the call the S(2) is complete, the entire call stack of S to S to S is gone.

    + +

    There are 2 more slides containing slightly different versions of the same diagrams. +They do not provide any extra information.

    + +

    Recursion on Trees

    + +
      +
    • We will often use recursion & induction on trees. +
        +
      • e.g. the tree rooted a v has some property if its subtrees have some related property
      • +
      +
    • +
    • e.g. the height of node v in a binary tree may be defined by:
    • +
    + +

    h(v)={0 if v is a leaf1+max{h(left(v)),h(right(v))} otherwise +h(v) = \begin{cases} +0 & \text{ if v is a leaf}\\ +1 + \text{max}\{h(\text{left}(v)), h(\text{right}(v))\} & \text{ otherwise} +\end{cases} +

    + +

    (We can define h(left(v)) to be -1 if left(v) does not exist, and sim. for right(v)).

    + +

    Recurssion a Trees Examples

    + +

    height of node v in T:

    + +

    h(v)={0 if v is a leaf1+max{h(left(v)),h(right(v))} ow +h(v) = \begin{cases} +0 & \text{ if v is a leaf}\\ +1+ max\{h(\text{left}(v)),h(\text{right}(v))\} & \text{ ow} +\end{cases} +

    + +

    for the follwing tree: h(v)=?h(v)=?

    + + +
      +
    • + + v + +
        +
      • + + a + +
          +
        • + + e + +
            +
          • + + g + +
          • +
          • + + h + +
          • +
          +
        • +
        • + + f + +
            +
          • + + i + +
          • +
          +
        • +
        +
      • +
      • + + b + +
          +
        • + + c + +
        • +
        • + + d + +
        • +
        +
      • +
      +
    • +
    + + +

    Recursion on Trees Example (pt 2)

    + +

    (See math equation of previous slide)

    + +

    h(v) = 3

    + + +
      +
    • + + v (3) + +
        +
      • + + a (2) + +
          +
        • + + e (1) + +
            +
          • + + g (0) + +
          • +
          • + + h (0) + +
          • +
          +
        • +
        • + + f (1) + +
            +
          • + + i (0) + +
          • +
          +
        • +
        +
      • +
      • + + b (1) + +
          +
        • + + c (0) + +
        • +
        • + + d (0) + +
        • +
        +
      • +
      +
    • +
    + + +

    Pseudo-code version:

    + +
    height(v){
    +  if v is a leaf
    +    return 0
    +  if v has one child u
    +    return 1+height(u)
    +  else
    +    return 1+max(height(left(v)), height(right(v)))
    +}
    +
    + +

    Traversals of Binary Tree

    + +
      +
    • A traversal of a graph is a process that “visits” each node in the graph once.
    • +
    • We consider 4 standard tree traversalt: +
        +
      1. level order
      2. +
      3. pre-order
      4. +
      5. in-order
      6. +
      7. post-order
      8. +
      +
    • +
    • 2,3,4 begin at the root & recursively visit the nodes in each subtree & the root. They vary in the relative ???(can’t read).
    • +
    + +

    (Level order later)

    + +

    Code:

    + +
    pre-order-T(v){
    +  visit v
    +  pre-order-T(left(v))
    +  pre-order-T(right(v))
    +}
    +
    + +

    pre-order-T(v) does nothing if v does not exist.

    + +
      +
    • v is visited before any of its decendants
    • +
    • every node in the left subtree is visited before any node in the right subtree.
    • +
    + +

    Tree to come back to:

    + + +
      +
    • + + A + +
        +
      • + + B + +
          +
        • + + D + +
        • +
        • + + E + +
        • +
        +
      • +
      • + + C + +
          +
        • + + F + +
        • +
        +
      • +
      +
    • +
    + + +

    Pre-order-traversal: A,B,D,E,C,F

    + +

    in-order-T

    + +

    code:

    + +
    in-order-T(v){
    +  in-order-T(left(v))
    +  visit v
    +  in-order-T(right(v))
    +}
    +
    + +

    In order traversal: D,B,E,A,C,F

    + +

    post-order-T

    + +

    code:

    + +
    post-order-T(v){
    +  post-order-T(left(v))
    +  post-order-T(right(v))
    +  visit v
    +}
    +
    + +

    Post order traversal: D,E,B,F,C,A

    + +

    End

    + +

    …some repeated slides… unknown reason

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/09/225 L9 - Recursion On Trees.pdf b/_site/melody/cmpt-225/09/225 L9 - Recursion On Trees.pdf new file mode 100644 index 0000000..58501c3 Binary files /dev/null and b/_site/melody/cmpt-225/09/225 L9 - Recursion On Trees.pdf differ diff --git a/_site/melody/cmpt-225/10/10.html b/_site/melody/cmpt-225/10/10.html new file mode 100644 index 0000000..aa86aa5 --- /dev/null +++ b/_site/melody/cmpt-225/10/10.html @@ -0,0 +1,158 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Transciber’s notes:

    +
      +
    •     \iff is “if and only if”
    • +
    •     \implies is “implies”
    • +
    • \forall is “for all”
    • +
    • \exists is “exists”
    • +
    + +

    Big-Oh–Part I

    + +

    CMPT 225

    + +

    Slide 1

    + +
      +
    • Recall: if f,g are functions f:N->N,g:N->N
    • +
    • f is O(g) means there are constants n0,c>0n_{0},c > 0 +s.t. for every n>n0,f(n)c.g(n)n>n_{0},f(n) \leq \text{c.g}(n)
    • +
    • that is for all but finitely many “small” values of n: f(n)cg(n)f(n) \leq cg(n)
    • +
    • or f grows no faster than g (asymtotically)
    • +
    • we typically write f(n)=O(g(n))f(n) = O(g(n))
    • +
    + +

    Consider f(n),g(n):

    + +

    Claim: f(n)=O(g(n))f(n) = O(g(n))

    + +

    Why:

    + +
      +
    • When f(n)>g(n)f(n) > g(n) for all n?
    • +
    • But f(n)<2g(n)f(n) < 2g(n) for almost all values of n.
    • +
    • choose n0n_{0} to “exclude” the real values of n.
    • +
    • Now: f(n)<2g(n)f(n) < 2g(n) for all n>n0n>n_{0}
    • +
    • So: f(n)=O(g(n))f(n) = O(g(n))
    • +
    + +

    Also:

    + +

    (A graph a cannot transcribe because the notation doesn’t make any sense. All future graphs have complexity on the y axis and the value of n [input to the function] as the x asxis.)

    + +

    Claim: f(n) is not O(g(n))

    + +

    However large we choose c,n0c,n_{0}, there will be some k (larger than n0n_0) s.d.: n>k    f(n)>cg(n)n > k \implies f(n) > c g(n)

    + +

    Consider O(1) (i.e. g(n)=1g(n)=1)

    + +

    f(n)=O(1)f(n) = O(1) if n0,c>0\exists n_{0},c > 0 s.t. n>n0  f(n)c×1\forall n>n_{0} \space \space f(n) \leq c\times 1

    + +

    (Another graph that makes no Goddamn sense whatsoever.)

    + +
      +
    • for every n>n0,f(n)<cn>n_{0}, f(n)<c <– >–>
    • +
    • so, f(n) grwos no faster than a constant
    • +
    • so f(n) is asymptotically bounded by a constant
    • +
    + +

    The constant does not matter

    + +

    Fact: f(n)=O(1)    f(n)=O(1027)    f(n)=O(11027)f(n) = O(1) \iff f(n)=O(\text{10}^{27}) \iff f(n) = O(\frac{1}{\text{10}^{27}})

    + +

    Suppose: f(n)=O(1027)f(n) = O(\text{10}^{27}) *

    + +

    Claim: f(n)f(n) is also O(11027)O(\frac{1}{\text{10}^{27}})

    + +

    * means n0,c>0\exists n_{0},c > 0 s.t. n>n0    f(n)c×1027n > n_{0} \implies f(n) \leq c\times\text{10}^{27}

    + +

    Want to show: n0,c1>0\exists n_{0},c^{1}>0 s.t. n>n0    f(n)c1×11027n>n_{0} \implies f(n) \leq c^{1}\times \frac{1}{\text{10}^{27}}

    + +

    Choose c1c^1 big enough that c111027c×1027c^{1} \frac{1}{\text{10}^{27}} \leq c \times \text{10}^{27}

    + +

    e.g: c1=c×1054c^{1} = c \times \text{10}^{54}

    + +

    Then:

    + +

    n>n0,f(n)c×1027c×10271054c11027 +\forall n>n_{0}, f(n) \leq c \times \text{10}^{27} \\ +\leq c \times \text{10}^{-27} - \text{10}^{54}\\ +\leq c^1 - \text{10}^{-27} +

    + +

    (54-27=27)

    + +

    So: f(n)=O(1027)f(n)=O(\text{10}^{-27})

    + +

    Asymtotic Notation (e.g. Big-Oh)

    + +
      +
    • Is not “about” algorithm
    • +
    • Is a tool for describing (growth of) functions
    • +
    • It is useful for describing functions related to algorithms + data structures, e.g.: +
        +
      • minimum or maximum time taken
      • +
      • minimum or maximum space needed
      • +
      • etc.
      • +
      +
    • +
    • We use it so often for worst-case time for an algorithm that we often leave implciit a statement like “let T(n) be the max time taken by algorithm A as an input of size as most n.” This statement is essential.
    • +
    + +

    Ex. Complexity of Palindrome Checking

    + +
      +
    • using a stack & queue
    • +
    • Algorithm: +
        +
      1. Insert all tokens into a stack & a queue
      2. +
      3. Repeat pop one token; dequeue one token. if different report ‘no’.
      4. +
      +
    • +
    • size of input = number of symbols or token
    • +
    • each token is (all together O(1)): +
        +
      • pushed on the stack, O(1)
      • +
      • enqueued on the queue, O(1)
      • +
      • popped off the stack, O(1)
      • +
      • dequeued from the queue, O(1)
      • +
      • compared to one other token, O(1)
      • +
      +
    • +
    • n tokens    n×O(1)\text{n tokens} \implies n \times O(1) time in total
    • +
    • So: T(n)=n×O(1)=O(n)T(n) = n \times O(1) = O(n)
    • +
    + +

    What does n×O(1)=O(n)n\times O(1) = O(n) mean?

    + +

    It means: f(n)=O(1)    n×f(n)=O(n)f(n) = O(1) \iff n\times f(n) = O(n)

    + +

    To see if it is true:

    + +

    f(n)=O(1)    c>0 s.t. f(n)<c,for any nZ    c>0 s.t. n×f(n)<c×n,for any nN    n×f(n)=O(n) +f(n) = O(1) \iff \exists c>0 \space \text{s.t.} \space f(n) < c, \text{for any} \space n \in \Z\\ +\iff \exists c>0 \text{ s.t. } n\times f(n) < c\times n, \text{for any } n \in \N\\ +\iff n\times f(n) = O(n) +

    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/10/10/index.html b/_site/melody/cmpt-225/10/10/index.html new file mode 100644 index 0000000..aa86aa5 --- /dev/null +++ b/_site/melody/cmpt-225/10/10/index.html @@ -0,0 +1,158 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Transciber’s notes:

    +
      +
    •     \iff is “if and only if”
    • +
    •     \implies is “implies”
    • +
    • \forall is “for all”
    • +
    • \exists is “exists”
    • +
    + +

    Big-Oh–Part I

    + +

    CMPT 225

    + +

    Slide 1

    + +
      +
    • Recall: if f,g are functions f:N->N,g:N->N
    • +
    • f is O(g) means there are constants n0,c>0n_{0},c > 0 +s.t. for every n>n0,f(n)c.g(n)n>n_{0},f(n) \leq \text{c.g}(n)
    • +
    • that is for all but finitely many “small” values of n: f(n)cg(n)f(n) \leq cg(n)
    • +
    • or f grows no faster than g (asymtotically)
    • +
    • we typically write f(n)=O(g(n))f(n) = O(g(n))
    • +
    + +

    Consider f(n),g(n):

    + +

    Claim: f(n)=O(g(n))f(n) = O(g(n))

    + +

    Why:

    + +
      +
    • When f(n)>g(n)f(n) > g(n) for all n?
    • +
    • But f(n)<2g(n)f(n) < 2g(n) for almost all values of n.
    • +
    • choose n0n_{0} to “exclude” the real values of n.
    • +
    • Now: f(n)<2g(n)f(n) < 2g(n) for all n>n0n>n_{0}
    • +
    • So: f(n)=O(g(n))f(n) = O(g(n))
    • +
    + +

    Also:

    + +

    (A graph a cannot transcribe because the notation doesn’t make any sense. All future graphs have complexity on the y axis and the value of n [input to the function] as the x asxis.)

    + +

    Claim: f(n) is not O(g(n))

    + +

    However large we choose c,n0c,n_{0}, there will be some k (larger than n0n_0) s.d.: n>k    f(n)>cg(n)n > k \implies f(n) > c g(n)

    + +

    Consider O(1) (i.e. g(n)=1g(n)=1)

    + +

    f(n)=O(1)f(n) = O(1) if n0,c>0\exists n_{0},c > 0 s.t. n>n0  f(n)c×1\forall n>n_{0} \space \space f(n) \leq c\times 1

    + +

    (Another graph that makes no Goddamn sense whatsoever.)

    + +
      +
    • for every n>n0,f(n)<cn>n_{0}, f(n)<c <– >–>
    • +
    • so, f(n) grwos no faster than a constant
    • +
    • so f(n) is asymptotically bounded by a constant
    • +
    + +

    The constant does not matter

    + +

    Fact: f(n)=O(1)    f(n)=O(1027)    f(n)=O(11027)f(n) = O(1) \iff f(n)=O(\text{10}^{27}) \iff f(n) = O(\frac{1}{\text{10}^{27}})

    + +

    Suppose: f(n)=O(1027)f(n) = O(\text{10}^{27}) *

    + +

    Claim: f(n)f(n) is also O(11027)O(\frac{1}{\text{10}^{27}})

    + +

    * means n0,c>0\exists n_{0},c > 0 s.t. n>n0    f(n)c×1027n > n_{0} \implies f(n) \leq c\times\text{10}^{27}

    + +

    Want to show: n0,c1>0\exists n_{0},c^{1}>0 s.t. n>n0    f(n)c1×11027n>n_{0} \implies f(n) \leq c^{1}\times \frac{1}{\text{10}^{27}}

    + +

    Choose c1c^1 big enough that c111027c×1027c^{1} \frac{1}{\text{10}^{27}} \leq c \times \text{10}^{27}

    + +

    e.g: c1=c×1054c^{1} = c \times \text{10}^{54}

    + +

    Then:

    + +

    n>n0,f(n)c×1027c×10271054c11027 +\forall n>n_{0}, f(n) \leq c \times \text{10}^{27} \\ +\leq c \times \text{10}^{-27} - \text{10}^{54}\\ +\leq c^1 - \text{10}^{-27} +

    + +

    (54-27=27)

    + +

    So: f(n)=O(1027)f(n)=O(\text{10}^{-27})

    + +

    Asymtotic Notation (e.g. Big-Oh)

    + +
      +
    • Is not “about” algorithm
    • +
    • Is a tool for describing (growth of) functions
    • +
    • It is useful for describing functions related to algorithms + data structures, e.g.: +
        +
      • minimum or maximum time taken
      • +
      • minimum or maximum space needed
      • +
      • etc.
      • +
      +
    • +
    • We use it so often for worst-case time for an algorithm that we often leave implciit a statement like “let T(n) be the max time taken by algorithm A as an input of size as most n.” This statement is essential.
    • +
    + +

    Ex. Complexity of Palindrome Checking

    + +
      +
    • using a stack & queue
    • +
    • Algorithm: +
        +
      1. Insert all tokens into a stack & a queue
      2. +
      3. Repeat pop one token; dequeue one token. if different report ‘no’.
      4. +
      +
    • +
    • size of input = number of symbols or token
    • +
    • each token is (all together O(1)): +
        +
      • pushed on the stack, O(1)
      • +
      • enqueued on the queue, O(1)
      • +
      • popped off the stack, O(1)
      • +
      • dequeued from the queue, O(1)
      • +
      • compared to one other token, O(1)
      • +
      +
    • +
    • n tokens    n×O(1)\text{n tokens} \implies n \times O(1) time in total
    • +
    • So: T(n)=n×O(1)=O(n)T(n) = n \times O(1) = O(n)
    • +
    + +

    What does n×O(1)=O(n)n\times O(1) = O(n) mean?

    + +

    It means: f(n)=O(1)    n×f(n)=O(n)f(n) = O(1) \iff n\times f(n) = O(n)

    + +

    To see if it is true:

    + +

    f(n)=O(1)    c>0 s.t. f(n)<c,for any nZ    c>0 s.t. n×f(n)<c×n,for any nN    n×f(n)=O(n) +f(n) = O(1) \iff \exists c>0 \space \text{s.t.} \space f(n) < c, \text{for any} \space n \in \Z\\ +\iff \exists c>0 \text{ s.t. } n\times f(n) < c\times n, \text{for any } n \in \N\\ +\iff n\times f(n) = O(n) +

    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/10/225 L10 - Big-Oh.pdf b/_site/melody/cmpt-225/10/225 L10 - Big-Oh.pdf new file mode 100644 index 0000000..ca17656 Binary files /dev/null and b/_site/melody/cmpt-225/10/225 L10 - Big-Oh.pdf differ diff --git a/_site/melody/cmpt-225/11/11.html b/_site/melody/cmpt-225/11/11.html new file mode 100644 index 0000000..ca6e594 --- /dev/null +++ b/_site/melody/cmpt-225/11/11.html @@ -0,0 +1,2089 @@ + + + + + | tait.tech + + + + + +
    +
    +

    Binary Search Trees

    + +

    CMPT 225

    + + + +
      +
    • Set: unordered collection of values/objects.
    • +
    • Operations: +
        +
      • insert(x) // add x to set
      • +
      • member(x) // check if x in set. a.k.a. find(x), search(x), lookup(x), …
      • +
      • remove(x) // remove x from set
      • +
      • size() // get size of set
      • +
      • empty() // is set empty
      • +
      • clear() // remove all elements (i.e., make set empty)
      • +
      +
    • +
    • We call the values we store keys,
    • +
    • We assume the keys are from some unordered set S. +
        +
      • i.e. for any two keys x,ySx,y\in S, we have exactly one of x<y,x=y,y<xx< y,x=y,y< x
      • +
      +
    • +
    • What implementaions where all operations are efficient/fast +
        +
      • Q: What will count as “fast”?
      • +
      +
    • +
    + + + +

    Consider time complexity of operations ofr simple list + array implementations!

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    typeinsertfindremove
    un-ordered array(green) O(1)O(n)O(n)
    ordered array(red) O(n)(purple outline) O(log n)(red) O(n)
    un-ordered linked list(green) O(1)(red) O(n)(red) O(n)
    ordered linked list(red) O(n)(red) O(n)(red) O(n)
    + +

    Q: What will count as “fast”?

    + +

    A: Time O(log n) //n is size of set

    + + + +
      +
    • Multiset: like set, but with multiplicities (aka bag) +
        +
      • count(x)
      • +
      +
    • +
    • Map: unordered collection of >key,value< pairs, associating at most one value with each key. (e.g. partial function keys -> values) +
        +
      • put(key,val) // in place of insert(x)
      • +
      • get(key) // return value associated with key
      • +
      +
    • +
    • Dictionary: like map, but associates a collection of values with each key.
    • +
    + +

    Implementations of these are simple abstractions to implementations of sets, which we focus on.

    + +

    Binary Search Tree (B.S.T.)

    + +

    A BST is:

    + +
      +
    • a binary tree // a structure invariant
    • +
    • with nodes labled by keys
    • +
    • satisfying the following order invariant: for every two nodes u,v: +
        +
      • if u is in left subtree of v, then key(u)<key(v)\text{key}(u) < \text{key}(v)
      • +
      • if u is in the right subtree of v, then key(u)>key(v)\text{key}(u) > \text{key}(v)
      • +
      +
    • +
    + +

    Ex.

    + +

    Example 1 (checked):

    + + +
      +
    • + + 5 + +
        +
      • + + 3 + +
      • +
      • + + 6 + +
      • +
      +
    • +
    + + +

    Example 2 (X):

    + + +
      +
    • + + 5 + +
        +
      • + + 3 + +
          +
        • + + 2 + +
        • +
        • + + 6 + +
        • +
        +
      • +
      • + + 8 + +
      • +
      +
    • +
    + + +

    Example 3 (check) (right and left are written explicitly when there are not two nodes to write. Otherwise, left is written first and right is listed second.):

    + + +
      +
    • + + 5 + +
        +
      • + + 10 (right) + +
          +
        • + + 20 (right) + +
            +
          • + + 30 (right) + +
              +
            • + + 25 (left) + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Example 4 (check):

    + + +
      +
    • + + 5 + +
    • +
    + + +

    Every sub-tree of a BST is a BST

    + + +
      +
    • + + 500 + +
        +
      • + + 200 + +
          +
        • + + 100 + +
        • +
        • + + 300 + +
            +
          • + + 250 + +
          • +
          • + + 350 + +
          • +
          +
        • +
        +
      • +
      • + + 700 + +
          +
        • + + 600 + +
            +
          • + + 650 (keys in this subtree would be >600 , <700) + +
          • +
          • + + 560 + +
          • +
          +
        • +
        • + + 800 + +
        • +
        +
      • +
      +
    • +
    + + +

    This makes recursive algorithms very nature.

    + +

    Fact:

    + +

    In-order traversal of a BST visits keys in non-decreasing order.

    + +

    Proof sketch:

    + +
      +
    • Basic: h=0h=0, so one node
    • +
    • I.H.: the claim holds for all trees of heighth\text{height} \leq h
    • +
    • I.S.: T is: v with left tree of A and right tree of B. (A, B may b e empty)
    • +
    • We: +
        +
      1. traverse A, visiting keys in sequence: a1,a2,aka_{1}, a_{2}, \dots a_{k}
      2. +
      3. visit v
      4. +
      5. tranverse B, visiting keys in sequence b1,b2,bmb_{1}, b_{2}, \dots b_{m}
      6. +
      +
    • +
    • Overall, we visit: a1,a2,ak,b1,b2,bma_1, a_2, \dots a_k, b_1, b_2, \dots b_m
    • +
    • By I.E. a1a2aka_1 \leq a_2 \leq \dots \leq a_k
    • +
    • + b1,b2bmb_1, \leq b_2 \leq \dots \leq b_m +
    • +
    • Because T is a BST, so:
    • +
    + +

    akkey(v)<b1a1a2akkeys(v)b1b2bm +a_k \leq \text{key}(v) < b_1\\ +\therefore a_1 \leq a_2 \leq \dots \leq a_k \leq \text{keys}(v) \leq b_1 \leq b_2 \dots \leq b_m +

    + +

    BST Find/Search: examples

    + +

    (Trasncriber’s note: the links are the search path for the algorithms)

    + +

    find(5):

    + + +
      +
    • + + 3 + +
        +
      • + + 2 + +
      • +
      • + + 8 + +
          +
        • + + 5 (check) + +
            +
          • + + 4 + +
          • +
          • + + 6 + +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      +
    • +
    + + +

    find(1):

    + + +
      +
    • + + 3 + +
        +
      • + + 2 + +
      • +
      • + + 8 + +
          +
        • + + 5 + +
            +
          • + + 4 + +
          • +
          • + + 6 (X) + +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      +
    • +
    + + +

    Find 6:

    + + +
      +
    • + + 5 + +
        +
      • + + 8 (right, X) + +
          +
        • + + 10 (right) + +
        • +
        +
      • +
      +
    • +
    + + +

    Some notation:

    + +

    Suppose v is a node of BST. We write:

    + +
      +
    • left(v) = left child of v
    • +
    • right(v) = right child of v
    • +
    • key(v) = key labelling v
    • +
    • node(v) node v s.t. key(v)=x
    • +
    + +

    BSD find(x) Pseudo-code

    + +
    find(x){// return true iff t is in the tree.
    +  return find(t,root)
    +}
    +
    +find(t,v)// return true if t appears in ubstree rooted at v.
    +{
    +  if t < key(v) & v has a left subtree
    +    return find(t, left(v))
    +  if t > key(v) & v has a right subtree
    +    return find(t, right(v))
    +  if key(v) = t
    +    return true
    +  return false //v is a leaf, does not have t
    +}
    +
    + +

    BST find(t,v) pseudo-code – alternate version

    + +
    find(t,v) // return true if t appears in subtree rooted at v
    +{
    +  if key(v)=t
    +    return true
    +  if t < key(v) & v has a left subtree
    +    return find(t,left(v))
    +  if t > key(v) & v has a right subtree
    +    return find(t,right(v))
    +  return false
    +}
    +
    + +

    Q: Which version is better?

    + +

    A: key(v)=t will almsot always be false, so the first return should do fewer comparisons and usually be false.

    + +

    BST insert(x) Pseudo-code

    + +
    insert(t){
    +  // adds t to the tree
    +  // assumes t is not in the tree already*
    +  u <- node at which find(t,root) terminates**
    +  if t<key(u)
    +    give u a new left child with key t.
    +  else
    +    give u a new right child with key t.
    +}
    +
    + +

    * Excersise: Write the version that does not make this assumption.

    + +

    ** Excersise: Write the version where the search is excplicit.

    + +

    BST Insert Examples

    + +

    insert(1):

    + + +
      +
    • + + 3 + +
        +
      • + + 2 + +
          +
        • + + 1 (inserted) + +
        • +
        +
      • +
      • + + 8 + +
          +
        • + + 5 + +
            +
          • + + 4 + +
          • +
          • + + 6 + +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      +
    • +
    + + +

    insert(7):

    + + +
      +
    • + + 3 + +
        +
      • + + 2 + +
      • +
      • + + 8 + +
          +
        • + + 5 + +
            +
          • + + 4 + +
          • +
          • + + 6 + +
              +
            • + + 7 (right) + +
            • +
            +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      +
    • +
    + + +

    BST insert(x) Pseudo-code – explicit search version…

    + +
    insert(t){ //adds t to the tree if it is not already there
    +  insert(t, root)
    +}
    +insert(t,v) //insert t in the subtree rooted at v, if it is not there
    +{
    +  if t < key(v) & v has a left subtree
    +    insert(t, left(v))
    +  if t > key(v) & v has a right subtree
    +    insert(t, right(v))
    +  if t < key(v) //here v has no left child
    +    give v a new left child with key t
    +  if t > key(v) //here v has no right child
    +    give v a new right child with key t.
    +  // if we reach here, t=key(v), so do nothing.
    +}
    +
    + +

    Insertion Over for BSTs: Examples

    + +

    1)

    + +
      +
    • start with an empty BST
    • +
    • insert 5,2,3,7,8,1,6 in the given order
    • +
    + + +
      +
    • + + 5 + +
        +
      • + + 2 + +
          +
        • + + 1 + +
        • +
        • + + 3 + +
        • +
        +
      • +
      • + + 7 + +
          +
        • + + 6 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    2)

    + +
      +
    • start with an empty BST
    • +
    • insert 1,2,3,5,6,7,8 in the order given
    • +
    + + +
      +
    • + + 1 + +
        +
      • + + 2 (right) + +
          +
        • + + 3 (right) + +
            +
          • + + 4 (right) + +
              +
            • + + 5 (right) + +
                +
              • + + 6 (right) + +
                  +
                • + + 7 (right) + +
                    +
                  • + + 8 (right) + +
                  • +
                  +
                • +
                +
              • +
              +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Notes

    + +
      +
    • Insertion order affects the shape of a BST.
    • +
    • Removal order can too.
    • +
    + +

    BST remove(t)

    + +

    We consider 3 cases, increasing difficulty.

    + +
      +
    1. Case 1: t is at a leaf (example figure #1): +
        +
      1. find the node v with key(v)=t
      2. +
      3. delete v
      4. +
      +
    2. +
    3. Case 2: t is a node with 1 child (example figure #2 and example figure #3) +
        +
      1. find the node v with key(v)=t
      2. +
      3. let u be the child of v
      4. +
      5. replace v with the subtree rooted at u
      6. +
      +
    4. +
    5. For case 3, see the next section
    6. +
    + +

    Example Figure #1

    + +

    remove(7):

    + + +
      +
    • + + 5 + +
        +
      • + + 3 + +
          +
        • + + 2 (left) + +
        • +
        +
      • +
      • + + 8 + +
          +
        • + + 7 (Xed out) + +
        • +
        • + + 9 + +
        • +
        +
      • +
      +
    • +
    + + +

    Example Figure #2

    + +

    remove(3)

    + +

    step 1 (original)

    + + +
      +
    • + + 5 + +
        +
      • + + 3 (Xed out) + +
          +
        • + + 1 (left) + +
        • +
        +
      • +
      • + + 10 + +
      • +
      +
    • +
    + + +

    step 2

    + + +
      +
    • + + 5 + +
        +
      • + + 10 (right) + +
      • +
      +
    • +
    + + + +
      +
    • + + 1 + +
    • +
    + + +

    step 3

    + + +
      +
    • + + 5 + +
        +
      • + + 1 + +
      • +
      • + + 10 + +
      • +
      +
    • +
    + + +

    Example Figure #4

    + +

    remove(10)

    + +

    step 1 (original)

    + + +
      +
    • + + 4 + +
        +
      • + + 2 + +
      • +
      • + + 10 (Xed out) + +
          +
        • + + 7 (left) + +
            +
          • + + 6 + +
              +
            • + + 5 (left) + +
            • +
            +
          • +
          • + + 8 + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    step 2

    + + +
      +
    • + + 4 + +
        +
      • + + 2 (left) + +
      • +
      +
    • +
    + + +
      +
    • + + 7 + +
        +
      • + + 6 + +
          +
        • + + 5 (left + +
        • +
        +
      • +
      • + + 8 + +
      • +
      +
    • +
    + + +

    step 3

    + + +
      +
    • + + 4 + +
        +
      • + + 2 + +
      • +
      • + + 7 + +
          +
        • + + 6 + +
            +
          • + + 5 (left + +
          • +
          +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    BST remove: Case 3 Preperation: Successors

    + +
      +
    • In an ordered collection X=si1,si,si+1,si+2X=\langle \cdots s_{i-1}, s_{i}, s_{i+1}, s_{i+2} \cdots \rangle +
        +
      • si1s_{i-1} is the predocessor of sis_{i}
      • +
      • si+1s_{i+1} is the successor if sis_{i}
      • +
      • Write: succx(si)=si+1\text{succ}_{x}(s_{i}) = s_{i+1}
      • +
      +
    • +
    • Let V=v1,vnV=\langle v_{1},\cdots v_{n}\rangle be the nodes of the tree ordered as per an in-order traversal.
    • +
    • Let K=k1,,knK=\langle k_{1},\cdots ,k_{n}\rangle$$ be the keys, in non-decreasing order.
    • +
    • Then: y=key(u)    succk(y)=key(succv(u))y=\text{key}(u) \implies \text{succ}_{k}(y) = \text{key}(\text{succ}_{v}(u)) i.e., the next node has the next key.
    • +
    + +

    BST remove: Case 3 Preperation: Successorts in BSTs

    + +
      +
    • If S is a set of keys, and xSx\in S, then the successor of x in S is the smallest value yS s.t. x<yy\in S \text{ s.t. } x< y. +Ex. S={19,27,8,3,12},succ(8)=12,succ(12)=19,S=\{ 19, 27, 8, 3, 12 \}, \text{succ}(8)=12, \text{succ}(12)=19, \cdots (S={3,8,12,19,27})(S=\{3,8,12,19,27\})
    • +
    • In a BST, in-order traversal visits keys in order. +
        +
      • Let S be the set of keys in BST T.
      • +
      • the successor of x in S is key(u)\text{key}(u) where u is the node of T that an in-order traversal of T visits next after v.
      • +
      +
    • +
    + + +
      +
    • + + 5 + +
        +
      • + + 3 + +
          +
        • + + 2 (left) + +
        • +
        +
      • +
      • + + 8 + +
          +
        • + + 7 + +
        • +
        • + + 9 + +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • If v is a node of BST T, then we can say the successor of v in T is the node of T visited just after v by an in-order traversal of T. Then: succ(x)=key(succ(node(x)))\text{succ}(x)=\text{key}(\text{succ}(\text{node}(x)))
    • +
    • Or: if key(v)=x\text{key}(v)=x, we can find the successor of x by finding the successor node of v, and getting its key: succ(key(v))=key(succ(v))\text{succ}(\text{key}(v)) = \text{key}(\text{succ}(v))
    • +
    + +

    BST remove: Case 3 Preperation: Successors

    + +

    If node v has a right child, it is easy to find its successor: succ(v)\text{succ}(v) is the first node visited by an in-order traversal of the right subtree of v.

    + +

    Ex. 6 diagrams. All of which give v a right subtree, one of one node, one of one node with a left child, one with a left leaf and right subtree of its own, and three variations on arbitrary numbers of children attached to the left node of v.

    + +

    To find the successor of node v that has a right child, use:

    + +
    succ(v){
    +  u<-right(v)
    +  while(left(u) exists){
    +    u<-left(u)
    +  }
    +  return u
    +}
    +
    + +

    BST remove(t)

    + +

    Case 3: t is at a node with 2 children:

    + +
      +
    1. find the node v with key(v)=t
    2. +
    3. find the successor of v – call it u.
    4. +
    5. key(v)<-key(u) //replace t with succ(t) at v.
    6. +
    7. delete u: +
        +
      1. if u is a leaf, delete it.
      2. +
      3. if u is not a leaf, it has one child w, replace u with the subtree rooted at w.
      4. +
      +
    8. +
    + +

    Notice: 4.1 is like case 1; 4.2 is like case 2.

    + +

    BST remove(k) when node(k) has two children

    + +

    Ex. to remove 5:

    + +
      +
    1. Find 5
    2. +
    3. Find successor of 5
    4. +
    5. Replace 5 with its succ.
    6. +
    7. In this example, succ(5) has no children so just delete the node where it was.
    8. +
    + +

    Example tree:

    + + +
      +
    • + + 20 (link starts step 1.) + +
        +
      • + + 15 + +
          +
        • + + 5 (left; Xed out; link starts step 2.) + +
            +
          • + + 2 + +
          • +
          • + + 10 + +
              +
            • + + 7 + +
                +
              • + + 6 (successor of 5) + +
              • +
              • + + 8 + +
              • +
              +
            • +
            • + + 12 + +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + 25 + +
          +
        • + + 22 + +
        • +
        • + + 26 + +
        • +
        +
      • +
      +
    • +
    + + +

    After switching 5 and succ(5):

    + +

    (transcriber’s note: may be incorrect, but I’m writing what’s there)

    + + +
      +
    • + + 20 + +
        +
      • + + 6 + +
          +
        • + + 2 + +
        • +
        • + + 10 + +
            +
          • + + 7 + +
              +
            • + + 8 (right) + +
            • +
            +
          • +
          • + + 12 + +
          • +
          +
        • +
        +
      • +
      • + + ... + +
      • +
      +
    • +
    + + +

    Example tree 2:

    + +

    To remove 6:

    + +
      +
    1. Find 6
    2. +
    3. Find successor of 6
    4. +
    5. Replace 6 with its successor
    6. +
    7. Replace succ(6) with its non-empty subtree
    8. +
    + +

    Tree:

    + + +
      +
    • + + 30 + +
        +
      • + + 2 + +
          +
        • + + 1 + +
        • +
        • + + 6 (crossed out; link starts step 2.) + +
            +
          • + + ... + +
          • +
          • + + 14 + +
              +
            • + > + 11 + +
                +
              • + + 7 (succ of 6) + +
                  +
                • + + 9 (right; subtree of succ(6)) + +
                    +
                  • + + 8 (subtree of succ(6)) + +
                  • +
                  • + + 10 (subtree of succ(6)) + +
                  • +
                  +
                • +
                +
              • +
              • + + 12 + +
              • +
              +
            • +
            • + + ... + +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + ... + +
      • +
      +
    • +
    + + +

    Becomes, by step 4:

    + + +
      +
    • + + 30 + +
        +
      • + + 2 + +
          +
        • + + 1 + +
        • +
        • + + 7 + +
            +
          • + + ... + +
          • +
          • + + 14 + +
              +
            • + + 11 + +
                +
              • + + 9 + +
                  +
                • + + 8 + +
                • +
                • + + 10 + +
                • +
                +
              • +
              • + + 12 + +
              • +
              +
            • +
            • + + ... + +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + ... + +
      • +
      +
    • +
    + + +

    Complexity of BST Operations

    + +
      +
    • Measure as a function of: height (h) or size/# of keys (n).
    • +
    • All operations essentially involve traversing a path from the root to a node v, where in the worst case v is a leaf of maximum depth.
    • +
    • So: +
        +
      • find: O(h), O(n)
      • +
      • insert: O(h), O(n)
      • +
      • remove: O(h), O(n)
      • +
      +
    • +
    • For “short bushy” trees (e.g. T1) h is small relative in n.
    • +
    • For “tall skinny” trees (e.g. T2) h is proportional to n.
    • +
    + +

    Q: Can we always have short bushy BSTs?

    + +

    T1 h=? h = ?

    + + +
      +
    • + + node + +
        +
      • + + node + +
          +
        • + + node + +
        • +
        • + + node + +
        • +
        +
      • +
      • + + node + +
          +
        • + + node + +
        • +
        • + + node + +
        • +
        +
      • +
      +
    • +
    + + +

    T2 hnh \cong n

    + + +
      +
    • + + node + +
        +
      • + + node + +
          +
        • + + node + +
            +
          • + + ... + +
              +
            • + + node + +
                +
              • + + node + +
              • +
              +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Perfect Binary Tree

    + +
      +
    • A perfect binary tree of height h is a binary tree of height h with the max number of nodes:
    • +
    + +

    1 (yes):

    + + +
      +
    • + + node + +
    • +
    + + +

    2 (no):

    + + +
      +
    • + + node + +
        +
      • + + node (left) + +
      • +
      +
    • +
    + + +

    3 (yes):

    + + +
      +
    • + + node + +
        +
      • + + node + +
      • +
      • + + node + +
      • +
      +
    • +
    + + +

    4 (yes):

    + + +
      +
    • + + node + +
        +
      • + + node + +
          +
        • + + node + +
        • +
        • + + node + +
        • +
        +
      • +
      • + + node + +
          +
        • + + node + +
        • +
        • + + node + +
        • +
        +
      • +
      +
    • +
    + + +

    5 (no):

    + + +
      +
    • + + node + +
        +
      • + + node + +
          +
        • + + node + +
            +
          • + + node + +
          • +
          • + + node + +
          • +
          +
        • +
        • + + node + +
            +
          • + + node + +
          • +
          • + + node + +
          • +
          +
        • +
        +
      • +
      • + + node + +
          +
        • + + node + +
            +
          • + + node (right) + +
          • +
          +
        • +
        • + + node + +
            +
          • + + node + +
          • +
          • + + node + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    6 (no):

    + + +
      +
    • + + node + +
        +
      • + + node (right) + +
          +
        • + + node (right) + +
            +
          • + + node (right) + +
              +
            • + + node (right) + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • Claim: Every perfect binary tree of height h has 2htl12^{\text{htl}}-1 nodes.
    • +
    • Pg: By induction on h, or on the structure of the tree.
    • +
    • Basis: If h=0, there is one node (the root). We have 2htl1=211=12^{\text{htl}}-1 = 2^{1}-1=1 as required.
    • +
    • I.H.: Let kok \geq o, and assume that every perfect binary tree of height k has 2kh12^{kh}-1 nodes.
    • +
    • T.S.: (Need to show a plot of height k+1 has 2(k+1)+112^{(k+1)+1}-1 nodes). A perfect binary tree of height k+1 is constructed as: k is height of left (A) or right (B) subtree; k+1 is the height of the subtree plus one (the root). Where A,B are perfect binary trees of height k. +By I.H. they have 2k+112^{k+1}-1 nodes. +So, the tree has 2k+11+2k+11+1=2×2k+11=2(k+1)+112^{k+1}-1 + 2^{k+1}-1 + 1 = 2\times 2^{k+1} -1 = 2^{(k+1)+1}-1, as required.
    • +
    + +

    Existance of Optimal BSTs

    + +

    Claim: For every set S of n keys, there exists a BST for S with height at most 1+log2n1+\log_{2} n

    + +

    Proof: Let h be the smallest integer s.t. 2hn2^{h} \geq n, and let m=2hm=2^{h}. +So,

    + +

    2hn>2h1log22hlog2n>log22h1hlog2n>h1h<1+log2n +2^{h} \geq n > 2^{h-1}\\ +\log_{2} 2^{h} \geq \log_{2} n > \log_{2} 2^{h-1}\\ +h \geq \log_{2} n > h-1\\ +h < 1+\log_{2} n +

    + +

    let T be the perfect binary tree of height h

    + +

    Label the first n nodes of T (as visited by an in-order traversal) with the +keys of S, and delete the remaining +ndoes (to get T1T^{1}).

    + +

    T1T^{1} is a BST for S with height h<1+log2nh< 1+\log_{2} n

    + +

    So, there is always a BST with height O(logn)O(\log n).

    + +

    Optimal BST Insertion Order

    + +

    Given a set of keys, we can insert them so as to get a minimum height BST:

    + +

    Consider:

    + +

    Graph of a perfect tree, with height of 4. Every node has two children, except for the 8 leafs.

    + +

    What can we say about the key at the root? It is the median key.

    + +

    Observe: the first key inserted into a BST is at the root forever (unless we remove it from the BST).

    + +

    Given a set of keys, we can insert them to get a minimum height BST:

    + +

    (transcriber’s note: I may have done this wrong, the drawing of the following tree is very weird.)

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
      • +
      • + + 2 + +
      • +
      +
    • +
    + + +

    * apply the “root is the median key” principle to each subtree.

    + +

    So, there is always a BST with height logn\cong\log n

    + +

    Can we maintain min. height with O(logn)O(\log n) as we insert and remove keys?

    + +

    Consider A:

    + + +
      +
    • + + 5 + +
        +
      • + + 3 + +
          +
        • + + 2 + +
        • +
        • + + 4 + +
        • +
        +
      • +
      • + + 7 + +
          +
        • + + 6 (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    insert(1) would make it become B:

    + + +
      +
    • + + 4 + +
        +
      • + + 2 + +
          +
        • + + 1 + +
        • +
        • + + 3 + +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 5 + +
        • +
        • + + 7 + +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • B is the only min height BST for 1..7.
    • +
    • A -> B requires “moving every node”
    • +
    • To get O(logn)O(\log n) operations, we need antoher kind of search tree, other than plain BSTs.
    • +
    • To get efficient search trees, give up at least one of: +
        +
      • binary
      • +
      • min height
      • +
      +
    • +
    • Next: self-balancing search trees.
    • +
    + +

    End (transciber’s note: not the end)

    + +

    (some repeated slides and graphics)

    + +

    Notice:

    + +

    Because a perfect binary tree of height h has:

    + +
      +
    • h height
    • +
    • 2h+112^{h+1}-1 nodes
    • +
    • 2h12^{h}-1 internal nodes (nodes with children)
    • +
    • 2h2^h leaves
    • +
    + +

    Then: 2h+2h1=2×2h1=2h+112^{h} + 2^{h}-1 = 2\times 2^{h}-1 = 2^{h+1}-1

    + +

    Actual end

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/11/11/index.html b/_site/melody/cmpt-225/11/11/index.html new file mode 100644 index 0000000..da155a8 --- /dev/null +++ b/_site/melody/cmpt-225/11/11/index.html @@ -0,0 +1,2091 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Binary Search Trees

    + +

    CMPT 225

    + + + +
      +
    • Set: unordered collection of values/objects.
    • +
    • Operations: +
        +
      • insert(x) // add x to set
      • +
      • member(x) // check if x in set. a.k.a. find(x), search(x), lookup(x), …
      • +
      • remove(x) // remove x from set
      • +
      • size() // get size of set
      • +
      • empty() // is set empty
      • +
      • clear() // remove all elements (i.e., make set empty)
      • +
      +
    • +
    • We call the values we store keys,
    • +
    • We assume the keys are from some unordered set S. +
        +
      • i.e. for any two keys x,ySx,y\in S, we have exactly one of x<y,x=y,y<xx< y,x=y,y< x
      • +
      +
    • +
    • What implementaions where all operations are efficient/fast +
        +
      • Q: What will count as “fast”?
      • +
      +
    • +
    + + + +

    Consider time complexity of operations ofr simple list + array implementations!

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    typeinsertfindremove
    un-ordered array(green) O(1)O(n)O(n)
    ordered array(red) O(n)(purple outline) O(log n)(red) O(n)
    un-ordered linked list(green) O(1)(red) O(n)(red) O(n)
    ordered linked list(red) O(n)(red) O(n)(red) O(n)
    + +

    Q: What will count as “fast”?

    + +

    A: Time O(log n) //n is size of set

    + + + +
      +
    • Multiset: like set, but with multiplicities (aka bag) +
        +
      • count(x)
      • +
      +
    • +
    • Map: unordered collection of >key,value< pairs, associating at most one value with each key. (e.g. partial function keys -> values) +
        +
      • put(key,val) // in place of insert(x)
      • +
      • get(key) // return value associated with key
      • +
      +
    • +
    • Dictionary: like map, but associates a collection of values with each key.
    • +
    + +

    Implementations of these are simple abstractions to implementations of sets, which we focus on.

    + +

    Binary Search Tree (B.S.T.)

    + +

    A BST is:

    + +
      +
    • a binary tree // a structure invariant
    • +
    • with nodes labled by keys
    • +
    • satisfying the following order invariant: for every two nodes u,v: +
        +
      • if u is in left subtree of v, then key(u)<key(v)\text{key}(u) < \text{key}(v)
      • +
      • if u is in the right subtree of v, then key(u)>key(v)\text{key}(u) > \text{key}(v)
      • +
      +
    • +
    + +

    Ex.

    + +

    Example 1 (checked):

    + + +
      +
    • + + 5 + +
        +
      • + + 3 + +
      • +
      • + + 6 + +
      • +
      +
    • +
    + + +

    Example 2 (X):

    + + +
      +
    • + + 5 + +
        +
      • + + 3 + +
          +
        • + + 2 + +
        • +
        • + + 6 + +
        • +
        +
      • +
      • + + 8 + +
      • +
      +
    • +
    + + +

    Example 3 (check) (right and left are written explicitly when there are not two nodes to write. Otherwise, left is written first and right is listed second.):

    + + +
      +
    • + + 5 + +
        +
      • + + 10 (right) + +
          +
        • + + 20 (right) + +
            +
          • + + 30 (right) + +
              +
            • + + 25 (left) + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Example 4 (check):

    + + +
      +
    • + + 5 + +
    • +
    + + +

    Every sub-tree of a BST is a BST

    + + +
      +
    • + + 500 + +
        +
      • + + 200 + +
          +
        • + + 100 + +
        • +
        • + + 300 + +
            +
          • + + 250 + +
          • +
          • + + 350 + +
          • +
          +
        • +
        +
      • +
      • + + 700 + +
          +
        • + + 600 + +
            +
          • + + 650 (keys in this subtree would be >600 , <700) + +
          • +
          • + + 560 + +
          • +
          +
        • +
        • + + 800 + +
        • +
        +
      • +
      +
    • +
    + + +

    This makes recursive algorithms very nature.

    + +

    Fact:

    + +

    In-order traversal of a BST visits keys in non-decreasing order.

    + +

    Proof sketch:

    + +
      +
    • Basic: h=0h=0, so one node
    • +
    • I.H.: the claim holds for all trees of heighth\text{height} \leq h
    • +
    • I.S.: T is: v with left tree of A and right tree of B. (A, B may b e empty)
    • +
    • We: +
        +
      1. traverse A, visiting keys in sequence: a1,a2,aka_{1}, a_{2}, \dots a_{k}
      2. +
      3. visit v
      4. +
      5. tranverse B, visiting keys in sequence b1,b2,bmb_{1}, b_{2}, \dots b_{m}
      6. +
      +
    • +
    • Overall, we visit: a1,a2,ak,b1,b2,bma_1, a_2, \dots a_k, b_1, b_2, \dots b_m
    • +
    • By I.E. a1a2aka_1 \leq a_2 \leq \dots \leq a_k
    • +
    • + b1,b2bmb_1, \leq b_2 \leq \dots \leq b_m +
    • +
    • Because T is a BST, so:
    • +
    + +

    akkey(v)<b1a1a2akkeys(v)b1b2bm +a_k \leq \text{key}(v) < b_1\\ +\therefore a_1 \leq a_2 \leq \dots \leq a_k \leq \text{keys}(v) \leq b_1 \leq b_2 \dots \leq b_m +

    + +

    BST Find/Search: examples

    + +

    (Trasncriber’s note: the links are the search path for the algorithms)

    + +

    find(5):

    + + +
      +
    • + + 3 + +
        +
      • + + 2 + +
      • +
      • + + 8 + +
          +
        • + + 5 (check) + +
            +
          • + + 4 + +
          • +
          • + + 6 + +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      +
    • +
    + + +

    find(1):

    + + +
      +
    • + + 3 + +
        +
      • + + 2 + +
      • +
      • + + 8 + +
          +
        • + + 5 + +
            +
          • + + 4 + +
          • +
          • + + 6 (X) + +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      +
    • +
    + + +

    Find 6:

    + + +
      +
    • + + 5 + +
        +
      • + + 8 (right, X) + +
          +
        • + + 10 (right) + +
        • +
        +
      • +
      +
    • +
    + + +

    Some notation:

    + +

    Suppose v is a node of BST. We write:

    + +
      +
    • left(v) = left child of v
    • +
    • right(v) = right child of v
    • +
    • key(v) = key labelling v
    • +
    • node(v) node v s.t. key(v)=x
    • +
    + +

    BSD find(x) Pseudo-code

    + +
    find(x){// return true iff t is in the tree.
    +  return find(t,root)
    +}
    +
    +find(t,v)// return true if t appears in ubstree rooted at v.
    +{
    +  if t < key(v) & v has a left subtree
    +    return find(t, left(v))
    +  if t > key(v) & v has a right subtree
    +    return find(t, right(v))
    +  if key(v) = t
    +    return true
    +  return false //v is a leaf, does not have t
    +}
    +
    + +

    BST find(t,v) pseudo-code – alternate version

    + +
    find(t,v) // return true if t appears in subtree rooted at v
    +{
    +  if key(v)=t
    +    return true
    +  if t < key(v) & v has a left subtree
    +    return find(t,left(v))
    +  if t > key(v) & v has a right subtree
    +    return find(t,right(v))
    +  return false
    +}
    +
    + +

    Q: Which version is better?

    + +

    A: key(v)=t will almsot always be false, so the first return should do fewer comparisons and usually be false.

    + +

    BST insert(x) Pseudo-code

    + +
    insert(t){
    +  // adds t to the tree
    +  // assumes t is not in the tree already*
    +  u <- node at which find(t,root) terminates**
    +  if t<key(u)
    +    give u a new left child with key t.
    +  else
    +    give u a new right child with key t.
    +}
    +
    + +

    * Excersise: Write the version that does not make this assumption.

    + +

    ** Excersise: Write the version where the search is excplicit.

    + +

    BST Insert Examples

    + +

    insert(1):

    + + +
      +
    • + + 3 + +
        +
      • + + 2 + +
          +
        • + + 1 (inserted) + +
        • +
        +
      • +
      • + + 8 + +
          +
        • + + 5 + +
            +
          • + + 4 + +
          • +
          • + + 6 + +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      +
    • +
    + + +

    insert(7):

    + + +
      +
    • + + 3 + +
        +
      • + + 2 + +
      • +
      • + + 8 + +
          +
        • + + 5 + +
            +
          • + + 4 + +
          • +
          • + + 6 + +
              +
            • + + 7 (right) + +
            • +
            +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      +
    • +
    + + +

    BST insert(x) Pseudo-code – explicit search version…

    + +
    insert(t){ //adds t to the tree if it is not already there
    +  insert(t, root)
    +}
    +insert(t,v) //insert t in the subtree rooted at v, if it is not there
    +{
    +  if t < key(v) & v has a left subtree
    +    insert(t, left(v))
    +  if t > key(v) & v has a right subtree
    +    insert(t, right(v))
    +  if t < key(v) //here v has no left child
    +    give v a new left child with key t
    +  if t > key(v) //here v has no right child
    +    give v a new right child with key t.
    +  // if we reach here, t=key(v), so do nothing.
    +}
    +
    + +

    Insertion Over for BSTs: Examples

    + +

    1)

    + +
      +
    • start with an empty BST
    • +
    • insert 5,2,3,7,8,1,6 in the given order
    • +
    + + +
      +
    • + + 5 + +
        +
      • + + 2 + +
          +
        • + + 1 + +
        • +
        • + + 3 + +
        • +
        +
      • +
      • + + 7 + +
          +
        • + + 6 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    2)

    + +
      +
    • start with an empty BST
    • +
    • insert 1,2,3,5,6,7,8 in the order given
    • +
    + + +
      +
    • + + 1 + +
        +
      • + + 2 (right) + +
          +
        • + + 3 (right) + +
            +
          • + + 4 (right) + +
              +
            • + + 5 (right) + +
                +
              • + + 6 (right) + +
                  +
                • + + 7 (right) + +
                    +
                  • + + 8 (right) + +
                  • +
                  +
                • +
                +
              • +
              +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Notes

    + +
      +
    • Insertion order affects the shape of a BST.
    • +
    • Removal order can too.
    • +
    + +

    BST remove(t)

    + +

    We consider 3 cases, increasing difficulty.

    + +
      +
    1. Case 1: t is at a leaf (example figure #1): +
        +
      1. find the node v with key(v)=t
      2. +
      3. delete v
      4. +
      +
    2. +
    3. Case 2: t is a node with 1 child (example figure #2 and example figure #3) +
        +
      1. find the node v with key(v)=t
      2. +
      3. let u be the child of v
      4. +
      5. replace v with the subtree rooted at u
      6. +
      +
    4. +
    5. For case 3, see the next section
    6. +
    + +

    Example Figure #1

    + +

    remove(7):

    + + +
      +
    • + + 5 + +
        +
      • + + 3 + +
          +
        • + + 2 (left) + +
        • +
        +
      • +
      • + + 8 + +
          +
        • + + 7 (Xed out) + +
        • +
        • + + 9 + +
        • +
        +
      • +
      +
    • +
    + + +

    Example Figure #2

    + +

    remove(3)

    + +

    step 1 (original)

    + + +
      +
    • + + 5 + +
        +
      • + + 3 (Xed out) + +
          +
        • + + 1 (left) + +
        • +
        +
      • +
      • + + 10 + +
      • +
      +
    • +
    + + +

    step 2

    + + +
      +
    • + + 5 + +
        +
      • + + 10 (right) + +
      • +
      +
    • +
    + + + +
      +
    • + + 1 + +
    • +
    + + +

    step 3

    + + +
      +
    • + + 5 + +
        +
      • + + 1 + +
      • +
      • + + 10 + +
      • +
      +
    • +
    + + +

    Example Figure #4

    + +

    remove(10)

    + +

    step 1 (original)

    + + +
      +
    • + + 4 + +
        +
      • + + 2 + +
      • +
      • + + 10 (Xed out) + +
          +
        • + + 7 (left) + +
            +
          • + + 6 + +
              +
            • + + 5 (left) + +
            • +
            +
          • +
          • + + 8 + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    step 2

    + + +
      +
    • + + 4 + +
        +
      • + + 2 (left) + +
      • +
      +
    • +
    + + +
      +
    • + + 7 + +
        +
      • + + 6 + +
          +
        • + + 5 (left + +
        • +
        +
      • +
      • + + 8 + +
      • +
      +
    • +
    + + +

    step 3

    + + +
      +
    • + + 4 + +
        +
      • + + 2 + +
      • +
      • + + 7 + +
          +
        • + + 6 + +
            +
          • + + 5 (left + +
          • +
          +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    BST remove: Case 3 Preperation: Successors

    + +
      +
    • In an ordered collection X=si1,si,si+1,si+2X=\langle \cdots s_{i-1}, s_{i}, s_{i+1}, s_{i+2} \cdots \rangle +
        +
      • si1s_{i-1} is the predocessor of sis_{i}
      • +
      • si+1s_{i+1} is the successor if sis_{i}
      • +
      • Write: succx(si)=si+1\text{succ}_{x}(s_{i}) = s_{i+1}
      • +
      +
    • +
    • Let V=v1,vnV=\langle v_{1},\cdots v_{n}\rangle be the nodes of the tree ordered as per an in-order traversal.
    • +
    • Let K=k1,,knK=\langle k_{1},\cdots ,k_{n}\rangle$$ be the keys, in non-decreasing order.
    • +
    • Then: y=key(u)    succk(y)=key(succv(u))y=\text{key}(u) \implies \text{succ}_{k}(y) = \text{key}(\text{succ}_{v}(u)) i.e., the next node has the next key.
    • +
    + +

    BST remove: Case 3 Preperation: Successorts in BSTs

    + +
      +
    • If S is a set of keys, and xSx\in S, then the successor of x in S is the smallest value yS s.t. x<yy\in S \text{ s.t. } x< y. +Ex. S={19,27,8,3,12},succ(8)=12,succ(12)=19,S=\{ 19, 27, 8, 3, 12 \}, \text{succ}(8)=12, \text{succ}(12)=19, \cdots (S={3,8,12,19,27})(S=\{3,8,12,19,27\})
    • +
    • In a BST, in-order traversal visits keys in order. +
        +
      • Let S be the set of keys in BST T.
      • +
      • the successor of x in S is key(u)\text{key}(u) where u is the node of T that an in-order traversal of T visits next after v.
      • +
      +
    • +
    + + +
      +
    • + + 5 + +
        +
      • + + 3 + +
          +
        • + + 2 (left) + +
        • +
        +
      • +
      • + + 8 + +
          +
        • + + 7 + +
        • +
        • + + 9 + +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • If v is a node of BST T, then we can say the successor of v in T is the node of T visited just after v by an in-order traversal of T. Then: succ(x)=key(succ(node(x)))\text{succ}(x)=\text{key}(\text{succ}(\text{node}(x)))
    • +
    • Or: if key(v)=x\text{key}(v)=x, we can find the successor of x by finding the successor node of v, and getting its key: succ(key(v))=key(succ(v))\text{succ}(\text{key}(v)) = \text{key}(\text{succ}(v))
    • +
    + +

    BST remove: Case 3 Preperation: Successors

    + +

    If node v has a right child, it is easy to find its successor: succ(v)\text{succ}(v) is the first node visited by an in-order traversal of the right subtree of v.

    + +

    Ex. 6 diagrams. All of which give v a right subtree, one of one node, one of one node with a left child, one with a left leaf and right subtree of its own, and three variations on arbitrary numbers of children attached to the left node of v.

    + +

    To find the successor of node v that has a right child, use:

    + +
    succ(v){
    +  u<-right(v)
    +  while(left(u) exists){
    +    u<-left(u)
    +  }
    +  return u
    +}
    +
    + +

    BST remove(t)

    + +

    Case 3: t is at a node with 2 children:

    + +
      +
    1. find the node v with key(v)=t
    2. +
    3. find the successor of v – call it u.
    4. +
    5. key(v)<-key(u) //replace t with succ(t) at v.
    6. +
    7. delete u: +
        +
      1. if u is a leaf, delete it.
      2. +
      3. if u is not a leaf, it has one child w, replace u with the subtree rooted at w.
      4. +
      +
    8. +
    + +

    Notice: 4.1 is like case 1; 4.2 is like case 2.

    + +

    BST remove(k) when node(k) has two children

    + +

    Ex. to remove 5:

    + +
      +
    1. Find 5
    2. +
    3. Find successor of 5
    4. +
    5. Replace 5 with its succ.
    6. +
    7. In this example, succ(5) has no children so just delete the node where it was.
    8. +
    + +

    Example tree:

    + + +
      +
    • + + 20 (link starts step 1.) + +
        +
      • + + 15 + +
          +
        • + + 5 (left; Xed out; link starts step 2.) + +
            +
          • + + 2 + +
          • +
          • + + 10 + +
              +
            • + + 7 + +
                +
              • + + 6 (successor of 5) + +
              • +
              • + + 8 + +
              • +
              +
            • +
            • + + 12 + +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + 25 + +
          +
        • + + 22 + +
        • +
        • + + 26 + +
        • +
        +
      • +
      +
    • +
    + + +

    After switching 5 and succ(5):

    + +

    (transcriber’s note: may be incorrect, but I’m writing what’s there)

    + + +
      +
    • + + 20 + +
        +
      • + + 6 + +
          +
        • + + 2 + +
        • +
        • + + 10 + +
            +
          • + + 7 + +
              +
            • + + 8 (right) + +
            • +
            +
          • +
          • + + 12 + +
          • +
          +
        • +
        +
      • +
      • + + ... + +
      • +
      +
    • +
    + + +

    Example tree 2:

    + +

    To remove 6:

    + +
      +
    1. Find 6
    2. +
    3. Find successor of 6
    4. +
    5. Replace 6 with its successor
    6. +
    7. Replace succ(6) with its non-empty subtree
    8. +
    + +

    Tree:

    + + +
      +
    • + + 30 + +
        +
      • + + 2 + +
          +
        • + + 1 + +
        • +
        • + + 6 (crossed out; link starts step 2.) + +
            +
          • + + ... + +
          • +
          • + + 14 + +
              +
            • + > + 11 + +
                +
              • + + 7 (succ of 6) + +
                  +
                • + + 9 (right; subtree of succ(6)) + +
                    +
                  • + + 8 (subtree of succ(6)) + +
                  • +
                  • + + 10 (subtree of succ(6)) + +
                  • +
                  +
                • +
                +
              • +
              • + + 12 + +
              • +
              +
            • +
            • + + ... + +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + ... + +
      • +
      +
    • +
    + + +

    Becomes, by step 4:

    + + +
      +
    • + + 30 + +
        +
      • + + 2 + +
          +
        • + + 1 + +
        • +
        • + + 7 + +
            +
          • + + ... + +
          • +
          • + + 14 + +
              +
            • + + 11 + +
                +
              • + + 9 + +
                  +
                • + + 8 + +
                • +
                • + + 10 + +
                • +
                +
              • +
              • + + 12 + +
              • +
              +
            • +
            • + + ... + +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + ... + +
      • +
      +
    • +
    + + +

    Complexity of BST Operations

    + +
      +
    • Measure as a function of: height (h) or size/# of keys (n).
    • +
    • All operations essentially involve traversing a path from the root to a node v, where in the worst case v is a leaf of maximum depth.
    • +
    • So: +
        +
      • find: O(h), O(n)
      • +
      • insert: O(h), O(n)
      • +
      • remove: O(h), O(n)
      • +
      +
    • +
    • For “short bushy” trees (e.g. T1) h is small relative in n.
    • +
    • For “tall skinny” trees (e.g. T2) h is proportional to n.
    • +
    + +

    Q: Can we always have short bushy BSTs?

    + +

    T1 h=? h = ?

    + + +
      +
    • + + node + +
        +
      • + + node + +
          +
        • + + node + +
        • +
        • + + node + +
        • +
        +
      • +
      • + + node + +
          +
        • + + node + +
        • +
        • + + node + +
        • +
        +
      • +
      +
    • +
    + + +

    T2 hnh \cong n

    + + +
      +
    • + + node + +
        +
      • + + node + +
          +
        • + + node + +
            +
          • + + ... + +
              +
            • + + node + +
                +
              • + + node + +
              • +
              +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Perfect Binary Tree

    + +
      +
    • A perfect binary tree of height h is a binary tree of height h with the max number of nodes:
    • +
    + +

    1 (yes):

    + + +
      +
    • + + node + +
    • +
    + + +

    2 (no):

    + + +
      +
    • + + node + +
        +
      • + + node (left) + +
      • +
      +
    • +
    + + +

    3 (yes):

    + + +
      +
    • + + node + +
        +
      • + + node + +
      • +
      • + + node + +
      • +
      +
    • +
    + + +

    4 (yes):

    + + +
      +
    • + + node + +
        +
      • + + node + +
          +
        • + + node + +
        • +
        • + + node + +
        • +
        +
      • +
      • + + node + +
          +
        • + + node + +
        • +
        • + + node + +
        • +
        +
      • +
      +
    • +
    + + +

    5 (no):

    + + +
      +
    • + + node + +
        +
      • + + node + +
          +
        • + + node + +
            +
          • + + node + +
          • +
          • + + node + +
          • +
          +
        • +
        • + + node + +
            +
          • + + node + +
          • +
          • + + node + +
          • +
          +
        • +
        +
      • +
      • + + node + +
          +
        • + + node + +
            +
          • + + node (right) + +
          • +
          +
        • +
        • + + node + +
            +
          • + + node + +
          • +
          • + + node + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    6 (no):

    + + +
      +
    • + + node + +
        +
      • + + node (right) + +
          +
        • + + node (right) + +
            +
          • + + node (right) + +
              +
            • + + node (right) + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • Claim: Every perfect binary tree of height h has 2htl12^{\text{htl}}-1 nodes.
    • +
    • Pg: By induction on h, or on the structure of the tree.
    • +
    • Basis: If h=0, there is one node (the root). We have 2htl1=211=12^{\text{htl}}-1 = 2^{1}-1=1 as required.
    • +
    • I.H.: Let kok \geq o, and assume that every perfect binary tree of height k has 2kh12^{kh}-1 nodes.
    • +
    • T.S.: (Need to show a plot of height k+1 has 2(k+1)+112^{(k+1)+1}-1 nodes). A perfect binary tree of height k+1 is constructed as: k is height of left (A) or right (B) subtree; k+1 is the height of the subtree plus one (the root). Where A,B are perfect binary trees of height k. +By I.H. they have 2k+112^{k+1}-1 nodes. +So, the tree has 2k+11+2k+11+1=2×2k+11=2(k+1)+112^{k+1}-1 + 2^{k+1}-1 + 1 = 2\times 2^{k+1} -1 = 2^{(k+1)+1}-1, as required.
    • +
    + +

    Existance of Optimal BSTs

    + +

    Claim: For every set S of n keys, there exists a BST for S with height at most 1+log2n1+\log_{2} n

    + +

    Proof: Let h be the smallest integer s.t. 2hn2^{h} \geq n, and let m=2hm=2^{h}. +So,

    + +

    2hn>2h1log22hlog2n>log22h1hlog2n>h1h<1+log2n +2^{h} \geq n > 2^{h-1}\\ +\log_{2} 2^{h} \geq \log_{2} n > \log_{2} 2^{h-1}\\ +h \geq \log_{2} n > h-1\\ +h < 1+\log_{2} n +

    + +

    let T be the perfect binary tree of height h

    + +

    Label the first n nodes of T (as visited by an in-order traversal) with the +keys of S, and delete the remaining +ndoes (to get T1T^{1}).

    + +

    T1T^{1} is a BST for S with height h<1+log2nh< 1+\log_{2} n

    + +

    So, there is always a BST with height O(logn)O(\log n).

    + +

    Optimal BST Insertion Order

    + +

    Given a set of keys, we can insert them so as to get a minimum height BST:

    + +

    Consider:

    + +

    Graph of a perfect tree, with height of 4. Every node has two children, except for the 8 leafs.

    + +

    What can we say about the key at the root? It is the median key.

    + +

    Observe: the first key inserted into a BST is at the root forever (unless we remove it from the BST).

    + +

    Given a set of keys, we can insert them to get a minimum height BST:

    + +

    (transcriber’s note: I may have done this wrong, the drawing of the following tree is very weird.)

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
      • +
      • + + 2 + +
      • +
      +
    • +
    + + +

    * apply the “root is the median key” principle to each subtree.

    + +

    So, there is always a BST with height logn\cong\log n

    + +

    Can we maintain min. height with O(logn)O(\log n) as we insert and remove keys?

    + +

    Consider A:

    + + +
      +
    • + + 5 + +
        +
      • + + 3 + +
          +
        • + + 2 + +
        • +
        • + + 4 + +
        • +
        +
      • +
      • + + 7 + +
          +
        • + + 6 (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    insert(1) would make it become B:

    + + +
      +
    • + + 4 + +
        +
      • + + 2 + +
          +
        • + + 1 + +
        • +
        • + + 3 + +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 5 + +
        • +
        • + + 7 + +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • B is the only min height BST for 1..7.
    • +
    • A -> B requires “moving every node”
    • +
    • To get O(logn)O(\log n) operations, we need antoher kind of search tree, other than plain BSTs.
    • +
    • To get efficient search trees, give up at least one of: +
        +
      • binary
      • +
      • min height
      • +
      +
    • +
    • Next: self-balancing search trees.
    • +
    + +

    End (transciber’s note: not the end)

    + +

    (some repeated slides and graphics)

    + +

    Notice:

    + +

    Because a perfect binary tree of height h has:

    + +
      +
    • h height
    • +
    • 2h+112^{h+1}-1 nodes
    • +
    • 2h12^{h}-1 internal nodes (nodes with children)
    • +
    • 2h2^h leaves
    • +
    + +

    Then: 2h+2h1=2×2h1=2h+112^{h} + 2^{h}-1 = 2\times 2^{h}-1 = 2^{h+1}-1

    + +

    Actual end

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/11/225 L11 - BST.pdf b/_site/melody/cmpt-225/11/225 L11 - BST.pdf new file mode 100644 index 0000000..4015aa8 Binary files /dev/null and b/_site/melody/cmpt-225/11/225 L11 - BST.pdf differ diff --git a/_site/melody/cmpt-225/12/12.html b/_site/melody/cmpt-225/12/12.html new file mode 100644 index 0000000..e0240bc --- /dev/null +++ b/_site/melody/cmpt-225/12/12.html @@ -0,0 +1,1791 @@ + + + + + | tait.tech + + + + + +
    +
    +

    AVL Trees – CMPT 225

    + +

    Recall: A BST is:

    + +
      +
    • a binary tree
    • +
    • with nodes labled by keys
    • +
    • for every two nodes u,v: +
        +
      • if u is in the left subtree of v, then key(u) < key(v)
      • +
      • if u is in the right subtree of v, then key(u) > key(v)
      • +
      +
    • +
    • +

      BST operations take time proportional to the tree height, which might be the same as the number of keys.

      +
    • +
    • AVL Trees are a kind of “self-balancing” BST. +Tree height is always at most 2log2n2 \log_{2} n +where n is number of keys.
    • +
    • An AVL Tree is a BST that satisfies the following height-balance invariant: +
        +
      • + + + + + + + + +
        for every node v: $$\text{height}(\text{left}(v)) - \text{height}(\text{right(v))\leq 1$$ #TODO FIX
        +
      • +
      • (We define height(left(v)) = -1 if left(v) does not exist, similarly for right.
      • +
      +
    • +
    • Implementing the Operations: +
        +
      1. Perform BST operations, then
      2. +
      3. repair balance if needed.
      4. +
      +
    • +
    + +

    How unbalanced can an AVL Tree be?

    + +

    Ex. A “Maximally unbalanced” height - 5 AVL Tree

    + + +
      +
    • + + root node + +
        +
      • + + left subtree (7 nodes) + +
          +
        • + + node + +
            +
          • + + node + +
              +
            • + + node (left) + +
            • +
            +
          • +
          • + + node + +
          • +
          +
        • +
        • + + node + +
            +
          • + + node (left) + +
          • +
          +
        • +
        +
      • +
      • + + right subtree (31 nodes) + +
          +
        • + + node + +
            +
          • + + node + +
              +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            +
          • +
          • + + node + +
              +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            +
          • +
          +
        • +
        • + + node + +
            +
          • + + node + +
              +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            +
          • +
          • + + node + +
              +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    How tall can an AVL Tree be?

    + +

    Let N(h) = min # of nodes in an AVL tree of height h.

    + +

    Observe:

    + +
      +
    • + N(0)=1N(0) = 1 +
    • +
    • + N(1)=2N(1) = 2 +
    • +
    • + N(h)=N(h1)+N(h2)+1N(h) = N(h-1) + N(h-2) + 1 +
    • +
    • + N(h)>2N(h2)N(h) > 2 N(h-2) +
    • +
    • + N(h)>2×2N(h4)N(h) > 2 \times 2 N(h-4) +
    • +
    • + N(h)>23N(h6)N(h) > 2^{3} N(h-6) +
    • +
    • +
    • N(h)>2iN(h2i)N(h) > 2^{i} N(h-2i) (if H is even, we end when h2i=0    i=h/2h-2i=0 \implies i = h/2)
    • +
    • + N(h)>2y×c=2h/2×1N(h) > 2^{y} \times c = 2^{h/2}\times 1 +
    • +
    + +

    Claim: N(h)>2h/2N(h) > 2^{h/2}

    + +

    Proposition: N(h)>2h/2N(h) > 2^{h/2}

    + +
      +
    • Pf.: +
        +
      • By ind. on h.
      • +
      • Basis: N(0)=120=1N(0)=1 \leq 2^{0} = 1 (checkmark); N(1)=2>21/2=2N(1)=2 > 2^{1/2} = \sqrt{2} (checkmark).
      • +
      • Assume, for some h1h\geq 1, that N(h)>2h/2N(h)>2^{h/2}
      • +
      • Now N(h+1)>2N(h1)2×2h12=21+h12=2h1+22=2h+12N(h+1) > 2N(h-1) \geq 2\times 2^{\frac{h-1}{2}} = 2^{1+\frac{h-1}{2}} = 2^{\frac{h-1+2}{2}} = 2^{\frac{h+1}{2}} (checkmark)
      • +
      • So: N(h)>2h/2    log2N(h)>h/2    h<2log2N(h)2log2nN(h)>2^{h/2} \implies \log_{2} N(h) > h/2 \implies h < 2 \log_{2} N(h) \leq 2 \log_{2} n
      • +
      • We have: for every AVL tree with n nodes and height h, h<2log2n=O(logn)h < 2 \log_{2} n = O(\log n)
      • +
      +
    • +
    • Thus: AVL Tree search takes time that is O(logn)O(\log n)
    • +
    + +

    Max AVL Tree Height vs BST height

    + +

    (Worst case # of nodes visited by AVL-tree vs BST search)

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nn2log2n2 \log_{2} n
    101077
    1001001414
    100010002020
    10410^{4}2727
    10510^{5}3333
    10610^{6}4040
    10710^{7}4747
    10810^{8}5353
    10910^{9}6060
    101010^{10}6666
    + +

    Unbalanced subtrees are “repaired” using rotations

    + + +
      +
    • + + 5 + +
        +
      • + + 3 + +
          +
        • + + 1 + +
        • +
        • + + 4 + +
        • +
        +
      • +
      • + + 7 + +
          +
        • + + 6 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    Can be converted to the following using a right rotation at node with 5. +Can be converted from the following using a left rotation at the node with 3.

    + + +
      +
    • + + 3 + +
        +
      • + + 1 + +
      • +
      • + + 5 + +
          +
        • + + 4 + +
        • +
        • + + 7 + +
            +
          • + + 6 + +
          • +
          • + + 8 + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    AVL Tree insertion:

    + +
      +
    1. Do BST insertion.
    2. +
    3. If there is an unbalanced node, +
        +
      • let v be the unbalanced node of greatest depth*
      • +
      • repair the imbalance at v.
      • +
      +
    4. +
    + +

    Consider 4 cases (w is new node, v is unbalanced, k is height of tree, h is height of any given subtree):

    + +

    2 outside cases:

    + +

    Case 1:

    + + +
      +
    • + + v (root; h=k) + +
        +
      • + + left (h=k-1) + +
          +
        • + + left left (h=k-2) + +
            +
          • + + ... + +
              +
            • + + w + +
            • +
            +
          • +
          +
        • +
        • + + left right (h=k-2) + +
            +
          • + + ... + +
          • +
          +
        • +
        +
      • +
      • + + right (h=k-2) + +
          +
        • + + ... + +
        • +
        +
      • +
      +
    • +
    + + +

    Case 2:

    + + +
      +
    • + + v (root) + +
        +
      • + + left + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + right + +
          +
        • + + right left + +
            +
          • + + ... + +
          • +
          +
        • +
        • + + right right + +
            +
          • + + ... + +
              +
            • + + w + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Case 3:

    + + +
      +
    • + + v (root) + +
        +
      • + + left + +
          +
        • + + left left + +
            +
          • + + ... + +
          • +
          +
        • +
        • + + left right + +
            +
          • + + ... + +
              +
            • + + w* + +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + right + +
          +
        • + + ... + +
        • +
        +
      • +
      +
    • +
    + + +

    Case 4:

    + + +
      +
    • + + v (root) + +
        +
      • + + left + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + right + +
          +
        • + + right left + +
            +
          • + + ... + +
              +
            • + + w* + +
            • +
            +
          • +
          +
        • +
        • + + right right + +
            +
          • + + ... + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    * It msut be on the path from the new leaf to the root.

    + +

    To fix the “ouside” cases:

    + +

    Do 1 rotation at the unbalanced node.

    + + +
      +
    • + + v (root; h=k) + +
        +
      • + + u (h=k-1) + +
          +
        • + + left left (h=k-1) + +
            +
          • + + T1 (h=h-2) + +
              +
            • + + w + +
            • +
            +
          • +
          +
        • +
        • + + T2 (h=k-2) + +
        • +
        +
      • +
      • + + T3 (h=k-2) + +
      • +
      +
    • +
    + + +

    After rotation:

    + + +
      +
    • + + u (root; h=k) + +
        +
      • + + T1 (left; h=k-2) + +
          +
        • + + w + +
        • +
        +
      • +
      • + + v (right; h=k-1) + +
          +
        • + + T2 (right left; h=k-2) + +
        • +
        • + + T3 (right right; h=k-2) + +
        • +
        +
      • +
      +
    • +
    + + +

    * The final height of u is k, so the tree is now balanced.

    + +

    Excersizes:

    + +
      +
    1. Draw the right-right case in detail.
    2. +
    3. Draw them with minimal sized T1,T2,T3
    4. +
    + +

    The “inside cases” are not fixed by this rotation:

    + + +
      +
    • + + v (root) + +
        +
      • + + node (left; h=k-1) + +
          +
        • + + T1 (left left; h=k-2) + +
        • +
        • + + T2 (left right; h=k-2) + +
            +
          • + + w + +
          • +
          +
        • +
        +
      • +
      • + + T3 (right; h=k-2) + +
      • +
      +
    • +
    + + +

    Would become, with a right rotation:

    + + +
      +
    • + + v (root) + +
        +
      • + + T1 (left; h=k-2) + +
      • +
      • + + node (right; h=k) + +
          +
        • + + T2 (right left; h=k-2) + +
            +
          • + + w + +
          • +
          +
        • +
        • + + T3 (right right) + +
        • +
        +
      • +
      +
    • +
    + + +

    (Transcriber’s note: w is now used as a variable by the slides. w will no longer represent the asdded node. This will be written down in full as “new node”.)

    + +

    To fix the “inside” cases, we use two rotations:

    + + +
      +
    • + + v (h=k; v is the unbalanced node of any depth; label=a) + +
        +
      • + + u (h=k-1; label=b) + +
          +
        • + + T1 (h=k-2) + +
        • +
        • + + w (h=k-2; label=w) + +
            +
          • + + T2 (h=k-3) + +
              +
            • + + Insertion here or... one other place + +
            • +
            +
          • +
          • + + T3 (h=k-3) + +
              +
            • + + Other possible insertion place. + +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + T4 (h=k-2) + +
      • +
      +
    • +
    + + +

    Left rotation at b:

    + + +
      +
    • + + a + +
        +
      • + + c (h=k) + +
          +
        • + + b + +
            +
          • + + T1 + +
          • +
          • + + T2 + +
              +
            • + + possible insertion + +
            • +
            +
          • +
          +
        • +
        • + + T3 (h=k-3) + +
            +
          • + + possible insertion (not part of height) + +
          • +
          +
        • +
        +
      • +
      • + + T4 (h=k-2) + +
      • +
      +
    • +
    + + +

    After 1 rotation, this is too tall (like outisde case

    + +

    Right rotation at a:

    + + +
      +
    • + + c + +
        +
      • + + b (h=k-1) + +
          +
        • + + T1 (h=k-2) + +
        • +
        • + + T2 (h=k-2) + +
            +
          • + + possible insertion point + +
          • +
          +
        • +
        +
      • +
      • + + a (h=k-1) + +
          +
        • + + T3 (h=k-2) + +
            +
          • + + possible insertion + +
          • +
          +
        • +
        • + + T4 (h=k-2) + +
        • +
        +
      • +
      +
    • +
    + + +

    The entire operation is:

    + +
      +
    1. left(c) <- b
    2. +
    3. right(c) <- a
    4. +
    5. left(a) <- T3
    6. +
    7. right(b) <- T2
    8. +
    9. Change parent(a) to be parent(c)
    10. +
    + +

    (#1 rotation = 3 assignments; 2 rotations = 6 assignments; double rot = 5 assignments.)

    + +

    AVL Tree Removal

    + +
      +
    1. Do BST Removal.
    2. +
    3. Rebalance.
    4. +
    + +

    Define “the parent of the unbalanced node” (*) by cases:

    + +
      +
    1. The deleted key was at a leaf.
    2. +
    3. The deleted key was at a node with one child.
    4. +
    5. The deleted key is at a node with 2 children.
    6. +
    + +

    Case 1 graphs (X is removed node; p is parent of removed node):

    + + +
      +
    • + + p + +
        +
      • + + X (right) + +
      • +
      +
    • +
    + + +

    Becomes:

    + + +
      +
    • + + p + +
    • +
    + + +

    Graphs for case 2:

    + + +
      +
    • + + p + +
        +
      • + + X (right) + +
          +
        • + + ... (right) + +
        • +
        +
      • +
      +
    • +
    + + +

    Becomes:

    + + +
      +
    • + + p + +
        +
      • + + ... (right) + +
      • +
      +
    • +
    + + +

    Graphs for case 3:

    + + +
      +
    • + + X + +
        +
      • + + ... + +
      • +
      • + + node + +
          +
        • + + p (left) + +
            +
          • + + node (arrow to X) + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Or:

    + + +
      +
    • + + X1 + +
        +
      • + + ... + +
      • +
      • + + node + +
          +
        • + + p (left) + +
            +
          • + + X2 (arrow to X1) + +
              +
            • + + ... (right) + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
    + +

    Fact: After doing a BST removal in an AVL tree, +there is at most 1 unbalanced node, and it is on the path from the parent of the deleted node to the root.

    + +

    (If the deleted node was the root, the “parent of the deleted node” does not exist–but also there can be no unbalanced node)

    + +

    Consider:

    + + +
      +
    • + + root + +
        +
      • + + ... + +
          +
        • + + ... + +
            +
          • + + continues on with no detail + +
          • +
          • + + o (orange) + +
              +
            • + + continues on with no detail + +
            • +
            • + + node + +
                +
              • + + b (blue; left) + +
                  +
                • + + m (mauve) + +
                    +
                  • + + continues on with no detail (right) + +
                  • +
                  +
                • +
                • + + continues oN with no detail + +
                • +
                +
              • +
              +
            • +
            +
          • +
          +
        • +
        • + + continues on with no detail + +
        • +
        +
      • +
      • + + continues on with no detail + +
      • +
      +
    • +
    + + +

    Becomes:

    + + +
      +
    • + + root + +
        +
      • + + ... (g; green) + +
          +
        • + + ... (r; red) + +
            +
          • + + continues on with no detail + +
          • +
          • + + o (orange) + +
              +
            • + + continues on with no detail + +
            • +
            • + + node + +
                +
              • + + b (blue; left) + +
                  +
                • + + continues on with no detail + +
                • +
                • + + continues oN with no detail + +
                • +
                +
              • +
              +
            • +
            +
          • +
          +
        • +
        • + + continues on with no detail + +
        • +
        +
      • +
      • + + continues on with no detail + +
      • +
      +
    • +
    + + +

    Terms:

    + +
      +
    • o, orange: Node with key to be deleted.
    • +
    • m, mauve: Deleted node
    • +
    • b, blue: Parent of deleted node
    • +
    • r, red: Unbalanced node
    • +
    • g, green: This node, for example, cannot be unbalanced.
    • +
    + +

    An AVL tree removal that illustrates:

    + +
      +
    1. Need to re-balance after removal
    2. +
    3. Re-balancing node u may reduce the height of a subtree, resulting in an ancestor of u being unbalanced.
    4. +
    + +

    remove(14):

    + + +
      +
    • + + 10 (h=3) + +
        +
      • + + 4 + +
          +
        • + + 2 + +
        • +
        • + + 7 + +
            +
          • + + 5 (left) + +
          • +
          +
        • +
        +
      • +
      • + + 12 + +
          +
        • + + 14 (removed; right) + +
        • +
        +
      • +
      +
    • +
    + + +

    left at 4:

    + + +
      +
    • + + 19 + +
        +
      • + + 7 + +
          +
        • + + 4 (left) + +
            +
          • + + 2 + +
          • +
          • + + 5 + +
          • +
          +
        • +
        +
      • +
      • + + 12 + +
      • +
      +
    • +
    + + +

    right at 10:

    + + +
      +
    • + + 7 + +
        +
      • + + 4 + +
          +
        • + + 2 + +
        • +
        • + + 5 + +
        • +
        +
      • +
      • + + 10 + +
          +
        • + + 12 (right) + +
        • +
        +
      • +
      +
    • +
    + + +

    Abstract version:

    + + +
      +
    • + + node + +
        +
      • + + node + +
          +
        • + + ... + +
        • +
        • + + ... + +
            +
          • + + 5 (left) + +
          • +
          +
        • +
        +
      • +
      • + + ... + +
          +
        • + + 14 (right; deleted) + +
        • +
        +
      • +
      +
    • +
    + + +

    Rebalance (for deletion):

    + +
      +
    • w <- parent of deleted node, if it exists
    • +
    • for (each node u=wrootu = w\cdots \text{root} on path from wrootw\cdots \text{root}){ +
        +
      • if u is unbalanced +
          +
        • Let T be the subtree rooted at u
        • +
        • rebalance T using suitable rotations*
        • +
        • if height of T did not get smaller, return
        • +
        +
      • +
      +
    • +
    • }
    • +
    + +

    * either a single or a double rotation, based on case diagrams similar to that used for insertion.

    + +

    Correctness of the algorithm involves two properties:

    + +
      +
    1. There is at most 1 unbalanced node after deletion.
    2. +
    3. Rebalancing w may make an ancestor of w unbalanced.
    4. +
    + +

    Complexity of AVL tree operations

    + +
      +
    • Every AVL tree with n nodes has height O(logn)O(\log n).
    • +
    • The worst case amount of work for main opperations is: +
        +
      • search: O(logn)O(\log n) +
          +
        • one traversal from root to leaf: O(logn)O(\log n)
        • +
        +
      • +
      • insert: O(logn)O(\log n) +
          +
        • two traversals from root to leaf (down & back up): O(logn)O(\log n)
        • +
        • two rotations: O(1)O(1)
        • +
        +
      • +
      • remove: O(logn)O(\log n) +
          +
        • two traversals from root to leaf (down & back up): O(logn)O(\log n)
        • +
        • at most, two rotations at each node on that path: O(1)×O(logn)=O(logn)O(1) \times O(\log n) = O(\log n).
        • +
        +
      • +
      +
    • +
    + +

    All three major operations in O(logn)O(\log n) time.

    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/12/12/index.html b/_site/melody/cmpt-225/12/12/index.html new file mode 100644 index 0000000..93ff0ad --- /dev/null +++ b/_site/melody/cmpt-225/12/12/index.html @@ -0,0 +1,1793 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    AVL Trees – CMPT 225

    + +

    Recall: A BST is:

    + +
      +
    • a binary tree
    • +
    • with nodes labled by keys
    • +
    • for every two nodes u,v: +
        +
      • if u is in the left subtree of v, then key(u) < key(v)
      • +
      • if u is in the right subtree of v, then key(u) > key(v)
      • +
      +
    • +
    • +

      BST operations take time proportional to the tree height, which might be the same as the number of keys.

      +
    • +
    • AVL Trees are a kind of “self-balancing” BST. +Tree height is always at most 2log2n2 \log_{2} n +where n is number of keys.
    • +
    • An AVL Tree is a BST that satisfies the following height-balance invariant: +
        +
      • + + + + + + + + +
        for every node v: $$\text{height}(\text{left}(v)) - \text{height}(\text{right(v))\leq 1$$ #TODO FIX
        +
      • +
      • (We define height(left(v)) = -1 if left(v) does not exist, similarly for right.
      • +
      +
    • +
    • Implementing the Operations: +
        +
      1. Perform BST operations, then
      2. +
      3. repair balance if needed.
      4. +
      +
    • +
    + +

    How unbalanced can an AVL Tree be?

    + +

    Ex. A “Maximally unbalanced” height - 5 AVL Tree

    + + +
      +
    • + + root node + +
        +
      • + + left subtree (7 nodes) + +
          +
        • + + node + +
            +
          • + + node + +
              +
            • + + node (left) + +
            • +
            +
          • +
          • + + node + +
          • +
          +
        • +
        • + + node + +
            +
          • + + node (left) + +
          • +
          +
        • +
        +
      • +
      • + + right subtree (31 nodes) + +
          +
        • + + node + +
            +
          • + + node + +
              +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            +
          • +
          • + + node + +
              +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            +
          • +
          +
        • +
        • + + node + +
            +
          • + + node + +
              +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            +
          • +
          • + + node + +
              +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            • + + node + +
                +
              • + + node + +
              • +
              • + + node + +
              • +
              +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    How tall can an AVL Tree be?

    + +

    Let N(h) = min # of nodes in an AVL tree of height h.

    + +

    Observe:

    + +
      +
    • + N(0)=1N(0) = 1 +
    • +
    • + N(1)=2N(1) = 2 +
    • +
    • + N(h)=N(h1)+N(h2)+1N(h) = N(h-1) + N(h-2) + 1 +
    • +
    • + N(h)>2N(h2)N(h) > 2 N(h-2) +
    • +
    • + N(h)>2×2N(h4)N(h) > 2 \times 2 N(h-4) +
    • +
    • + N(h)>23N(h6)N(h) > 2^{3} N(h-6) +
    • +
    • +
    • N(h)>2iN(h2i)N(h) > 2^{i} N(h-2i) (if H is even, we end when h2i=0    i=h/2h-2i=0 \implies i = h/2)
    • +
    • + N(h)>2y×c=2h/2×1N(h) > 2^{y} \times c = 2^{h/2}\times 1 +
    • +
    + +

    Claim: N(h)>2h/2N(h) > 2^{h/2}

    + +

    Proposition: N(h)>2h/2N(h) > 2^{h/2}

    + +
      +
    • Pf.: +
        +
      • By ind. on h.
      • +
      • Basis: N(0)=120=1N(0)=1 \leq 2^{0} = 1 (checkmark); N(1)=2>21/2=2N(1)=2 > 2^{1/2} = \sqrt{2} (checkmark).
      • +
      • Assume, for some h1h\geq 1, that N(h)>2h/2N(h)>2^{h/2}
      • +
      • Now N(h+1)>2N(h1)2×2h12=21+h12=2h1+22=2h+12N(h+1) > 2N(h-1) \geq 2\times 2^{\frac{h-1}{2}} = 2^{1+\frac{h-1}{2}} = 2^{\frac{h-1+2}{2}} = 2^{\frac{h+1}{2}} (checkmark)
      • +
      • So: N(h)>2h/2    log2N(h)>h/2    h<2log2N(h)2log2nN(h)>2^{h/2} \implies \log_{2} N(h) > h/2 \implies h < 2 \log_{2} N(h) \leq 2 \log_{2} n
      • +
      • We have: for every AVL tree with n nodes and height h, h<2log2n=O(logn)h < 2 \log_{2} n = O(\log n)
      • +
      +
    • +
    • Thus: AVL Tree search takes time that is O(logn)O(\log n)
    • +
    + +

    Max AVL Tree Height vs BST height

    + +

    (Worst case # of nodes visited by AVL-tree vs BST search)

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    nn2log2n2 \log_{2} n
    101077
    1001001414
    100010002020
    10410^{4}2727
    10510^{5}3333
    10610^{6}4040
    10710^{7}4747
    10810^{8}5353
    10910^{9}6060
    101010^{10}6666
    + +

    Unbalanced subtrees are “repaired” using rotations

    + + +
      +
    • + + 5 + +
        +
      • + + 3 + +
          +
        • + + 1 + +
        • +
        • + + 4 + +
        • +
        +
      • +
      • + + 7 + +
          +
        • + + 6 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    Can be converted to the following using a right rotation at node with 5. +Can be converted from the following using a left rotation at the node with 3.

    + + +
      +
    • + + 3 + +
        +
      • + + 1 + +
      • +
      • + + 5 + +
          +
        • + + 4 + +
        • +
        • + + 7 + +
            +
          • + + 6 + +
          • +
          • + + 8 + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    AVL Tree insertion:

    + +
      +
    1. Do BST insertion.
    2. +
    3. If there is an unbalanced node, +
        +
      • let v be the unbalanced node of greatest depth*
      • +
      • repair the imbalance at v.
      • +
      +
    4. +
    + +

    Consider 4 cases (w is new node, v is unbalanced, k is height of tree, h is height of any given subtree):

    + +

    2 outside cases:

    + +

    Case 1:

    + + +
      +
    • + + v (root; h=k) + +
        +
      • + + left (h=k-1) + +
          +
        • + + left left (h=k-2) + +
            +
          • + + ... + +
              +
            • + + w + +
            • +
            +
          • +
          +
        • +
        • + + left right (h=k-2) + +
            +
          • + + ... + +
          • +
          +
        • +
        +
      • +
      • + + right (h=k-2) + +
          +
        • + + ... + +
        • +
        +
      • +
      +
    • +
    + + +

    Case 2:

    + + +
      +
    • + + v (root) + +
        +
      • + + left + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + right + +
          +
        • + + right left + +
            +
          • + + ... + +
          • +
          +
        • +
        • + + right right + +
            +
          • + + ... + +
              +
            • + + w + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Case 3:

    + + +
      +
    • + + v (root) + +
        +
      • + + left + +
          +
        • + + left left + +
            +
          • + + ... + +
          • +
          +
        • +
        • + + left right + +
            +
          • + + ... + +
              +
            • + + w* + +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + right + +
          +
        • + + ... + +
        • +
        +
      • +
      +
    • +
    + + +

    Case 4:

    + + +
      +
    • + + v (root) + +
        +
      • + + left + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + right + +
          +
        • + + right left + +
            +
          • + + ... + +
              +
            • + + w* + +
            • +
            +
          • +
          +
        • +
        • + + right right + +
            +
          • + + ... + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    * It msut be on the path from the new leaf to the root.

    + +

    To fix the “ouside” cases:

    + +

    Do 1 rotation at the unbalanced node.

    + + +
      +
    • + + v (root; h=k) + +
        +
      • + + u (h=k-1) + +
          +
        • + + left left (h=k-1) + +
            +
          • + + T1 (h=h-2) + +
              +
            • + + w + +
            • +
            +
          • +
          +
        • +
        • + + T2 (h=k-2) + +
        • +
        +
      • +
      • + + T3 (h=k-2) + +
      • +
      +
    • +
    + + +

    After rotation:

    + + +
      +
    • + + u (root; h=k) + +
        +
      • + + T1 (left; h=k-2) + +
          +
        • + + w + +
        • +
        +
      • +
      • + + v (right; h=k-1) + +
          +
        • + + T2 (right left; h=k-2) + +
        • +
        • + + T3 (right right; h=k-2) + +
        • +
        +
      • +
      +
    • +
    + + +

    * The final height of u is k, so the tree is now balanced.

    + +

    Excersizes:

    + +
      +
    1. Draw the right-right case in detail.
    2. +
    3. Draw them with minimal sized T1,T2,T3
    4. +
    + +

    The “inside cases” are not fixed by this rotation:

    + + +
      +
    • + + v (root) + +
        +
      • + + node (left; h=k-1) + +
          +
        • + + T1 (left left; h=k-2) + +
        • +
        • + + T2 (left right; h=k-2) + +
            +
          • + + w + +
          • +
          +
        • +
        +
      • +
      • + + T3 (right; h=k-2) + +
      • +
      +
    • +
    + + +

    Would become, with a right rotation:

    + + +
      +
    • + + v (root) + +
        +
      • + + T1 (left; h=k-2) + +
      • +
      • + + node (right; h=k) + +
          +
        • + + T2 (right left; h=k-2) + +
            +
          • + + w + +
          • +
          +
        • +
        • + + T3 (right right) + +
        • +
        +
      • +
      +
    • +
    + + +

    (Transcriber’s note: w is now used as a variable by the slides. w will no longer represent the asdded node. This will be written down in full as “new node”.)

    + +

    To fix the “inside” cases, we use two rotations:

    + + +
      +
    • + + v (h=k; v is the unbalanced node of any depth; label=a) + +
        +
      • + + u (h=k-1; label=b) + +
          +
        • + + T1 (h=k-2) + +
        • +
        • + + w (h=k-2; label=w) + +
            +
          • + + T2 (h=k-3) + +
              +
            • + + Insertion here or... one other place + +
            • +
            +
          • +
          • + + T3 (h=k-3) + +
              +
            • + + Other possible insertion place. + +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + T4 (h=k-2) + +
      • +
      +
    • +
    + + +

    Left rotation at b:

    + + +
      +
    • + + a + +
        +
      • + + c (h=k) + +
          +
        • + + b + +
            +
          • + + T1 + +
          • +
          • + + T2 + +
              +
            • + + possible insertion + +
            • +
            +
          • +
          +
        • +
        • + + T3 (h=k-3) + +
            +
          • + + possible insertion (not part of height) + +
          • +
          +
        • +
        +
      • +
      • + + T4 (h=k-2) + +
      • +
      +
    • +
    + + +

    After 1 rotation, this is too tall (like outisde case

    + +

    Right rotation at a:

    + + +
      +
    • + + c + +
        +
      • + + b (h=k-1) + +
          +
        • + + T1 (h=k-2) + +
        • +
        • + + T2 (h=k-2) + +
            +
          • + + possible insertion point + +
          • +
          +
        • +
        +
      • +
      • + + a (h=k-1) + +
          +
        • + + T3 (h=k-2) + +
            +
          • + + possible insertion + +
          • +
          +
        • +
        • + + T4 (h=k-2) + +
        • +
        +
      • +
      +
    • +
    + + +

    The entire operation is:

    + +
      +
    1. left(c) <- b
    2. +
    3. right(c) <- a
    4. +
    5. left(a) <- T3
    6. +
    7. right(b) <- T2
    8. +
    9. Change parent(a) to be parent(c)
    10. +
    + +

    (#1 rotation = 3 assignments; 2 rotations = 6 assignments; double rot = 5 assignments.)

    + +

    AVL Tree Removal

    + +
      +
    1. Do BST Removal.
    2. +
    3. Rebalance.
    4. +
    + +

    Define “the parent of the unbalanced node” (*) by cases:

    + +
      +
    1. The deleted key was at a leaf.
    2. +
    3. The deleted key was at a node with one child.
    4. +
    5. The deleted key is at a node with 2 children.
    6. +
    + +

    Case 1 graphs (X is removed node; p is parent of removed node):

    + + +
      +
    • + + p + +
        +
      • + + X (right) + +
      • +
      +
    • +
    + + +

    Becomes:

    + + +
      +
    • + + p + +
    • +
    + + +

    Graphs for case 2:

    + + +
      +
    • + + p + +
        +
      • + + X (right) + +
          +
        • + + ... (right) + +
        • +
        +
      • +
      +
    • +
    + + +

    Becomes:

    + + +
      +
    • + + p + +
        +
      • + + ... (right) + +
      • +
      +
    • +
    + + +

    Graphs for case 3:

    + + +
      +
    • + + X + +
        +
      • + + ... + +
      • +
      • + + node + +
          +
        • + + p (left) + +
            +
          • + + node (arrow to X) + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Or:

    + + +
      +
    • + + X1 + +
        +
      • + + ... + +
      • +
      • + + node + +
          +
        • + + p (left) + +
            +
          • + + X2 (arrow to X1) + +
              +
            • + + ... (right) + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
    + +

    Fact: After doing a BST removal in an AVL tree, +there is at most 1 unbalanced node, and it is on the path from the parent of the deleted node to the root.

    + +

    (If the deleted node was the root, the “parent of the deleted node” does not exist–but also there can be no unbalanced node)

    + +

    Consider:

    + + +
      +
    • + + root + +
        +
      • + + ... + +
          +
        • + + ... + +
            +
          • + + continues on with no detail + +
          • +
          • + + o (orange) + +
              +
            • + + continues on with no detail + +
            • +
            • + + node + +
                +
              • + + b (blue; left) + +
                  +
                • + + m (mauve) + +
                    +
                  • + + continues on with no detail (right) + +
                  • +
                  +
                • +
                • + + continues oN with no detail + +
                • +
                +
              • +
              +
            • +
            +
          • +
          +
        • +
        • + + continues on with no detail + +
        • +
        +
      • +
      • + + continues on with no detail + +
      • +
      +
    • +
    + + +

    Becomes:

    + + +
      +
    • + + root + +
        +
      • + + ... (g; green) + +
          +
        • + + ... (r; red) + +
            +
          • + + continues on with no detail + +
          • +
          • + + o (orange) + +
              +
            • + + continues on with no detail + +
            • +
            • + + node + +
                +
              • + + b (blue; left) + +
                  +
                • + + continues on with no detail + +
                • +
                • + + continues oN with no detail + +
                • +
                +
              • +
              +
            • +
            +
          • +
          +
        • +
        • + + continues on with no detail + +
        • +
        +
      • +
      • + + continues on with no detail + +
      • +
      +
    • +
    + + +

    Terms:

    + +
      +
    • o, orange: Node with key to be deleted.
    • +
    • m, mauve: Deleted node
    • +
    • b, blue: Parent of deleted node
    • +
    • r, red: Unbalanced node
    • +
    • g, green: This node, for example, cannot be unbalanced.
    • +
    + +

    An AVL tree removal that illustrates:

    + +
      +
    1. Need to re-balance after removal
    2. +
    3. Re-balancing node u may reduce the height of a subtree, resulting in an ancestor of u being unbalanced.
    4. +
    + +

    remove(14):

    + + +
      +
    • + + 10 (h=3) + +
        +
      • + + 4 + +
          +
        • + + 2 + +
        • +
        • + + 7 + +
            +
          • + + 5 (left) + +
          • +
          +
        • +
        +
      • +
      • + + 12 + +
          +
        • + + 14 (removed; right) + +
        • +
        +
      • +
      +
    • +
    + + +

    left at 4:

    + + +
      +
    • + + 19 + +
        +
      • + + 7 + +
          +
        • + + 4 (left) + +
            +
          • + + 2 + +
          • +
          • + + 5 + +
          • +
          +
        • +
        +
      • +
      • + + 12 + +
      • +
      +
    • +
    + + +

    right at 10:

    + + +
      +
    • + + 7 + +
        +
      • + + 4 + +
          +
        • + + 2 + +
        • +
        • + + 5 + +
        • +
        +
      • +
      • + + 10 + +
          +
        • + + 12 (right) + +
        • +
        +
      • +
      +
    • +
    + + +

    Abstract version:

    + + +
      +
    • + + node + +
        +
      • + + node + +
          +
        • + + ... + +
        • +
        • + + ... + +
            +
          • + + 5 (left) + +
          • +
          +
        • +
        +
      • +
      • + + ... + +
          +
        • + + 14 (right; deleted) + +
        • +
        +
      • +
      +
    • +
    + + +

    Rebalance (for deletion):

    + +
      +
    • w <- parent of deleted node, if it exists
    • +
    • for (each node u=wrootu = w\cdots \text{root} on path from wrootw\cdots \text{root}){ +
        +
      • if u is unbalanced +
          +
        • Let T be the subtree rooted at u
        • +
        • rebalance T using suitable rotations*
        • +
        • if height of T did not get smaller, return
        • +
        +
      • +
      +
    • +
    • }
    • +
    + +

    * either a single or a double rotation, based on case diagrams similar to that used for insertion.

    + +

    Correctness of the algorithm involves two properties:

    + +
      +
    1. There is at most 1 unbalanced node after deletion.
    2. +
    3. Rebalancing w may make an ancestor of w unbalanced.
    4. +
    + +

    Complexity of AVL tree operations

    + +
      +
    • Every AVL tree with n nodes has height O(logn)O(\log n).
    • +
    • The worst case amount of work for main opperations is: +
        +
      • search: O(logn)O(\log n) +
          +
        • one traversal from root to leaf: O(logn)O(\log n)
        • +
        +
      • +
      • insert: O(logn)O(\log n) +
          +
        • two traversals from root to leaf (down & back up): O(logn)O(\log n)
        • +
        • two rotations: O(1)O(1)
        • +
        +
      • +
      • remove: O(logn)O(\log n) +
          +
        • two traversals from root to leaf (down & back up): O(logn)O(\log n)
        • +
        • at most, two rotations at each node on that path: O(1)×O(logn)=O(logn)O(1) \times O(\log n) = O(\log n).
        • +
        +
      • +
      +
    • +
    + +

    All three major operations in O(logn)O(\log n) time.

    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/12/Lecture_12_AVL.pdf b/_site/melody/cmpt-225/12/Lecture_12_AVL.pdf new file mode 100644 index 0000000..e985975 Binary files /dev/null and b/_site/melody/cmpt-225/12/Lecture_12_AVL.pdf differ diff --git a/_site/melody/cmpt-225/13/13.html b/_site/melody/cmpt-225/13/13.html new file mode 100644 index 0000000..48edf3a --- /dev/null +++ b/_site/melody/cmpt-225/13/13.html @@ -0,0 +1,3531 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Priority Queue & Heaps

    + +

    PriorityQueue ADT (PQ)

    + +
      +
    • Stores a collection of pairs (item, priority)
    • +
    • Priorities are from some ordered set. For simplicity, we use priorities from 0,1,2,… with 0 “highest priority”.
    • +
    • Main operations: +
        +
      • insert(item, priority); adds item with priority `priority.
      • +
      • extract_m/n(); removes (& returns) item with least priority.
      • +
      • update(item, priority); changes priority of item to priority.
      • +
      +
    • +
    • We want a data structure to implement efficient PQs. (e.g. O(log n) time for all operations.
    • +
    • We (again) will use a particular kind of tree.
    • +
    + +

    Level - Order Traversal of ordered binary trees.

    + +
      +
    • visits each node of the tree once.
    • +
    • visits every node at depth i before any node at depth i+1*.
    • +
    • visits every depth-d descendants of left(v) before any depth-d descendant of right(v).
    • +
    + +

    Diagrams

    + +

    Order of traversal Diagram 1:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
          +
        • + + 4 + +
            +
          • + + 8 + +
          • +
          • + + 9 + +
          • +
          +
        • +
        • + + 5 + +
            +
          • + + 10 + +
          • +
          • + + 11 + +
          • +
          +
        • +
        +
      • +
      • + + 3 + +
          +
        • + + 6 + +
            +
          • + + 12 + +
          • +
          • + + 13 + +
          • +
          +
        • +
        • + + 7 + +
            +
          • + + 14 + +
          • +
          • + + 15 + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Order of traversal diagram 2:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
          +
        • + + 4 + +
            +
          • + + 8 + +
              +
            • + + 14 + +
            • +
            • + + 15 + +
            • +
            +
          • +
          • + + 9 + +
          • +
          +
        • +
        • + + 5 + +
            +
          • + + 10 (left) + +
              +
            • + + 16 (left) + +
                +
              • + + 20 + +
              • +
              • + + 21 + +
              • +
              +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + 3 + +
          +
        • + + 6 + +
            +
          • + + 11 + +
              +
            • + + 17 + +
            • +
            • + + 18 + +
                +
              • + + 22 (right) + +
              • +
              +
            • +
            +
          • +
          • + + 12 + +
          • +
          +
        • +
        • + + 7 + +
            +
          • + + 13 (right) + +
              +
            • + + 19 + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    * in some tests, it is bottom-up, not top-down.

    + +

    Complete Binary Tree

    + +

    A complete binary tree of height h is:

    + +
      +
    1. A binary tree of height h;
    2. +
    3. with 2d2^{d} nodes at depth d, for every 0d<h0 \leq d < h
    4. +
    5. level order traversal visits every internal node before any leaf
    6. +
    7. every internal node is proper*, except perhaps the last**, which may have just a left child.
    8. +
    + +

    Diagrams

    + +

    Example 1: X (4)

    + + +
      +
    • + + root + +
        +
      • + + child (right) + +
      • +
      +
    • +
    + + +

    Example 2: checkmark

    + + +
      +
    • + + root + +
        +
      • + + child + +
      • +
      • + + child + +
      • +
      +
    • +
    + + +

    Example 3: X (3)

    + + +
      +
    • + + root + +
        +
      • + + child + +
      • +
      • + + child + +
          +
        • + + grandchild (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 4: X (4)

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild (left) + +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 5: checkmark

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild (left) + +
        • +
        +
      • +
      • + + child + +
      • +
      +
    • +
    + + +

    Example 6: checkmark

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
        • +
        • + + grandchild + +
        • +
        +
      • +
      • + + child + +
      • +
      +
    • +
    + + +

    Example 7: checkmark

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
        • +
        • + + grandchild + +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 8: X (5)

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
        • +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        • + + grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 9: X (4)

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        • + + grandchild + +
            +
          • + + great granchild (left) + +
          • +
          +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        • + + grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 10: checkmark

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        • + + grandchild + +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
        • +
        • + + grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 11: checkmark

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Unlabled tree on next slide:

    + + +
      +
    • + + ... + +
        +
      • + + node at arbitrary depth + +
          +
        • + + child node + +
        • +
        • + + child node + +
        • +
        +
      • +
      • + + node at arbitrary depth + +
          +
        • + + child node + +
        • +
        • + + child node + +
        • +
        +
      • +
      • + + node at arbitrary depth + +
          +
        • + + child node + +
        • +
        • + + child node + +
        • +
        +
      • +
      • + + node at arbitrary depth + +
          +
        • + + child node (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    Binary Heap Data Structure

    + +
      +
    • a complete binary tree (“shape invariant”)
    • +
    • with verticies labled by keys (that is: priorities) from some ordered set,
    • +
    • s.t. key((v)key(parent(v))\text{key}((v) \geq \text{key}(\text{parent}(v)) for every node v. (“order invariant”)
    • +
    + +

    Example (checkmark:

    + + +
      +
    • + + 1 + +
        +
      • + + 3 + +
          +
        • + + 6 + +
            +
          • + + 7 + +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      • + + 2 + +
          +
        • + + 5 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    Example (X):

    + + +
      +
    • + + 1 + +
        +
      • + + 3 (highlighted arrow to 2) + +
          +
        • + + 2 + +
            +
          • + + 7 + +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      • + + 6 (highlighted connection to 5) + +
          +
        • + + 5 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    This is the basic DS for implementing PQs (binary min-heap).

    + +
    + +
      +
    • How do we implement the operators so that invariants are maintained?
    • +
    • Consider Insertion: If we want to insert 14 into the heap, where should it go?
    • +
    + + +
      +
    • + + root (complete tree) + +
        +
      • + + ... (anbiguous number of node/depth) + +
          +
        • + + 10 + +
            +
          • + + 12 + +
              +
            • + + ... + +
            • +
            +
          • +
          • + + 20 + +
              +
            • + + ... + +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + ... (anbiguous number of node/depth) + +
          +
        • + + 11 + +
            +
          • + + 13 + +
              +
            • + + ... + +
            • +
            +
          • +
          • + + 19 + +
              +
            • + + ... + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Notice: there no choice about how the shape changes:

    + +

    Example 1:

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + inserted node (left) + +
          • +
          +
        • +
        • + + grandchild + +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
        • +
        • + + grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 2:

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great grandchild + +
          • +
          • + + great grandchild + +
          • +
          +
        • +
        • + + grandchild + +
            +
          • + + great grandchild + +
          • +
          • + + inserted node + +
          • +
          +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
        • +
        • + + grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 3:

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great grandchild + +
          • +
          • + + great grandchild + +
          • +
          +
        • +
        • + + grandchild + +
            +
          • + + great grandchild + +
          • +
          • + + great grandchild + +
          • +
          +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + inserted node (left) + +
          • +
          +
        • +
        • + + grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Heap Insert

    + +

    To insert an item with key k:

    + +
      +
    1. add a new leaf v with key(v)=k, so as to maintain the shape invariant.
    2. +
    3. re-establish the order invariant by executing percolate_up(v).
    4. +
    + +

    Code:

    + +
    percolate_up(v){
    +  while(v is not root and key(v) < key(parent(v))) {
    +    swap positions of v and parent(v) in the tree
    +  }
    +}
    +
    + +

    Insert 2, then 4, then 3 into:

    + +

    Original:

    + + +
      +
    • + + 1 + +
        +
      • + + 5 + +
          +
        • + + 7 + +
            +
          • + + 12 + +
          • +
          • + + 4 + +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 10 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    Insert 2:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 (5 is crossed out) + +
          +
        • + + 7 + +
            +
          • + + 12 + +
          • +
          • + + 4 + +
          • +
          +
        • +
        • + + 5 (9,2 are crossed out, arrow pointing to parent) + +
            +
          • + + 9 (2 is coressed out, arrow pointing to parent, left) + +
          • +
          +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 10 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    insert 4:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
          +
        • + + 7 + +
            +
          • + + 12 + +
          • +
          • + + 4 + +
          • +
          +
        • +
        • + + 4 (5 is corssed out, arrow pointing to parent) + +
            +
          • + + 9 + +
          • +
          • + + 5 (4 is coressed out, arrow pointing to parent) + +
          • +
          +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 10 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    Insert 3:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
          +
        • + + 7 + +
            +
          • + + 12 + +
          • +
          • + + 4 + +
          • +
          +
        • +
        • + + 4 + +
            +
          • + + 9 + +
          • +
          • + + 5 + +
          • +
          +
        • +
        +
      • +
      • + + 3 (6 is crossed out, double sided arrow to/from parent) + +
          +
        • + + 6 (10, 3 are crossed out, double sided arrow to/from parent) + +
            +
          • + + 10 (3 is corssed out, double sised arrow to/from parent, left) + +
          • +
          +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    Becomes:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + 3 + +
          +
        • + + 6 + +
            +
          • + + 10 (left) + +
          • +
          +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    Heap Extract-Min:

    + +

    Consider (need result of dot dot dots):

    + + +
      +
    • + + 5 + +
        +
      • + + ... (left) + +
      • +
      • + + ... (right) + +
      • +
      +
    • +
    + + +

    We must replace the root with the smaller of its children:

    + +

    Diagram labled “OK”:

    + + +
      +
    • + + ? + +
        +
      • + + 6 (arrow towards root) + +
          +
        • + + 10 + +
        • +
        • + + 12 + +
        • +
        +
      • +
      • + + 7 + +
      • +
      +
    • +
    + + +

    Diagram labled “NOT OK”:

    + + +
      +
    • + + ? + +
        +
      • + + 7 + +
          +
        • + + 10 + +
        • +
        • + + 12 + +
        • +
        +
      • +
      • + + 6 (arrow towards root) + +
      • +
      +
    • +
    + + +

    Heap Extract-Min

    + +

    To remove the (item with the) smalled key form the heap:

    + +
      +
    1. rename the root
    2. +
    3. replace the root with the “last leaf”, so as to maintain the shape invariant.
    4. +
    5. restore the order invariant by calling percolate_down(root)
    6. +
    + +

    Percolate_down is more work than percolate_up, +because it must look at both children +to see what to do (and the children may or may not exist)

    + +

    Code:

    + +
    percolate_down(v){
    +  while(v has a child c with key(c) < key(v)){
    +    c <- child of v with the smallest key among the children of v.
    +    swap v and c in the tree
    +  }
    +}
    +
    + +

    Notice that:

    + +
      +
    • v may have 0, 1 or 2 children
    • +
    • if v has 2 children, we care about the one with the smallest key.
    • +
    + +

    Do extract-min 3 times

    + +

    Original:

    + + +
      +
    • + + 1 + +
        +
      • + + 6 + +
          +
        • + + 12 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      • + + 4 + +
          +
        • + + 11 + +
        • +
        • + + 7 + +
        • +
        +
      • +
      +
    • +
    + + +

    First extract-min:

    + + +
      +
    • + + 4 (7, 1 are crossed out) + +
        +
      • + + 6 + +
          +
        • + + 12 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      • + + 7 (4 crossed out) + +
          +
        • + + 11 + +
        • +
        • + + (7 crossed out) + +
        • +
        +
      • +
      +
    • +
    + + +

    Second extract-min:

    + + +
      +
    • + + 6 (4, 11 are crossed out) + +
        +
      • + + 8 (6, 11 are crossed out) + +
          +
        • + + 12 + +
        • +
        • + + 11 (8 is crossed out) + +
        • +
        +
      • +
      • + + 7 + +
          +
        • + + (11 crossed out, left) + +
        • +
        +
      • +
      +
    • +
    + + +

    Third extract-min:

    + + +
      +
    • + + 7 (6, 11 are crossed out) + +
        +
      • + + 8 + +
          +
        • + + 12 + +
        • +
        • + + (11 is crossed out) + +
        • +
        +
      • +
      • + + 11 (7 is crossed out) + +
      • +
      +
    • +
    + + +

    Final form:

    + + +
      +
    • + + 7 + +
        +
      • + + 8 + +
          +
        • + + 12 (left) + +
        • +
        +
      • +
      • + + 11 + +
      • +
      +
    • +
    + + +

    Complexity of Heap Insert & Extract-min

    + +
      +
    • Claim: Insert & Extract-min take time O(log n) for heaps of size n.
    • +
    • Recall: A perfect binary tree of height h has 2h+112^{h+1}-1 nodes.
    • +
    • P.f.: By induction on h (or “the structure of the tree”). +
        +
      • Basis: If h=0 then we have 2011=12^{0-1} -1 = 1 nodes. (checkmark)
      • +
      • I.H.: Consider some h0h\geq 0 and assume the perfect binary tree of height h has 2h+112^{h+1} -1 nodes.
      • +
      • I.S.: show the p.b.t. of height h+1 has 2(h+1)+112^{(h+1)+1}-1 nodes. +
          +
        • The tree is: diagram of tree with left/right being of height h, and left/right plus the parent is h+1.
        • +
        • So it has 2h+11+2h+11+1=2×2h+11=2(h+1)+112^{h+1} -1 + 2^{h+1} -1 +1 = 2 \times 2^{h+1}-1 = 2^{(h+1)+1}-1 nodes. (circle with line through it)
        • +
        +
      • +
      +
    • +
    + +

    Size bounds on complete binary trees

    + +
      +
    • Every complete binary tree with height h and n nodes satisfies: 2hn2h+112^{h} \leq n \leq 2^{h+1}-1 +
        +
      • Smallest: (diagram of p.b.t. with height h and one node attached in the farthest left); #nodes = 2(h+1)+11+1=2h2^{(h+1)+1}-1+1 = 2^h
      • +
      • Largest: (diagram of p.b.t. with height h fully filled)
      • +
      +
    • +
    • So, we have:
    • +
    + +

    2hnlog22hlog2nhlog2nh=O(logn) +\begin{aligned} +2^{h} & \leq n\\ +\log_{2} 2^{h} & \leq \log_{2} n\\ +h & \leq \log_{2} n\\ +h & = O(\log n) +\end{aligned} +

    + +

    Heap insert & extract min take time O(log n)

    + +

    Linked Implementation of Heap

    + + + + +
      +
    • + + 2 (left, right, parent is null) + +
        +
      • + + 4 (left, right, parent) + +
          +
        • + + 7 (left=null, right=null, parent) + +
        • +
        • + + 8 (left=null, right=null, parent) + +
        • +
        +
      • +
      • + + 3 (left, right=null, parent) + +
          +
        • + + 5 (left=nill, right=null, parent) + +
        • +
        +
      • +
      +
    • +
    + + +

    Node:

    + +
      +
    • data
    • +
    • left
    • +
    • right
    • +
    • parent
    • +
    + +

    Array-Based Binary Heap Implementation

    + +

    Uses this embedding of a complete binary tree of size n in a size-n array:

    + +

    Tree version:

    + + +
      +
    • + + 0 + +
        +
      • + + 1 + +
          +
        • + + 3 + +
            +
          • + + 7 + +
          • +
          • + + 8 + +
          • +
          +
        • +
        • + + 4 + +
            +
          • + + 9 + +
          • +
          • + + 10 + +
          • +
          +
        • +
        +
      • +
      • + + 2 + +
          +
        • + + 5 + +
            +
          • + + 11 + +
          • +
          • + + 12 (inserted) + +
          • +
          +
        • +
        • + + 6 + +
        • +
        +
      • +
      +
    • +
    + + +

    Becomes, array version:

    + +
      +
    • 0
    • +
    • 1
    • +
    • 2
    • +
    • 3
    • +
    • 4
    • +
    • 5
    • +
    • 7
    • +
    • 8
    • +
    • 9
    • +
    • 10
    • +
    • 11
    • +
    • (inserted) 12
    • +
    + +

    ith ndoe in level-order traversal becomes ith array element.

    + + + +

    * growing and shrinking the tree is easy in the array embedding.

    + +

    Partially-filled Array Implementation of Binary Heap: Insert

    + +

    Original:

    + + +
      +
    • + + 2 + +
        +
      • + + 7 + +
          +
        • + + 8 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 9 (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    equals:

    + +
      +
    1. 2 left, right
    2. +
    3. 7 left, right
    4. +
    5. 6
    6. +
    7. 8
    8. +
    9. 10
    10. +
    11. 4
    12. +
    + +

    Insert 1:

    + + +
      +
    • + + 2 + +
        +
      • + + 7 + +
          +
        • + + 8 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 9 + +
        • +
        • + + (inserted) 1 + +
        • +
        +
      • +
      +
    • +
    + + +

    array implementation:

    + +
      +
    1. 2 (left, right)
    2. +
    3. 7 (left, right)
    4. +
    5. 6 (left)
    6. +
    7. 8
    8. +
    9. 10
    10. +
    11. 9
    12. +
    13. (inserted) 1
    14. +
    + +

    Becomes:

    + + +
      +
    • + + 1 + +
        +
      • + + 7 + +
          +
        • + + 8 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 2 + +
          +
        • + + 9 + +
        • +
        • + + 6 + +
        • +
        +
      • +
      +
    • +
    + + +

    Array implementation:

    + +
      +
    1. 1
    2. +
    3. 7
    4. +
    5. 2
    6. +
    7. 8
    8. +
    9. 10
    10. +
    11. 9
    12. +
    13. 6
    14. +
    + +

    Additional diagram:

    + + +
      +
    • + + 1 (2 is crossed out, arrow to 2) + +
        +
      • + + 7 + +
          +
        • + + 8 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 2 (6 is crossed out, arrow to 6) + +
          +
        • + + 9 + +
        • +
        • + + 6 (1 is crossed out) + +
        • +
        +
      • +
      +
    • +
    + + +

    In array form:

    + +
      +
    1. 1 (2 is crossed out) (left, right)
    2. +
    3. 7 (left, right)
    4. +
    5. 2 (6, 1 are crossed out)
    6. +
    7. 8
    8. +
    9. 10
    10. +
    11. 9
    12. +
    13. 6 (1 is crossed out)
    14. +
    + +

    Insert for Array-based Heap

    + +
      +
    • Variables: array A, size
    • +
    • Heap element are in A[0]A[size1]A[0] \cdots A[\text{size}-1]
    • +
    + +
    insert(k){
    +  A[size] <- k; // Add k to the new 'last leaf'
    +  v <- size
    +  p <- floor((v-1)/2) // p <- parent(v); percolate_up
    +  while(v>0 and A[v]<A[p]){
    +    swap A[v] and A[p]
    +    v <- p
    +    p <- floor((v-1)/2)
    +  }// end of percolate_up
    +  size <- size + 1;
    +}
    +
    + +

    Partially-filled Array Implementation of Binary Heap: Extract-min

    + +

    Original tree:

    + + +
      +
    • + + 2 + +
        +
      • + + 7 + +
          +
        • + + 8 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 9 + +
        • +
        • + + 11 + +
        • +
        +
      • +
      +
    • +
    + + +

    Array implemntation:

    + +
      +
    1. 2 (left, right)
    2. +
    3. 7 (left, right)
    4. +
    5. 6
    6. +
    7. 8
    8. +
    9. 10
    10. +
    11. 9
    12. +
    13. 11
    14. +
    + +

    After extract-min, tree:

    + + +
      +
    • + + 6 + +
        +
      • + + 7 + +
          +
        • + + 8 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 9 + +
          +
        • + + 11 (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    Array implementation:

    + +
      +
    1. 6
    2. +
    3. 7
    4. +
    5. 9
    6. +
    7. 8
    8. +
    9. 10
    10. +
    11. 11
    12. +
    + +

    After another extract-min, tree:

    + + +
      +
    • + + 7 + +
        +
      • + + 8 + +
          +
        • + + 11 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 9 + +
      • +
      +
    • +
    + + +

    Extract_min for Array-based Heap

    + +

    Code:

    + +
    extract_min(){
    +  temp <- A[0] // record value to return
    +  size <- size-1
    +  A[0] <- A[size] // move *old* last leaf to root
    +  i <- 0 // percolate down
    +  while(2i+1<size){// while i not a leaf
    +    child <- 2i+1 // the left child of i
    +    if(2i+2<size and A[2i+2] < A[2i+1]){
    +      child <- 2i+2 // use the right child if it exists and a smaller key
    +    }
    +    if(A[child]<A[i]){ // if order violated,
    +      swap A[child] and A[i] // swap parent+child
    +    } else {
    +      return temp
    +    }
    +  } // percolate-down
    +  return temp.
    +}
    +
    + +

    A small space-for-time trade-off in Extract-min

    + +
      +
    • Extract-min does many comparisons, e.g. ($$2i < \text{size}) to check if i is a leaf.
    • +
    • Suppose we ensure the array has size2×size\text{size} \geq 2\times \text{size} +and there is a big value, denoted \infty, that can be stored in the array but will never be a key. +and every array entry that is not a key is \infty.
    • +
    • Then, we can skip the explicit checks for being a leaf.
    • +
    + +

    Extract-min variant

    + +

    Code:

    + +
    extract_min(){
    +  temp <- A[0] // record value to return
    +  size <- size-1
    +  A[0] <- A[size] // move *old* last leaf to root
    +  A[size] <- inf // **
    +  i <- 0 // percolate down
    +  while(A[2i+1]+A[i] *or* A[2i+2]+A[i]){ // i has a child that is out of order
    +    if(A[2i+1]<A[2i+2]){ //if is a left child
    +      swap A[2i+1] and A[i]
    +      i <- 2i+1
    +    } else { //it is a right child
    +      swap A[2i+2] and A[i]
    +      i <- 2i+2
    +    }
    +  }
    +  return temp
    +}
    +
    + +

    Making a Heap from a Set

    + +
      +
    • Suppose you have n keys and want to make a heap with them.
    • +
    • Clearly can be done in time O(n log n)with n inserts.
    • +
    • Claim: the following alg. does it in time O(n).
    • +
    + +
    make_heap(T){
    +  //T is a complete b.t. with n keys.
    +  for(i=floor(n/2)-1 down to 0){
    +    call percolate_down on node i
    +  }
    +}
    +
    + +

    How does make-heap work?

    + +
      +
    • n/21\lfloor n/2 \rfloor -1 is the last internal node
    • +
    • the algorithm does a percolate-down at each internal node, working bottom-up. +
        +
      • (percolate_down makes a tree into a heap if the only node violating the order properly is the root)
      • +
      +
    • +
    + +

    Tree diagram:

    + + +
      +
    • + + 0 + +
        +
      • + + 1 + +
          +
        • + + 3 + +
            +
          • + + 7 + +
              +
            • + + 15 + +
            • +
            • + + 16 + +
            • +
            +
          • +
          • + + 8 + +
              +
            • + + 17 + +
            • +
            • + + 18 + +
            • +
            +
          • +
          +
        • +
        • + + 4 + +
            +
          • + + 9 (label: last internal node) + +
              +
            • + + 19 + +
            • +
            • + + 20 + +
            • +
            +
          • +
          • + + 10 + +
          • +
          +
        • +
        +
      • +
      • + + 2 + +
          +
        • + + 5 + +
            +
          • + + 11 + +
          • +
          • + + 12 + +
          • +
          +
        • +
        • + + 6 + +
            +
          • + + 13 + +
          • +
          • + + 14 + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Last internal node equation: n21=2121=9\lfloor \frac{n}{2} \rfloor -1 = \lfloor \frac{21}{2} \rfloor -1 = 9

    + +

    Make heap example

    + + +
      +
    • + + 10 (0) + +
        +
      • + + 9 (1) + +
          +
        • + + 7 (3) + +
            +
          • + + 3 + +
          • +
          • + + 2 + +
          • +
          +
        • +
        • + + 6 (4) + +
            +
          • + + 1 (left) + +
          • +
          +
        • +
        +
      • +
      • + + 8 (2) + +
          +
        • + + 5 + +
        • +
        • + + 4 + +
        • +
        +
      • +
      +
    • +
    + + +

    Note: n=10n=10; n/21=4\lfloor n/2 \rfloor -1 =4

    + +

    Notice: The exact order of visitng nodes does not matter – as long as we visit children before parents. [It follows that it is easy to do a recursive make-heap]

    + +

    Make heap Example

    + + +
      +
    • + + 1 (0; 10 is crossed out) + +
        +
      • + + 2 (1; 9, 10 are crossed out; checkmark) + +
          +
        • + + 3 (3; 10, 2, 7 are crossed out; checkmark)) + +
            +
          • + + 10 (3 is crossed out; checkmark) + +
          • +
          • + + 7 (2 is crossed out; checkmark) + +
          • +
          +
        • +
        • + + 6 (4; 6, 1 are crossed out; checkmark) + +
            +
          • + + 9 (left; 1,6 are crossed out) + +
          • +
          +
        • +
        +
      • +
      • + + 4 (2; 8 is crossed out; checkmark) + +
          +
        • + + 5 + +
        • +
        • + + 8 (4 is crossed out) + +
        • +
        +
      • +
      +
    • +
    + + +

    Note: n=10n=10; n/21=4\lfloor n/2 \rfloor -1 = 4

    + +

    Notice: The exact order of visitng nodes does not matter – as long as we visit children before parents. [It follows that it is easy to do a recursive make-heap]

    + +

    Make-heap Complexity

    + +
      +
    • Clearly O(n log n): n percolate-down calls, each O(log n).
    • +
    • How can we see it is actually O(n)?
    • +
    • Intuition: mark a distinct edge for for every possible swap (Time taken is bounded by max. # of swaps possible.)
    • +
    + +

    Diagram of a perfect binary tree with h=5. It is missing the rightmost 4 at h=5. +Easier than using a tree.

    + +

    Time Complexity of Make-heap

    + +
      +
    • Let S(n) be the max number of swaps carried out by make-heap on a set of size n.
    • +
    • We can bound S(n) by:
    • +
    + +

    S(n)d=0h12d(hd) +S(n) \leq \sum_{d=0}^{h-1} 2^{d} (h-d) +

    + +

    Explanation:

    + + + + + + + + + + + + + + + + + + + + + + +
    PartNote
    d=0h1\sum_{d=0}^{h-1}percolate_down is called, at most on each node at each depth d from 0 to h-1
    2d2^{d}there are 2d2^{d} nodes at depth d
    (hd)(h-d)The max # of swaps for a call to percolate-down on a node at depth d is h-d
    + +
    + +

    S(n)d=0h12d(hd)=20(h0)+21(h1)++2h2(h(h2))+hh1(h(h1)) +\begin{aligned} +S(n) & \leq \sum_{d=0}^{h-1} 2^{d} (h-d)\\ +& = 2^{0} (h-0) + 2^1 (h-1) + \cdots + 2^{h-2} (h(h-2)) + h^{h-1} (h-(h-1)) +\end{aligned} +

    + +

    Set i=h=di=h=d, d=h=id=h=i and while d ranges over 0,1,,h10,1,\cdots,h-1, i will range over h0,h1,,h(h1)h-0,h-1,\cdots,h-(h-1)

    + +

    Now:

    + +

    S(n)i=1h2hi(i)=i=1h2h2iii=1hn2ii=ni=1hi2ini=0hi2i2n +\begin{aligned} +S(n) \leq \sum_{i=1}^{h} 2^{h-i} (i) & = +\sum_{i=1}^{h} \frac{2^{h}}{2^i} i \leq \sum_{i=1}^{h} \frac{n}{2^i} i\\ +& = n \sum_{i=1}^{h} \frac{i}{2^i} \leq n \sum_{i=0}^{h} \frac{i}{2^i} \leq 2n +\end{aligned} +

    + +

    (i=0hi2i=020+121+222+323+=12+12+38+14+532+) +\Bigg ( \sum_{i=0}^{h} \frac{i}{2^i} = \frac{0}{2^0} + \frac{1}{2^1} + \frac{2}{2^2} + \frac{3}{2^3} + \cdots += \frac{1}{2} + \frac{1}{2} + \frac{3}{8} + \frac{1}{4} + \frac{5}{32} + \cdots \Bigg ) +

    + +

    Everything after 38\frac{3}{8} is less than or equal to 1.

    + +

    Complexity of Make-heap

    + +

    Work done by make-heap is bounded by a constant times the number of swaps so is O(n).

    + +

    Updating Priorities

    + +
      +
    • Suppose a heap contains an item with priority k, +and we execute update_priority(item, j).
    • +
    • We replace k with j in the heap, and then restore the order invariant: +
        +
      • +
        if j < k, do percolate_up from the modified node
        +if k < j, do percolate_down from the modified node.
        +
        +
      • +
      +
    • +
    + +

    Tree 1:

    + + +
      +
    • + + unlabled root + +
        +
      • + + unlabled child + +
      • +
      • + + unlabled child + +
          +
        • + + unlabled grandchild + +
            +
          • + + unlabled great-grandchild + +
              +
            • + + ... + +
            • +
            +
          • +
          • + + unlabled great-grandchild + +
              +
            • + + ... + +
            • +
            +
          • +
          +
        • +
        • + + unlabled grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Tree 2:

    + + +
      +
    • + + unlabled root + +
        +
      • + + unlabled child + +
      • +
      • + + unlabled child + +
          +
        • + + unlabled grandchild + +
            +
          • + + unlabled great-grandchild + +
              +
            • + + unlabled great-great-grandchild + +
                +
              • + + ... + +
              • +
              +
            • +
            • + + unlabled great-great-grandchild + +
                +
              • + + ... + +
              • +
              +
            • +
            +
          • +
          • + + unlabled great-grandchild + +
              +
            • + + unlabled great-great-grandchild + +
                +
              • + + ... + +
              • +
              +
            • +
            • + + unlabled great-great-grandchild + +
                +
              • + + ... + +
              • +
              +
            • +
            +
          • +
          +
        • +
        • + + unlabled grandchild + +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • This (restarting ???, can’t read ???) takes O(log n) time – but how do we find the right node to change??
    • +
    • To do this we need an auxiliary data structure.
    • +
    + +

    End (transcriber’s note: not the end)

    + +

    Correctness of swapping in percolate down

    + + +
      +
    • + + b + +
        +
      • + + a + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + c + +
          +
        • + + d + +
            +
          • + + ... + +
          • +
          +
        • +
        • + + e + +
            +
          • + + ... + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • Suppose we use percolating down c
    • +
    • Then c and b were previously swapped, +so we know beb\leq e, bdb\leq d, and b<cb < c.
    • +
    • If c>ec > e and ede \leq d, we swap c,e
    • +
    + +

    Now:

    + + +
      +
    • + + b + +
        +
      • + + a + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + e + +
          +
        • + + d + +
            +
          • + + ... + +
          • +
          +
        • +
        • + + c + +
            +
          • + + ... + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • we know becb\leq e \leq c and bedb\leq e \leq d
    • +
    • so order is OK, except possible below c–which we will have to look at.
    • +
    + +

    Correctness of swapping in percolate_up

    + + +
      +
    • + + b + +
        +
      • + + a + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + c + +
          +
        • + + d + +
            +
          • + + ... + +
          • +
          +
        • +
        • + + e + +
            +
          • + + ... + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • suppose we are percolating up c
    • +
    • we know cd,cec\leq d, c\leq e because we previously swapped c with d or e.
    • +
    • we know that bab\leq a
    • +
    • if c<bc< b, we swap c,b
    • +
    + +

    Now:

    + + +
      +
    • + + c + +
        +
      • + + a + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + b + +
          +
        • + + d + +
            +
          • + + ... + +
          • +
          +
        • +
        • + + e + +
            +
          • + + ... (T3) + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • we know that c<bec < b \leq e and c<bdc < b \leq d and c<bac < b \leq a
    • +
    • So order is OK, except possibly with ancestors of c, which we still must check.
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/13/13/index.html b/_site/melody/cmpt-225/13/13/index.html new file mode 100644 index 0000000..48edf3a --- /dev/null +++ b/_site/melody/cmpt-225/13/13/index.html @@ -0,0 +1,3531 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Priority Queue & Heaps

    + +

    PriorityQueue ADT (PQ)

    + +
      +
    • Stores a collection of pairs (item, priority)
    • +
    • Priorities are from some ordered set. For simplicity, we use priorities from 0,1,2,… with 0 “highest priority”.
    • +
    • Main operations: +
        +
      • insert(item, priority); adds item with priority `priority.
      • +
      • extract_m/n(); removes (& returns) item with least priority.
      • +
      • update(item, priority); changes priority of item to priority.
      • +
      +
    • +
    • We want a data structure to implement efficient PQs. (e.g. O(log n) time for all operations.
    • +
    • We (again) will use a particular kind of tree.
    • +
    + +

    Level - Order Traversal of ordered binary trees.

    + +
      +
    • visits each node of the tree once.
    • +
    • visits every node at depth i before any node at depth i+1*.
    • +
    • visits every depth-d descendants of left(v) before any depth-d descendant of right(v).
    • +
    + +

    Diagrams

    + +

    Order of traversal Diagram 1:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
          +
        • + + 4 + +
            +
          • + + 8 + +
          • +
          • + + 9 + +
          • +
          +
        • +
        • + + 5 + +
            +
          • + + 10 + +
          • +
          • + + 11 + +
          • +
          +
        • +
        +
      • +
      • + + 3 + +
          +
        • + + 6 + +
            +
          • + + 12 + +
          • +
          • + + 13 + +
          • +
          +
        • +
        • + + 7 + +
            +
          • + + 14 + +
          • +
          • + + 15 + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Order of traversal diagram 2:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
          +
        • + + 4 + +
            +
          • + + 8 + +
              +
            • + + 14 + +
            • +
            • + + 15 + +
            • +
            +
          • +
          • + + 9 + +
          • +
          +
        • +
        • + + 5 + +
            +
          • + + 10 (left) + +
              +
            • + + 16 (left) + +
                +
              • + + 20 + +
              • +
              • + + 21 + +
              • +
              +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + 3 + +
          +
        • + + 6 + +
            +
          • + + 11 + +
              +
            • + + 17 + +
            • +
            • + + 18 + +
                +
              • + + 22 (right) + +
              • +
              +
            • +
            +
          • +
          • + + 12 + +
          • +
          +
        • +
        • + + 7 + +
            +
          • + + 13 (right) + +
              +
            • + + 19 + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    * in some tests, it is bottom-up, not top-down.

    + +

    Complete Binary Tree

    + +

    A complete binary tree of height h is:

    + +
      +
    1. A binary tree of height h;
    2. +
    3. with 2d2^{d} nodes at depth d, for every 0d<h0 \leq d < h
    4. +
    5. level order traversal visits every internal node before any leaf
    6. +
    7. every internal node is proper*, except perhaps the last**, which may have just a left child.
    8. +
    + +

    Diagrams

    + +

    Example 1: X (4)

    + + +
      +
    • + + root + +
        +
      • + + child (right) + +
      • +
      +
    • +
    + + +

    Example 2: checkmark

    + + +
      +
    • + + root + +
        +
      • + + child + +
      • +
      • + + child + +
      • +
      +
    • +
    + + +

    Example 3: X (3)

    + + +
      +
    • + + root + +
        +
      • + + child + +
      • +
      • + + child + +
          +
        • + + grandchild (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 4: X (4)

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild (left) + +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 5: checkmark

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild (left) + +
        • +
        +
      • +
      • + + child + +
      • +
      +
    • +
    + + +

    Example 6: checkmark

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
        • +
        • + + grandchild + +
        • +
        +
      • +
      • + + child + +
      • +
      +
    • +
    + + +

    Example 7: checkmark

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
        • +
        • + + grandchild + +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 8: X (5)

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
        • +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        • + + grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 9: X (4)

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        • + + grandchild + +
            +
          • + + great granchild (left) + +
          • +
          +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        • + + grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 10: checkmark

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        • + + grandchild + +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
        • +
        • + + grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 11: checkmark

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        • + + grandchild + +
            +
          • + + great granchild + +
          • +
          • + + great granchild + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Unlabled tree on next slide:

    + + +
      +
    • + + ... + +
        +
      • + + node at arbitrary depth + +
          +
        • + + child node + +
        • +
        • + + child node + +
        • +
        +
      • +
      • + + node at arbitrary depth + +
          +
        • + + child node + +
        • +
        • + + child node + +
        • +
        +
      • +
      • + + node at arbitrary depth + +
          +
        • + + child node + +
        • +
        • + + child node + +
        • +
        +
      • +
      • + + node at arbitrary depth + +
          +
        • + + child node (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    Binary Heap Data Structure

    + +
      +
    • a complete binary tree (“shape invariant”)
    • +
    • with verticies labled by keys (that is: priorities) from some ordered set,
    • +
    • s.t. key((v)key(parent(v))\text{key}((v) \geq \text{key}(\text{parent}(v)) for every node v. (“order invariant”)
    • +
    + +

    Example (checkmark:

    + + +
      +
    • + + 1 + +
        +
      • + + 3 + +
          +
        • + + 6 + +
            +
          • + + 7 + +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      • + + 2 + +
          +
        • + + 5 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    Example (X):

    + + +
      +
    • + + 1 + +
        +
      • + + 3 (highlighted arrow to 2) + +
          +
        • + + 2 + +
            +
          • + + 7 + +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      • + + 6 (highlighted connection to 5) + +
          +
        • + + 5 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    This is the basic DS for implementing PQs (binary min-heap).

    + +
    + +
      +
    • How do we implement the operators so that invariants are maintained?
    • +
    • Consider Insertion: If we want to insert 14 into the heap, where should it go?
    • +
    + + +
      +
    • + + root (complete tree) + +
        +
      • + + ... (anbiguous number of node/depth) + +
          +
        • + + 10 + +
            +
          • + + 12 + +
              +
            • + + ... + +
            • +
            +
          • +
          • + + 20 + +
              +
            • + + ... + +
            • +
            +
          • +
          +
        • +
        +
      • +
      • + + ... (anbiguous number of node/depth) + +
          +
        • + + 11 + +
            +
          • + + 13 + +
              +
            • + + ... + +
            • +
            +
          • +
          • + + 19 + +
              +
            • + + ... + +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Notice: there no choice about how the shape changes:

    + +

    Example 1:

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + inserted node (left) + +
          • +
          +
        • +
        • + + grandchild + +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
        • +
        • + + grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 2:

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great grandchild + +
          • +
          • + + great grandchild + +
          • +
          +
        • +
        • + + grandchild + +
            +
          • + + great grandchild + +
          • +
          • + + inserted node + +
          • +
          +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
        • +
        • + + grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Example 3:

    + + +
      +
    • + + root + +
        +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + great grandchild + +
          • +
          • + + great grandchild + +
          • +
          +
        • +
        • + + grandchild + +
            +
          • + + great grandchild + +
          • +
          • + + great grandchild + +
          • +
          +
        • +
        +
      • +
      • + + child + +
          +
        • + + grandchild + +
            +
          • + + inserted node (left) + +
          • +
          +
        • +
        • + + grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Heap Insert

    + +

    To insert an item with key k:

    + +
      +
    1. add a new leaf v with key(v)=k, so as to maintain the shape invariant.
    2. +
    3. re-establish the order invariant by executing percolate_up(v).
    4. +
    + +

    Code:

    + +
    percolate_up(v){
    +  while(v is not root and key(v) < key(parent(v))) {
    +    swap positions of v and parent(v) in the tree
    +  }
    +}
    +
    + +

    Insert 2, then 4, then 3 into:

    + +

    Original:

    + + +
      +
    • + + 1 + +
        +
      • + + 5 + +
          +
        • + + 7 + +
            +
          • + + 12 + +
          • +
          • + + 4 + +
          • +
          +
        • +
        • + + 9 + +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 10 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    Insert 2:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 (5 is crossed out) + +
          +
        • + + 7 + +
            +
          • + + 12 + +
          • +
          • + + 4 + +
          • +
          +
        • +
        • + + 5 (9,2 are crossed out, arrow pointing to parent) + +
            +
          • + + 9 (2 is coressed out, arrow pointing to parent, left) + +
          • +
          +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 10 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    insert 4:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
          +
        • + + 7 + +
            +
          • + + 12 + +
          • +
          • + + 4 + +
          • +
          +
        • +
        • + + 4 (5 is corssed out, arrow pointing to parent) + +
            +
          • + + 9 + +
          • +
          • + + 5 (4 is coressed out, arrow pointing to parent) + +
          • +
          +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 10 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    Insert 3:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
          +
        • + + 7 + +
            +
          • + + 12 + +
          • +
          • + + 4 + +
          • +
          +
        • +
        • + + 4 + +
            +
          • + + 9 + +
          • +
          • + + 5 + +
          • +
          +
        • +
        +
      • +
      • + + 3 (6 is crossed out, double sided arrow to/from parent) + +
          +
        • + + 6 (10, 3 are crossed out, double sided arrow to/from parent) + +
            +
          • + + 10 (3 is corssed out, double sised arrow to/from parent, left) + +
          • +
          +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    Becomes:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + 3 + +
          +
        • + + 6 + +
            +
          • + + 10 (left) + +
          • +
          +
        • +
        • + + 8 + +
        • +
        +
      • +
      +
    • +
    + + +

    Heap Extract-Min:

    + +

    Consider (need result of dot dot dots):

    + + +
      +
    • + + 5 + +
        +
      • + + ... (left) + +
      • +
      • + + ... (right) + +
      • +
      +
    • +
    + + +

    We must replace the root with the smaller of its children:

    + +

    Diagram labled “OK”:

    + + +
      +
    • + + ? + +
        +
      • + + 6 (arrow towards root) + +
          +
        • + + 10 + +
        • +
        • + + 12 + +
        • +
        +
      • +
      • + + 7 + +
      • +
      +
    • +
    + + +

    Diagram labled “NOT OK”:

    + + +
      +
    • + + ? + +
        +
      • + + 7 + +
          +
        • + + 10 + +
        • +
        • + + 12 + +
        • +
        +
      • +
      • + + 6 (arrow towards root) + +
      • +
      +
    • +
    + + +

    Heap Extract-Min

    + +

    To remove the (item with the) smalled key form the heap:

    + +
      +
    1. rename the root
    2. +
    3. replace the root with the “last leaf”, so as to maintain the shape invariant.
    4. +
    5. restore the order invariant by calling percolate_down(root)
    6. +
    + +

    Percolate_down is more work than percolate_up, +because it must look at both children +to see what to do (and the children may or may not exist)

    + +

    Code:

    + +
    percolate_down(v){
    +  while(v has a child c with key(c) < key(v)){
    +    c <- child of v with the smallest key among the children of v.
    +    swap v and c in the tree
    +  }
    +}
    +
    + +

    Notice that:

    + +
      +
    • v may have 0, 1 or 2 children
    • +
    • if v has 2 children, we care about the one with the smallest key.
    • +
    + +

    Do extract-min 3 times

    + +

    Original:

    + + +
      +
    • + + 1 + +
        +
      • + + 6 + +
          +
        • + + 12 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      • + + 4 + +
          +
        • + + 11 + +
        • +
        • + + 7 + +
        • +
        +
      • +
      +
    • +
    + + +

    First extract-min:

    + + +
      +
    • + + 4 (7, 1 are crossed out) + +
        +
      • + + 6 + +
          +
        • + + 12 + +
        • +
        • + + 8 + +
        • +
        +
      • +
      • + + 7 (4 crossed out) + +
          +
        • + + 11 + +
        • +
        • + + (7 crossed out) + +
        • +
        +
      • +
      +
    • +
    + + +

    Second extract-min:

    + + +
      +
    • + + 6 (4, 11 are crossed out) + +
        +
      • + + 8 (6, 11 are crossed out) + +
          +
        • + + 12 + +
        • +
        • + + 11 (8 is crossed out) + +
        • +
        +
      • +
      • + + 7 + +
          +
        • + + (11 crossed out, left) + +
        • +
        +
      • +
      +
    • +
    + + +

    Third extract-min:

    + + +
      +
    • + + 7 (6, 11 are crossed out) + +
        +
      • + + 8 + +
          +
        • + + 12 + +
        • +
        • + + (11 is crossed out) + +
        • +
        +
      • +
      • + + 11 (7 is crossed out) + +
      • +
      +
    • +
    + + +

    Final form:

    + + +
      +
    • + + 7 + +
        +
      • + + 8 + +
          +
        • + + 12 (left) + +
        • +
        +
      • +
      • + + 11 + +
      • +
      +
    • +
    + + +

    Complexity of Heap Insert & Extract-min

    + +
      +
    • Claim: Insert & Extract-min take time O(log n) for heaps of size n.
    • +
    • Recall: A perfect binary tree of height h has 2h+112^{h+1}-1 nodes.
    • +
    • P.f.: By induction on h (or “the structure of the tree”). +
        +
      • Basis: If h=0 then we have 2011=12^{0-1} -1 = 1 nodes. (checkmark)
      • +
      • I.H.: Consider some h0h\geq 0 and assume the perfect binary tree of height h has 2h+112^{h+1} -1 nodes.
      • +
      • I.S.: show the p.b.t. of height h+1 has 2(h+1)+112^{(h+1)+1}-1 nodes. +
          +
        • The tree is: diagram of tree with left/right being of height h, and left/right plus the parent is h+1.
        • +
        • So it has 2h+11+2h+11+1=2×2h+11=2(h+1)+112^{h+1} -1 + 2^{h+1} -1 +1 = 2 \times 2^{h+1}-1 = 2^{(h+1)+1}-1 nodes. (circle with line through it)
        • +
        +
      • +
      +
    • +
    + +

    Size bounds on complete binary trees

    + +
      +
    • Every complete binary tree with height h and n nodes satisfies: 2hn2h+112^{h} \leq n \leq 2^{h+1}-1 +
        +
      • Smallest: (diagram of p.b.t. with height h and one node attached in the farthest left); #nodes = 2(h+1)+11+1=2h2^{(h+1)+1}-1+1 = 2^h
      • +
      • Largest: (diagram of p.b.t. with height h fully filled)
      • +
      +
    • +
    • So, we have:
    • +
    + +

    2hnlog22hlog2nhlog2nh=O(logn) +\begin{aligned} +2^{h} & \leq n\\ +\log_{2} 2^{h} & \leq \log_{2} n\\ +h & \leq \log_{2} n\\ +h & = O(\log n) +\end{aligned} +

    + +

    Heap insert & extract min take time O(log n)

    + +

    Linked Implementation of Heap

    + + + + +
      +
    • + + 2 (left, right, parent is null) + +
        +
      • + + 4 (left, right, parent) + +
          +
        • + + 7 (left=null, right=null, parent) + +
        • +
        • + + 8 (left=null, right=null, parent) + +
        • +
        +
      • +
      • + + 3 (left, right=null, parent) + +
          +
        • + + 5 (left=nill, right=null, parent) + +
        • +
        +
      • +
      +
    • +
    + + +

    Node:

    + +
      +
    • data
    • +
    • left
    • +
    • right
    • +
    • parent
    • +
    + +

    Array-Based Binary Heap Implementation

    + +

    Uses this embedding of a complete binary tree of size n in a size-n array:

    + +

    Tree version:

    + + +
      +
    • + + 0 + +
        +
      • + + 1 + +
          +
        • + + 3 + +
            +
          • + + 7 + +
          • +
          • + + 8 + +
          • +
          +
        • +
        • + + 4 + +
            +
          • + + 9 + +
          • +
          • + + 10 + +
          • +
          +
        • +
        +
      • +
      • + + 2 + +
          +
        • + + 5 + +
            +
          • + + 11 + +
          • +
          • + + 12 (inserted) + +
          • +
          +
        • +
        • + + 6 + +
        • +
        +
      • +
      +
    • +
    + + +

    Becomes, array version:

    + +
      +
    • 0
    • +
    • 1
    • +
    • 2
    • +
    • 3
    • +
    • 4
    • +
    • 5
    • +
    • 7
    • +
    • 8
    • +
    • 9
    • +
    • 10
    • +
    • 11
    • +
    • (inserted) 12
    • +
    + +

    ith ndoe in level-order traversal becomes ith array element.

    + + + +

    * growing and shrinking the tree is easy in the array embedding.

    + +

    Partially-filled Array Implementation of Binary Heap: Insert

    + +

    Original:

    + + +
      +
    • + + 2 + +
        +
      • + + 7 + +
          +
        • + + 8 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 9 (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    equals:

    + +
      +
    1. 2 left, right
    2. +
    3. 7 left, right
    4. +
    5. 6
    6. +
    7. 8
    8. +
    9. 10
    10. +
    11. 4
    12. +
    + +

    Insert 1:

    + + +
      +
    • + + 2 + +
        +
      • + + 7 + +
          +
        • + + 8 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 9 + +
        • +
        • + + (inserted) 1 + +
        • +
        +
      • +
      +
    • +
    + + +

    array implementation:

    + +
      +
    1. 2 (left, right)
    2. +
    3. 7 (left, right)
    4. +
    5. 6 (left)
    6. +
    7. 8
    8. +
    9. 10
    10. +
    11. 9
    12. +
    13. (inserted) 1
    14. +
    + +

    Becomes:

    + + +
      +
    • + + 1 + +
        +
      • + + 7 + +
          +
        • + + 8 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 2 + +
          +
        • + + 9 + +
        • +
        • + + 6 + +
        • +
        +
      • +
      +
    • +
    + + +

    Array implementation:

    + +
      +
    1. 1
    2. +
    3. 7
    4. +
    5. 2
    6. +
    7. 8
    8. +
    9. 10
    10. +
    11. 9
    12. +
    13. 6
    14. +
    + +

    Additional diagram:

    + + +
      +
    • + + 1 (2 is crossed out, arrow to 2) + +
        +
      • + + 7 + +
          +
        • + + 8 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 2 (6 is crossed out, arrow to 6) + +
          +
        • + + 9 + +
        • +
        • + + 6 (1 is crossed out) + +
        • +
        +
      • +
      +
    • +
    + + +

    In array form:

    + +
      +
    1. 1 (2 is crossed out) (left, right)
    2. +
    3. 7 (left, right)
    4. +
    5. 2 (6, 1 are crossed out)
    6. +
    7. 8
    8. +
    9. 10
    10. +
    11. 9
    12. +
    13. 6 (1 is crossed out)
    14. +
    + +

    Insert for Array-based Heap

    + +
      +
    • Variables: array A, size
    • +
    • Heap element are in A[0]A[size1]A[0] \cdots A[\text{size}-1]
    • +
    + +
    insert(k){
    +  A[size] <- k; // Add k to the new 'last leaf'
    +  v <- size
    +  p <- floor((v-1)/2) // p <- parent(v); percolate_up
    +  while(v>0 and A[v]<A[p]){
    +    swap A[v] and A[p]
    +    v <- p
    +    p <- floor((v-1)/2)
    +  }// end of percolate_up
    +  size <- size + 1;
    +}
    +
    + +

    Partially-filled Array Implementation of Binary Heap: Extract-min

    + +

    Original tree:

    + + +
      +
    • + + 2 + +
        +
      • + + 7 + +
          +
        • + + 8 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 6 + +
          +
        • + + 9 + +
        • +
        • + + 11 + +
        • +
        +
      • +
      +
    • +
    + + +

    Array implemntation:

    + +
      +
    1. 2 (left, right)
    2. +
    3. 7 (left, right)
    4. +
    5. 6
    6. +
    7. 8
    8. +
    9. 10
    10. +
    11. 9
    12. +
    13. 11
    14. +
    + +

    After extract-min, tree:

    + + +
      +
    • + + 6 + +
        +
      • + + 7 + +
          +
        • + + 8 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 9 + +
          +
        • + + 11 (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    Array implementation:

    + +
      +
    1. 6
    2. +
    3. 7
    4. +
    5. 9
    6. +
    7. 8
    8. +
    9. 10
    10. +
    11. 11
    12. +
    + +

    After another extract-min, tree:

    + + +
      +
    • + + 7 + +
        +
      • + + 8 + +
          +
        • + + 11 + +
        • +
        • + + 10 + +
        • +
        +
      • +
      • + + 9 + +
      • +
      +
    • +
    + + +

    Extract_min for Array-based Heap

    + +

    Code:

    + +
    extract_min(){
    +  temp <- A[0] // record value to return
    +  size <- size-1
    +  A[0] <- A[size] // move *old* last leaf to root
    +  i <- 0 // percolate down
    +  while(2i+1<size){// while i not a leaf
    +    child <- 2i+1 // the left child of i
    +    if(2i+2<size and A[2i+2] < A[2i+1]){
    +      child <- 2i+2 // use the right child if it exists and a smaller key
    +    }
    +    if(A[child]<A[i]){ // if order violated,
    +      swap A[child] and A[i] // swap parent+child
    +    } else {
    +      return temp
    +    }
    +  } // percolate-down
    +  return temp.
    +}
    +
    + +

    A small space-for-time trade-off in Extract-min

    + +
      +
    • Extract-min does many comparisons, e.g. ($$2i < \text{size}) to check if i is a leaf.
    • +
    • Suppose we ensure the array has size2×size\text{size} \geq 2\times \text{size} +and there is a big value, denoted \infty, that can be stored in the array but will never be a key. +and every array entry that is not a key is \infty.
    • +
    • Then, we can skip the explicit checks for being a leaf.
    • +
    + +

    Extract-min variant

    + +

    Code:

    + +
    extract_min(){
    +  temp <- A[0] // record value to return
    +  size <- size-1
    +  A[0] <- A[size] // move *old* last leaf to root
    +  A[size] <- inf // **
    +  i <- 0 // percolate down
    +  while(A[2i+1]+A[i] *or* A[2i+2]+A[i]){ // i has a child that is out of order
    +    if(A[2i+1]<A[2i+2]){ //if is a left child
    +      swap A[2i+1] and A[i]
    +      i <- 2i+1
    +    } else { //it is a right child
    +      swap A[2i+2] and A[i]
    +      i <- 2i+2
    +    }
    +  }
    +  return temp
    +}
    +
    + +

    Making a Heap from a Set

    + +
      +
    • Suppose you have n keys and want to make a heap with them.
    • +
    • Clearly can be done in time O(n log n)with n inserts.
    • +
    • Claim: the following alg. does it in time O(n).
    • +
    + +
    make_heap(T){
    +  //T is a complete b.t. with n keys.
    +  for(i=floor(n/2)-1 down to 0){
    +    call percolate_down on node i
    +  }
    +}
    +
    + +

    How does make-heap work?

    + +
      +
    • n/21\lfloor n/2 \rfloor -1 is the last internal node
    • +
    • the algorithm does a percolate-down at each internal node, working bottom-up. +
        +
      • (percolate_down makes a tree into a heap if the only node violating the order properly is the root)
      • +
      +
    • +
    + +

    Tree diagram:

    + + +
      +
    • + + 0 + +
        +
      • + + 1 + +
          +
        • + + 3 + +
            +
          • + + 7 + +
              +
            • + + 15 + +
            • +
            • + + 16 + +
            • +
            +
          • +
          • + + 8 + +
              +
            • + + 17 + +
            • +
            • + + 18 + +
            • +
            +
          • +
          +
        • +
        • + + 4 + +
            +
          • + + 9 (label: last internal node) + +
              +
            • + + 19 + +
            • +
            • + + 20 + +
            • +
            +
          • +
          • + + 10 + +
          • +
          +
        • +
        +
      • +
      • + + 2 + +
          +
        • + + 5 + +
            +
          • + + 11 + +
          • +
          • + + 12 + +
          • +
          +
        • +
        • + + 6 + +
            +
          • + + 13 + +
          • +
          • + + 14 + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +

    Last internal node equation: n21=2121=9\lfloor \frac{n}{2} \rfloor -1 = \lfloor \frac{21}{2} \rfloor -1 = 9

    + +

    Make heap example

    + + +
      +
    • + + 10 (0) + +
        +
      • + + 9 (1) + +
          +
        • + + 7 (3) + +
            +
          • + + 3 + +
          • +
          • + + 2 + +
          • +
          +
        • +
        • + + 6 (4) + +
            +
          • + + 1 (left) + +
          • +
          +
        • +
        +
      • +
      • + + 8 (2) + +
          +
        • + + 5 + +
        • +
        • + + 4 + +
        • +
        +
      • +
      +
    • +
    + + +

    Note: n=10n=10; n/21=4\lfloor n/2 \rfloor -1 =4

    + +

    Notice: The exact order of visitng nodes does not matter – as long as we visit children before parents. [It follows that it is easy to do a recursive make-heap]

    + +

    Make heap Example

    + + +
      +
    • + + 1 (0; 10 is crossed out) + +
        +
      • + + 2 (1; 9, 10 are crossed out; checkmark) + +
          +
        • + + 3 (3; 10, 2, 7 are crossed out; checkmark)) + +
            +
          • + + 10 (3 is crossed out; checkmark) + +
          • +
          • + + 7 (2 is crossed out; checkmark) + +
          • +
          +
        • +
        • + + 6 (4; 6, 1 are crossed out; checkmark) + +
            +
          • + + 9 (left; 1,6 are crossed out) + +
          • +
          +
        • +
        +
      • +
      • + + 4 (2; 8 is crossed out; checkmark) + +
          +
        • + + 5 + +
        • +
        • + + 8 (4 is crossed out) + +
        • +
        +
      • +
      +
    • +
    + + +

    Note: n=10n=10; n/21=4\lfloor n/2 \rfloor -1 = 4

    + +

    Notice: The exact order of visitng nodes does not matter – as long as we visit children before parents. [It follows that it is easy to do a recursive make-heap]

    + +

    Make-heap Complexity

    + +
      +
    • Clearly O(n log n): n percolate-down calls, each O(log n).
    • +
    • How can we see it is actually O(n)?
    • +
    • Intuition: mark a distinct edge for for every possible swap (Time taken is bounded by max. # of swaps possible.)
    • +
    + +

    Diagram of a perfect binary tree with h=5. It is missing the rightmost 4 at h=5. +Easier than using a tree.

    + +

    Time Complexity of Make-heap

    + +
      +
    • Let S(n) be the max number of swaps carried out by make-heap on a set of size n.
    • +
    • We can bound S(n) by:
    • +
    + +

    S(n)d=0h12d(hd) +S(n) \leq \sum_{d=0}^{h-1} 2^{d} (h-d) +

    + +

    Explanation:

    + + + + + + + + + + + + + + + + + + + + + + +
    PartNote
    d=0h1\sum_{d=0}^{h-1}percolate_down is called, at most on each node at each depth d from 0 to h-1
    2d2^{d}there are 2d2^{d} nodes at depth d
    (hd)(h-d)The max # of swaps for a call to percolate-down on a node at depth d is h-d
    + +
    + +

    S(n)d=0h12d(hd)=20(h0)+21(h1)++2h2(h(h2))+hh1(h(h1)) +\begin{aligned} +S(n) & \leq \sum_{d=0}^{h-1} 2^{d} (h-d)\\ +& = 2^{0} (h-0) + 2^1 (h-1) + \cdots + 2^{h-2} (h(h-2)) + h^{h-1} (h-(h-1)) +\end{aligned} +

    + +

    Set i=h=di=h=d, d=h=id=h=i and while d ranges over 0,1,,h10,1,\cdots,h-1, i will range over h0,h1,,h(h1)h-0,h-1,\cdots,h-(h-1)

    + +

    Now:

    + +

    S(n)i=1h2hi(i)=i=1h2h2iii=1hn2ii=ni=1hi2ini=0hi2i2n +\begin{aligned} +S(n) \leq \sum_{i=1}^{h} 2^{h-i} (i) & = +\sum_{i=1}^{h} \frac{2^{h}}{2^i} i \leq \sum_{i=1}^{h} \frac{n}{2^i} i\\ +& = n \sum_{i=1}^{h} \frac{i}{2^i} \leq n \sum_{i=0}^{h} \frac{i}{2^i} \leq 2n +\end{aligned} +

    + +

    (i=0hi2i=020+121+222+323+=12+12+38+14+532+) +\Bigg ( \sum_{i=0}^{h} \frac{i}{2^i} = \frac{0}{2^0} + \frac{1}{2^1} + \frac{2}{2^2} + \frac{3}{2^3} + \cdots += \frac{1}{2} + \frac{1}{2} + \frac{3}{8} + \frac{1}{4} + \frac{5}{32} + \cdots \Bigg ) +

    + +

    Everything after 38\frac{3}{8} is less than or equal to 1.

    + +

    Complexity of Make-heap

    + +

    Work done by make-heap is bounded by a constant times the number of swaps so is O(n).

    + +

    Updating Priorities

    + +
      +
    • Suppose a heap contains an item with priority k, +and we execute update_priority(item, j).
    • +
    • We replace k with j in the heap, and then restore the order invariant: +
        +
      • +
        if j < k, do percolate_up from the modified node
        +if k < j, do percolate_down from the modified node.
        +
        +
      • +
      +
    • +
    + +

    Tree 1:

    + + +
      +
    • + + unlabled root + +
        +
      • + + unlabled child + +
      • +
      • + + unlabled child + +
          +
        • + + unlabled grandchild + +
            +
          • + + unlabled great-grandchild + +
              +
            • + + ... + +
            • +
            +
          • +
          • + + unlabled great-grandchild + +
              +
            • + + ... + +
            • +
            +
          • +
          +
        • +
        • + + unlabled grandchild + +
        • +
        +
      • +
      +
    • +
    + + +

    Tree 2:

    + + +
      +
    • + + unlabled root + +
        +
      • + + unlabled child + +
      • +
      • + + unlabled child + +
          +
        • + + unlabled grandchild + +
            +
          • + + unlabled great-grandchild + +
              +
            • + + unlabled great-great-grandchild + +
                +
              • + + ... + +
              • +
              +
            • +
            • + + unlabled great-great-grandchild + +
                +
              • + + ... + +
              • +
              +
            • +
            +
          • +
          • + + unlabled great-grandchild + +
              +
            • + + unlabled great-great-grandchild + +
                +
              • + + ... + +
              • +
              +
            • +
            • + + unlabled great-great-grandchild + +
                +
              • + + ... + +
              • +
              +
            • +
            +
          • +
          +
        • +
        • + + unlabled grandchild + +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • This (restarting ???, can’t read ???) takes O(log n) time – but how do we find the right node to change??
    • +
    • To do this we need an auxiliary data structure.
    • +
    + +

    End (transcriber’s note: not the end)

    + +

    Correctness of swapping in percolate down

    + + +
      +
    • + + b + +
        +
      • + + a + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + c + +
          +
        • + + d + +
            +
          • + + ... + +
          • +
          +
        • +
        • + + e + +
            +
          • + + ... + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • Suppose we use percolating down c
    • +
    • Then c and b were previously swapped, +so we know beb\leq e, bdb\leq d, and b<cb < c.
    • +
    • If c>ec > e and ede \leq d, we swap c,e
    • +
    + +

    Now:

    + + +
      +
    • + + b + +
        +
      • + + a + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + e + +
          +
        • + + d + +
            +
          • + + ... + +
          • +
          +
        • +
        • + + c + +
            +
          • + + ... + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • we know becb\leq e \leq c and bedb\leq e \leq d
    • +
    • so order is OK, except possible below c–which we will have to look at.
    • +
    + +

    Correctness of swapping in percolate_up

    + + +
      +
    • + + b + +
        +
      • + + a + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + c + +
          +
        • + + d + +
            +
          • + + ... + +
          • +
          +
        • +
        • + + e + +
            +
          • + + ... + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • suppose we are percolating up c
    • +
    • we know cd,cec\leq d, c\leq e because we previously swapped c with d or e.
    • +
    • we know that bab\leq a
    • +
    • if c<bc< b, we swap c,b
    • +
    + +

    Now:

    + + +
      +
    • + + c + +
        +
      • + + a + +
          +
        • + + ... + +
        • +
        +
      • +
      • + + b + +
          +
        • + + d + +
            +
          • + + ... + +
          • +
          +
        • +
        • + + e + +
            +
          • + + ... (T3) + +
          • +
          +
        • +
        +
      • +
      +
    • +
    + + +
      +
    • we know that c<bec < b \leq e and c<bdc < b \leq d and c<bac < b \leq a
    • +
    • So order is OK, except possibly with ancestors of c, which we still must check.
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/13/225 L13 - PQ and Heaps.pdf b/_site/melody/cmpt-225/13/225 L13 - PQ and Heaps.pdf new file mode 100644 index 0000000..01c01fe Binary files /dev/null and b/_site/melody/cmpt-225/13/225 L13 - PQ and Heaps.pdf differ diff --git a/_site/melody/cmpt-225/14/14.html b/_site/melody/cmpt-225/14/14.html new file mode 100644 index 0000000..47500eb --- /dev/null +++ b/_site/melody/cmpt-225/14/14.html @@ -0,0 +1,200 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Big-Omega & Big-Theta

    + +

    Big-Oh Gives Upper Bounds

    + +

    For f,g functions NR+\N \rightarrow \R^+,
    +f(n)=Ω(g(n))f(n) = \Omega(g(n)) means there are c,n0>0c,n_0 > 0 s.t. +n>n0f(n)c×g(n)\forall n>n_0\quad f(n) \geq c \times g(n)

    + +

    i.e. f is asymptotically bounded from below by g

    + +

    (A graph with two lines. +f is a blue line with a wobbly, but mostly linear movement upwards. +c times g is a red line which has a similar trajectory, but end up just slightly below the blue line.)

    + +

    Note: We may have C<<1C << 1

    + +

    or f grows asymptotically at least as fast as g.

    + +

    Big-Oh & Big-Omega are Duals

    + +

    Fact: f(n)=Ω(g(n))g(n)=O(f(n))f(n) = \Omega(g(n)) \leftrightarrow g(n) = O(f(n))

    + +

    Pf: f(n)=Ω(g(n))f(n) = \Omega(g(n)):

    + +
      +
    • +     n0,c>0 s.t. n>n0    f(n)cg(n)\iff \exists n_0, c' > 0 \text{ s.t. } n>n_0 \implies f(n) \geq c' \cdot g(n) +
    • +
    • +     n0,c>0 s.t. n>n0    cg(n)f(n)\iff \exists n_0,c' > 0 \text{ s.t. } n>n_0 \implies c'\cdot g(n) \leq f(n) +
    • +
    •     n0,c>0 s.t. n>n0    g(n)cf(n)\iff\exists n_0,c > 0 \text{ s.t. } n>n_0 \implies g(n) \leq c\cdot f(n) // letting c=1cc = \frac{1}{c'}
    • +
    • +     g(n)O(f(n))\iff g(n) O(f(n)) +
    • +
    + +

    So: f grows at least as fast as g     \iff g grows at most as fast as f.

    + +

    Examples: Worse-case times

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OperationO(1)O(1)Ω(1)\Omega(1)O(logn)O(\log n) (highlighted)Ω(logn)\Omega(\log n) (highlighted)O(n)O(n)Ω(n)\Omega(n)O(nlogn)O(n \log n)Ω(nlogn)\Omega(n \log n)
    stack push/pop (highlighted)✓ (green)✓ (green)❌ (blue)❌ (blue)❌ (blue)
    unqueue/dequeue✓ (green)✓ (green)❌ (blue)❌ (blue)❌ (blue)
    heap insert or extract min❌ (pink)✓ (green)✓ (green)❌ (blue)❌ (blue)
    AVL-tree find, insert, remove❌ (pink)✓ (green)✓ (green)❌ (blue)❌ (blue)
    make_heap❌ (pink)❌ (pink)✓ (green)✓ (green)❌ (blue)
    BST find, insert, remove❌ (pink)❌ (pink)✓ (green)✓ (green)❌ (blue)
    Sorting❌ (pink)❌ (pink)❌ (pink)✓ (green)✓ (green)? (red)
    + +

    Big-Theta Expresses “Tight Bounds”

    + +

    For f,g functions NR+\N \rightarrow \R^+, +f(n)=Θ(g(n))f(n) = \Theta(g(n)) means there are c1,c2,n0>0 s.t. n>n0    c1g(n)f(n)c2g(n)c_1, c_2,n_0 > 0 \text{ s.t. } n>n_0 \implies c_1\cdot g(n) \leq f(n) \leq c_2\cdot g(n)

    + +

    i.e. f asymptotically bounded from above and below by g

    + +

    (Diagram with three lines, wobbly but roughly linear, with them stacked in the following order from top to bottom:

    + +
      +
    • + c2gc_2 \cdot g +
    • +
    • f
    • +
    • c1gc_1 \cdot g)
    • +
    + +

    or f grows asymptotically the same as g.

    + +

    “Grows the same as” is systemetic

    + +

    Fact: f(n)=Θ(g(n))    g(n)=Θ(f(n))f(n) = \Theta(g(n)) \iff g(n) = \Theta(f(n))

    + +

    i.e. f grows the same as g     \iff g grows the same as f.

    + +

    P.f.: $$f(n) = \Theta(g(n))

    + +
      +
    • +     c1,c2,n0>0 s.t. n>n0c1g(n)f(n)c2g(n)\iff \exists \text{c1},\text{c2},n_0 > 0 \text{ s.t. } \forall n>n_0 \quad c1\cdot g(n) \leq f(n) \leq c2\cdot g(n) +
    • +
    • +     c1,c2,n0>0 s.t. n>n01c2f(n)g(n)1c1f(n)\iff \exists c_1,c2,n_0 > 0 \text{ s.t. } \forall n>n_0 \quad \frac{1}{c2} f(n) \leq g(n) \leq \frac{1}{c1} f(n) +
    • +
    • +     c3,c4,n0>0 s.t. n>n0c3f(n)g(n)c4f(n)\iff \exists c_3,c_4,n_0 > 0 \text{ s.t. } \forall n>n_0 c_3 f(n) \leq g(n) \leq c_4 f(n) +
    • +
    • +     g(n)=Θ(f(n))\iff g(n) = \Theta(f(n)) +
    • +
    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/14/14/index.html b/_site/melody/cmpt-225/14/14/index.html new file mode 100644 index 0000000..47500eb --- /dev/null +++ b/_site/melody/cmpt-225/14/14/index.html @@ -0,0 +1,200 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Big-Omega & Big-Theta

    + +

    Big-Oh Gives Upper Bounds

    + +

    For f,g functions NR+\N \rightarrow \R^+,
    +f(n)=Ω(g(n))f(n) = \Omega(g(n)) means there are c,n0>0c,n_0 > 0 s.t. +n>n0f(n)c×g(n)\forall n>n_0\quad f(n) \geq c \times g(n)

    + +

    i.e. f is asymptotically bounded from below by g

    + +

    (A graph with two lines. +f is a blue line with a wobbly, but mostly linear movement upwards. +c times g is a red line which has a similar trajectory, but end up just slightly below the blue line.)

    + +

    Note: We may have C<<1C << 1

    + +

    or f grows asymptotically at least as fast as g.

    + +

    Big-Oh & Big-Omega are Duals

    + +

    Fact: f(n)=Ω(g(n))g(n)=O(f(n))f(n) = \Omega(g(n)) \leftrightarrow g(n) = O(f(n))

    + +

    Pf: f(n)=Ω(g(n))f(n) = \Omega(g(n)):

    + +
      +
    • +     n0,c>0 s.t. n>n0    f(n)cg(n)\iff \exists n_0, c' > 0 \text{ s.t. } n>n_0 \implies f(n) \geq c' \cdot g(n) +
    • +
    • +     n0,c>0 s.t. n>n0    cg(n)f(n)\iff \exists n_0,c' > 0 \text{ s.t. } n>n_0 \implies c'\cdot g(n) \leq f(n) +
    • +
    •     n0,c>0 s.t. n>n0    g(n)cf(n)\iff\exists n_0,c > 0 \text{ s.t. } n>n_0 \implies g(n) \leq c\cdot f(n) // letting c=1cc = \frac{1}{c'}
    • +
    • +     g(n)O(f(n))\iff g(n) O(f(n)) +
    • +
    + +

    So: f grows at least as fast as g     \iff g grows at most as fast as f.

    + +

    Examples: Worse-case times

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OperationO(1)O(1)Ω(1)\Omega(1)O(logn)O(\log n) (highlighted)Ω(logn)\Omega(\log n) (highlighted)O(n)O(n)Ω(n)\Omega(n)O(nlogn)O(n \log n)Ω(nlogn)\Omega(n \log n)
    stack push/pop (highlighted)✓ (green)✓ (green)❌ (blue)❌ (blue)❌ (blue)
    unqueue/dequeue✓ (green)✓ (green)❌ (blue)❌ (blue)❌ (blue)
    heap insert or extract min❌ (pink)✓ (green)✓ (green)❌ (blue)❌ (blue)
    AVL-tree find, insert, remove❌ (pink)✓ (green)✓ (green)❌ (blue)❌ (blue)
    make_heap❌ (pink)❌ (pink)✓ (green)✓ (green)❌ (blue)
    BST find, insert, remove❌ (pink)❌ (pink)✓ (green)✓ (green)❌ (blue)
    Sorting❌ (pink)❌ (pink)❌ (pink)✓ (green)✓ (green)? (red)
    + +

    Big-Theta Expresses “Tight Bounds”

    + +

    For f,g functions NR+\N \rightarrow \R^+, +f(n)=Θ(g(n))f(n) = \Theta(g(n)) means there are c1,c2,n0>0 s.t. n>n0    c1g(n)f(n)c2g(n)c_1, c_2,n_0 > 0 \text{ s.t. } n>n_0 \implies c_1\cdot g(n) \leq f(n) \leq c_2\cdot g(n)

    + +

    i.e. f asymptotically bounded from above and below by g

    + +

    (Diagram with three lines, wobbly but roughly linear, with them stacked in the following order from top to bottom:

    + +
      +
    • + c2gc_2 \cdot g +
    • +
    • f
    • +
    • c1gc_1 \cdot g)
    • +
    + +

    or f grows asymptotically the same as g.

    + +

    “Grows the same as” is systemetic

    + +

    Fact: f(n)=Θ(g(n))    g(n)=Θ(f(n))f(n) = \Theta(g(n)) \iff g(n) = \Theta(f(n))

    + +

    i.e. f grows the same as g     \iff g grows the same as f.

    + +

    P.f.: $$f(n) = \Theta(g(n))

    + +
      +
    • +     c1,c2,n0>0 s.t. n>n0c1g(n)f(n)c2g(n)\iff \exists \text{c1},\text{c2},n_0 > 0 \text{ s.t. } \forall n>n_0 \quad c1\cdot g(n) \leq f(n) \leq c2\cdot g(n) +
    • +
    • +     c1,c2,n0>0 s.t. n>n01c2f(n)g(n)1c1f(n)\iff \exists c_1,c2,n_0 > 0 \text{ s.t. } \forall n>n_0 \quad \frac{1}{c2} f(n) \leq g(n) \leq \frac{1}{c1} f(n) +
    • +
    • +     c3,c4,n0>0 s.t. n>n0c3f(n)g(n)c4f(n)\iff \exists c_3,c_4,n_0 > 0 \text{ s.t. } \forall n>n_0 c_3 f(n) \leq g(n) \leq c_4 f(n) +
    • +
    • +     g(n)=Θ(f(n))\iff g(n) = \Theta(f(n)) +
    • +
    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/14/225 L14 - Big Omega and Theta.pdf b/_site/melody/cmpt-225/14/225 L14 - Big Omega and Theta.pdf new file mode 100644 index 0000000..04f118c Binary files /dev/null and b/_site/melody/cmpt-225/14/225 L14 - Big Omega and Theta.pdf differ diff --git a/_site/melody/cmpt-225/15/15.html b/_site/melody/cmpt-225/15/15.html new file mode 100644 index 0000000..dd8cd75 --- /dev/null +++ b/_site/melody/cmpt-225/15/15.html @@ -0,0 +1,1031 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Sorting

    + +

    Sorting

    + +
      +
    • re-arranging elements of a sequence S s.t. S0S1S2Sn1S \text{ s.t. } S_0 \leq S_1 \leq S_2 \leq \cdots \leq S_{n-1}
    • +
    • We will look at 5 sorting algorithms: +
        +
      • 3 iterative
      • +
      • 2 recursive
      • +
      +
    • +
    + +

    The iterative algorithms

    + +
      +
    • maintain a partition: “unsorted part” & “srtoed part”
    • +
    • sort a sequence of n elements in n-1 stages
    • +
    • at each stage, move 1 element from the unsorted part to the sorted part: +
        +
      • (Diagram of a generic array with unseen “sorted” items on the left and “unsorted” elements on the right. Caption: “1 stage moves 1 element”)
      • +
      +
    • +
    + +
    sort(A){
    +  * initialize 
    +  * repeat n-1 times
    +    * move 1 element from unsorted and sorted part
    +}
    +
    + +
      +
    • The algorithms differ in how they: +
        +
      • select an element to remove from the unsorted part
      • +
      • insert it into the sorted part
      • +
      +
    • +
    + +

    Insertion Sort

    + +
      +
    • Initially sorted part is just A[0]
    • +
    • Repeat n-1 times +
        +
      • remove the first element from the unsorted part
      • +
      • insert it into the sorted part (shifting elements to the right as needed)
      • +
      +
    • +
    + +

    Diagram of array as it gets sorted in three stages:

    + +
      +
    • Stage 1: sorted is leftmost (0th) element; n-1 elements are unsorted on the right.
    • +
    • Stage 2: approximately half of the array is sorted; an arrow points from the leftmost value inside the unsorted side to an arbitrary position inside the sorted side.
    • +
    • Stage 3: just over half of the array is sorted now.
    • +
    + +

    Code:

    + +
    insertion sort(A){
    +  for(i=1 to n-1){
    +    pivot = A[i] // first element in unsorted part
    +    j=i-1
    +    // The following loop shifts all elements in sorted parts that are larger than pivot 1 "to the right"
    +    while(j>=0 AND A[i] > pivot){
    +      A[j+i] = A[j] // shift jth
    +      j = j-1
    +    }
    +    A[j+i] = pivot // move pivot into position.
    +  }
    +}
    +
    + +

    Insertion Sort Example

    + +

    Stages:

    + +
      +
    • Stage 0: Original +
        +
      • + + + + + + + + + + + +
        542613
        +
      • +
      +
    • +
    • Stage 1: (label: 4) +
        +
      • + + + + + + + + + + + +
        452613
        +
      • +
      +
    • +
    • Stage 2: (label: 2) +
        +
      • + + + + + + + + + + + +
        245613
        +
      • +
      +
    • +
    • Stage 3: (label: 6) +
        +
      • + + + + + + + + + + + +
        245613
        +
      • +
      +
    • +
    • Stage 4: (label: 1) +
        +
      • + + + + + + + + + + + +
        124563
        +
      • +
      +
    • +
    • Stage 5: (label: 3) +
        +
      • + + + + + + + + + + + +
        123456
        +
      • +
      +
    • +
    + +

    Selection Sort

    + +
      +
    • initially sorted part is empty
    • +
    • repeat n-1 times +
        +
      • find the smallest element in the unsorted part
      • +
      • make it the first position which becomes the now last position of sorted part.
      • +
      +
    • +
    + +

    Diagram of parts:

    + +
      +
    • Initially, the entire array is all unsorted.
    • +
    • Over time the sorted elements stack up on the left.
    • +
    • Every time an element is moved, it is moved from the unsorted part (lowest element) and swapped with the element just after the end of the sorted part, making the sorted part one element bigger.
    • +
    • Eventually all elements are sorted in descending order.
    • +
    + +

    Code:

    + +
    selection_sort(A){
    +  for(i=1 to n-1){
    +    // find min element of unsorted
    +    j=i-1 // j is index of min found so far.
    +    k=i
    +    while(k<n){
    +      if(A[k]<A[j]) j=k;
    +      k=k+1
    +    }
    +    swap A[i-1] and A[j]
    +  }
    +}
    +
    + +

    Process of Selection Sort:

    + +
      +
    • Original: all unsorted +
        +
      • + + + + + + + + + + + +
        542613
        +
      • +
      +
    • +
    • Stage 1: [0] is sorted; 1 and 5 swap +
        +
      • + + + + + + + + + + + +
        142653
        +
      • +
      +
    • +
    • Stage 2: [0..1] is sorted; 2 and 4 swap +
        +
      • + + + + + + + + + + + +
        124653
        +
      • +
      +
    • +
    • Stage 3: [0..2] is sorted; 3 and 4 swap +
        +
      • + + + + + + + + + + + +
        123654
        +
      • +
      +
    • +
    • Stage 4: [0..3] is sorted; 4 and 6 swap +
        +
      • + + + + + + + + + + + +
        123456
        +
      • +
      +
    • +
    • Stage 5: [0..4] is sorted; annotation: s.t. s (final stage) +
        +
      • + + + + + + + + + + + +
        123456
        +
      • +
      +
    • +
    + +

    Heapsort (Selection Sort is crossed out)

    + +
      +
    • Initially sorted part empty
    • +
    • (highlighted) make unsorted part into a heap
    • +
    • repeat n-1 times +
        +
      • find the smallest element in the unsorted part (Note: heap extract takes log(n) time vs. Θ(n) for the scan in selection sort)
      • +
      • move it to the first position which becomes the new last position of the started part.
      • +
      +
    • +
    + +

    Consider the organization of array contents:

    + +
      +
    1. (Diagram of array with sorted half on the right and the unsorted half on the left.) A purple arrow points to the leftmost element in the unsorted portion. The note reads: “if this is the root of the heap, then it is also the smallest element in the unsorted part, so is in its correct final position. +To use this arrangement, the root of the heap keeps moving, so we have lots of shifting to do.”
    2. +
    3. (A diagram showing the same array with sorted and unsorted halves.) A purple arrow points to the last element in the array; it points to a purple circle. A purple square is at the leftmost element of the unsorted half (the one discussed in the last item). The note reads: “If this is the root of the, then everything works: +
        +
      • We extract the final element (purple circle); move the last leaf (purple square) to the root + do a percolate-down; +store the final element (purple circle) where the last element of the unsorted list (purple square) was, +which is now free, and is the correct final location for the previously final element (purple circle); after which we have:
      • +
      +
        +
      • (Diagram of array with the “sorted” half extended one cell over to encompass the purple circle) + * But: we must re-code our heap implementation s.t. the root is at A[n-1], with the result that the indexing is now less intuitive.
      • +
      +
    4. +
    5. Instead, we use a max-heap, and this arrangement: +
        +
      • (Diagram showcasing, as previously, a sorted half to the right and an unsorted half on the left. An orange circle labeled “root of heap” is the very first element of the list and the unsorted half; an orange square labeled “last leaf” sits at the end (rightmost side) of the unsorted half.)
      • +
      • The heap root is at A[0]
      • +
      • Heap Extraction remove the root of the heap (orange circle), moves the last leaf (orange square) to A[0], +freeing up the spot where the root of the heap (orange circle) belongs.
      • +
      • This leaves us with: (Diagram of the orange circle near the middle of the array, at the leftmost portion of the sorted half. The orange square is in the center of the unsorted half.)
      • +
      • Re-coding a min heap into a max heap is just replacing < with > and vice versa.
      • +
      +
    6. +
    + +

    Heapsort (Selectioon Sort is crossed out)

    + +
      +
    • initially sorted part empty
    • +
    • (highlighted) make unsorted part into a max heap
    • +
    • repeat n-1 times: +
        +
      • find the largest (smallest is crossed out) element in the unsorted part
      • +
      • move it to the last (first is crossed out) position which becomes the new first (last is crossed out) position of the sorted part.
      • +
      +
    • +
    + +

    Code:

    + +
    heapsort(A){
    +  buildMaxHeap(A)
    +  for(i=1 to n-1){
    +    A[n-1] extractMax()
    +  }
    +}
    +
    + +

    Stages of sorting:

    + +
      +
    • (Diagram of unsorted array with first element labeled as “heap with max here”.)
    • +
    • (Diagram of a half-sorted array showing the swap between the first and last elements of the unsorted portion of the array. Labeled as “take max element from root…” and “take last leaf from end of heap” with arrows pointing to one another.)
    • +
    • (Diagram of a half+1 sorted array, displaying the new sorted element that has been swapped from the root element of the heap. Labeled as “newest element of sorted part” and “this is the final location” (the new element just swapped), and “y new root of heap (which then gets percolated down)” (what is now the first element of the array, which was also just swapped).)
    • +
    + +

    Unsorted heap of size 1 has smallest element.

    + +

    Heapsort with in-line percolate-down

    + +

    Code:

    + +
    heapsort(A){
    +  makeMaxHeap(A)
    +  for(i=1 to n-1){
    +    swap A[0] and A[n-1] // move last leaf to root and old root to where last leaf was
    +    size <- n-i+1 // size of heap = size of unsorted part
    +    // start of percolate down
    +    j <- 0
    +    while(2j+1 < size){
    +      child <- 2j+1
    +      if(2j+2 < size AND A[2j+2] < A[2j+1]){
    +        child <- 2j+2
    +      }
    +      if(A[child]<A[j]){
    +        swap A[child] and A[j]
    +        j <- child
    +      } else {
    +        j <- size // termite the while
    +      }
    +    } // end of percolate down
    +  }
    +}
    +
    + +

    Heapsort Example

    + +
      +
    • Original: +
        +
      • + + + + + + + + + + + +
        542613
        +
      • +
      +
    • +
    • Turn into heap: +
        +
      • + + + + + + + + + + + +
        653412
        +
      • +
      +
    • +
    • Swap root (6) and last unsorted element (2): +
        +
      • + + + + + + + + + + + +
        253416
        +
      • +
      +
    • +
    • Re-heap the unsorted portion: [0..4] +
        +
      • + + + + + + + + + + + +
        543216
        +
      • +
      +
    • +
    • Swap root (5) and the last unsorted element (2): +
        +
      • + + + + + + + + + + + +
        143256
        +
      • +
      +
    • +
    • Re-heap the unsorted portion: [0..3] +
        +
      • + + + + + + + + + + + +
        423156
        +
      • +
      +
    • +
    • Swap root (4) and the last unsorted element (1): +
        +
      • + + + + + + + + + + + +
        123456
        +
      • +
      +
    • +
    • Re-heap unsorted portion: [0..2] +
        +
      • + + + + + + + + + + + +
        321456
        +
      • +
      +
    • +
    • Swap root (3) and last unsorted element (1): +
        +
      • + + + + + + + + + + + +
        123456
        +
      • +
      +
    • +
    • Re-heap unsorted portion: [0..1] +
        +
      • + + + + + + + + + + + +
        213456
        +
      • +
      +
    • +
    • Swap root (2) and last unsorted element (1): +
        +
      • + + + + + + + + + + + +
        123456
        +
      • +
      +
    • +
    • Array is sorted because unsorted portion is only 1 element.
    • +
    + +

    Tree version of above (heap):

    + +

    Original:

    + + +
      +
    • + + 5 + +
        +
      • + + 4 + +
          +
        • + + 6 + +
        • +
        • + + 1 + +
        • +
        +
      • +
      • + + 2 + +
          +
        • + + 3 (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    After re-heap and one removal:

    + + +
      +
    • + + 2 + +
        +
      • + + 5 + +
          +
        • + + 2 + +
        • +
        • + + 1 + +
        • +
        +
      • +
      • + + 3 + +
      • +
      +
    • +
    + + +

    After a second re-heap and removal:

    + + +
      +
    • + + 1 + +
        +
      • + + 4 + +
          +
        • + + 2 (left) + +
        • +
        +
      • +
      • + + 3 + +
      • +
      +
    • +
    + + +

    After a third:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
      • +
      • + + 3 + +
      • +
      +
    • +
    + + +

    Examples stop here.

    + +

    Heapsort Example (2)

    + +

    (Repeat same as above, except with different trees.)

    + +

    Trees (Transcriber’s note: these trees don’t seem relavant to me…. but maybe I’m wrong):

    + + +
      +
    • + + 2 (crossed out with orange 5) + +
        +
      • + + 5 (crossed out next to orange 2 which is also crossed out; an orange 4 is not crossed out) + +
          +
        • + + 4 (crossed out with orange 2) + +
        • +
        • + + 1 + +
        • +
        +
      • +
      • + + 3 + +
      • +
      +
    • +
    + + +
      +
    • + + 1 (crossed out with an orange 4) + +
        +
      • + + 4 (crossed out with orange 1, which is also crossed out; an orange 2, not crossed out is next to it) + +
          +
        • + + 2 (left; crossed out with orange 1) + +
        • +
        +
      • +
      • + + 3 + +
      • +
      +
    • +
    + + +
      +
    • + + 1 (orange 2) + +
        +
      • + + 2 (left; orange 1) + +
      • +
      +
    • +
    + + +

    Time Complexity of Iterative Sorting Algorithms

    + +
      +
    • each algorithm does exactly n-1 stages
    • +
    • the work done at the ith stage varies with the algorithm (& input).
    • +
    • we take # from item comparisons as a measure of work/time*.
    • +
    + +
    +
    Selection Sort
    +
    exactly n-i comparisons to find num element in unsorted part
    +
    Insertion Sort
    +
    between 1 and i comparisons to find location for pivot
    +
    HeapSort
    +
    between 1 and 2log2(ni1)2\log_{2} (n-i-1) comparisons for percolate-down
    +
    + +

    * Number of comparisons

    + +
      +
    • We must verify # comparisons (or some constant times # comparisons) is an upper bound on work done by each algorithm.
    • +
    • +

      of assignments (& swaps) also matters in actual run time.

      +
    • +
    + +

    Selection Sort

    + +

    On input of size n, # of comparisons is always (regardless of input):

    + +

    i=1n1(ni)=i=1n1i=S(ni)=(n1)(n)2=n2n2=Θ(n2) +\begin{aligned} +\sum_{i=1}^{n-1} (n-i) & = \sum_{i=1}^{n-1} i\\ +& = S(n-i)\\ +& = \frac{(n-1)(n)}{2}\\ +& = \frac{n^2 -n}{2}\\ +& = \Theta(n^2) +\end{aligned} +

    + +

    Insertion Sort – Worst Case

    + +

    Upper Bound: # comparisonsi=1n1i=n2n2=O(n2)\text{\# comparisons} \leq \sum_{i=1}^{n-1} i = \frac{n^{2} -n}{2} = O(n^{2})

    + +

    Lower Bound:

    + +
      +
    • Worst case initial sequence is in reverse order. e.g.: +
        +
      • + + + + + + + + + + +
        nn-1n-21
        +
      • +
      +
    • +
    • In the ith stage we have: +
        +
      • + + + + + + + + + + + + + + +
        n-i+1n-1+2nn-1n-1-121
        +
      • +
      • + + + + + + + + + + + + + + +
        n-in-i+1n-1nn-i-121
        +
      • +
      +
    • +
    • This takes i comparisons, because the sorted part is of size i.
    • +
    • So, # comparisonsi=1n1=Ω(n2)\text{\# comparisons} \leq \sum{i=1}^{n-1} = \Omega(n^{2})
    • +
    + +

    So, insertion sort worst case is Θ(n2)\Theta(n^{2})

    + +

    (Transcriber’s note: I’m fairly certain you can only use big-O notation when talking about worst case scenario, not Theta. But I’m leaving it as written.)

    + +

    Insertion Sort Best Case

    + +

    Best case: initial sequence is fully ordered.

    + +

    Then: In each stage, exactly 1 comparison is made.

    + +

    So, # comparisons=n1=Θ(n)\text{\# comparisons} = n-1 = \Theta(n).

    + +

    Heapsort Worst Case

    + +

    Upper bound:

    + +

    # comparisonsi=1n12log2(ni+1)=2i=1n1log2(i+1)=2i=1n1log2n=2nlog2n=O(nlogn) +\begin{aligned} +\text{\# comparisons} & \leq \sum_{i=1}^{n-1} 2\log_{2} (n-i+1)\\ +& = 2\sum_{i=1}^{n-1} \log_{2} (i+1)\\ +& = \leq 2\sum_{i=1}^{n-1} \log_{2} n\\ +& = \leq 2n\log_{2} n\\ +& = O(n \log n) +\end{aligned} +

    + +

    Lower Bound? (empty space)

    + +

    Base Case? (What input would lead to no movement during percolate-down? +What if we exclude this case?)

    + +

    Recursive Divide & Conquer Sorting

    + +

    TODO

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/15/15/index.html b/_site/melody/cmpt-225/15/15/index.html new file mode 100644 index 0000000..dd8cd75 --- /dev/null +++ b/_site/melody/cmpt-225/15/15/index.html @@ -0,0 +1,1031 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Sorting

    + +

    Sorting

    + +
      +
    • re-arranging elements of a sequence S s.t. S0S1S2Sn1S \text{ s.t. } S_0 \leq S_1 \leq S_2 \leq \cdots \leq S_{n-1}
    • +
    • We will look at 5 sorting algorithms: +
        +
      • 3 iterative
      • +
      • 2 recursive
      • +
      +
    • +
    + +

    The iterative algorithms

    + +
      +
    • maintain a partition: “unsorted part” & “srtoed part”
    • +
    • sort a sequence of n elements in n-1 stages
    • +
    • at each stage, move 1 element from the unsorted part to the sorted part: +
        +
      • (Diagram of a generic array with unseen “sorted” items on the left and “unsorted” elements on the right. Caption: “1 stage moves 1 element”)
      • +
      +
    • +
    + +
    sort(A){
    +  * initialize 
    +  * repeat n-1 times
    +    * move 1 element from unsorted and sorted part
    +}
    +
    + +
      +
    • The algorithms differ in how they: +
        +
      • select an element to remove from the unsorted part
      • +
      • insert it into the sorted part
      • +
      +
    • +
    + +

    Insertion Sort

    + +
      +
    • Initially sorted part is just A[0]
    • +
    • Repeat n-1 times +
        +
      • remove the first element from the unsorted part
      • +
      • insert it into the sorted part (shifting elements to the right as needed)
      • +
      +
    • +
    + +

    Diagram of array as it gets sorted in three stages:

    + +
      +
    • Stage 1: sorted is leftmost (0th) element; n-1 elements are unsorted on the right.
    • +
    • Stage 2: approximately half of the array is sorted; an arrow points from the leftmost value inside the unsorted side to an arbitrary position inside the sorted side.
    • +
    • Stage 3: just over half of the array is sorted now.
    • +
    + +

    Code:

    + +
    insertion sort(A){
    +  for(i=1 to n-1){
    +    pivot = A[i] // first element in unsorted part
    +    j=i-1
    +    // The following loop shifts all elements in sorted parts that are larger than pivot 1 "to the right"
    +    while(j>=0 AND A[i] > pivot){
    +      A[j+i] = A[j] // shift jth
    +      j = j-1
    +    }
    +    A[j+i] = pivot // move pivot into position.
    +  }
    +}
    +
    + +

    Insertion Sort Example

    + +

    Stages:

    + +
      +
    • Stage 0: Original +
        +
      • + + + + + + + + + + + +
        542613
        +
      • +
      +
    • +
    • Stage 1: (label: 4) +
        +
      • + + + + + + + + + + + +
        452613
        +
      • +
      +
    • +
    • Stage 2: (label: 2) +
        +
      • + + + + + + + + + + + +
        245613
        +
      • +
      +
    • +
    • Stage 3: (label: 6) +
        +
      • + + + + + + + + + + + +
        245613
        +
      • +
      +
    • +
    • Stage 4: (label: 1) +
        +
      • + + + + + + + + + + + +
        124563
        +
      • +
      +
    • +
    • Stage 5: (label: 3) +
        +
      • + + + + + + + + + + + +
        123456
        +
      • +
      +
    • +
    + +

    Selection Sort

    + +
      +
    • initially sorted part is empty
    • +
    • repeat n-1 times +
        +
      • find the smallest element in the unsorted part
      • +
      • make it the first position which becomes the now last position of sorted part.
      • +
      +
    • +
    + +

    Diagram of parts:

    + +
      +
    • Initially, the entire array is all unsorted.
    • +
    • Over time the sorted elements stack up on the left.
    • +
    • Every time an element is moved, it is moved from the unsorted part (lowest element) and swapped with the element just after the end of the sorted part, making the sorted part one element bigger.
    • +
    • Eventually all elements are sorted in descending order.
    • +
    + +

    Code:

    + +
    selection_sort(A){
    +  for(i=1 to n-1){
    +    // find min element of unsorted
    +    j=i-1 // j is index of min found so far.
    +    k=i
    +    while(k<n){
    +      if(A[k]<A[j]) j=k;
    +      k=k+1
    +    }
    +    swap A[i-1] and A[j]
    +  }
    +}
    +
    + +

    Process of Selection Sort:

    + +
      +
    • Original: all unsorted +
        +
      • + + + + + + + + + + + +
        542613
        +
      • +
      +
    • +
    • Stage 1: [0] is sorted; 1 and 5 swap +
        +
      • + + + + + + + + + + + +
        142653
        +
      • +
      +
    • +
    • Stage 2: [0..1] is sorted; 2 and 4 swap +
        +
      • + + + + + + + + + + + +
        124653
        +
      • +
      +
    • +
    • Stage 3: [0..2] is sorted; 3 and 4 swap +
        +
      • + + + + + + + + + + + +
        123654
        +
      • +
      +
    • +
    • Stage 4: [0..3] is sorted; 4 and 6 swap +
        +
      • + + + + + + + + + + + +
        123456
        +
      • +
      +
    • +
    • Stage 5: [0..4] is sorted; annotation: s.t. s (final stage) +
        +
      • + + + + + + + + + + + +
        123456
        +
      • +
      +
    • +
    + +

    Heapsort (Selection Sort is crossed out)

    + +
      +
    • Initially sorted part empty
    • +
    • (highlighted) make unsorted part into a heap
    • +
    • repeat n-1 times +
        +
      • find the smallest element in the unsorted part (Note: heap extract takes log(n) time vs. Θ(n) for the scan in selection sort)
      • +
      • move it to the first position which becomes the new last position of the started part.
      • +
      +
    • +
    + +

    Consider the organization of array contents:

    + +
      +
    1. (Diagram of array with sorted half on the right and the unsorted half on the left.) A purple arrow points to the leftmost element in the unsorted portion. The note reads: “if this is the root of the heap, then it is also the smallest element in the unsorted part, so is in its correct final position. +To use this arrangement, the root of the heap keeps moving, so we have lots of shifting to do.”
    2. +
    3. (A diagram showing the same array with sorted and unsorted halves.) A purple arrow points to the last element in the array; it points to a purple circle. A purple square is at the leftmost element of the unsorted half (the one discussed in the last item). The note reads: “If this is the root of the, then everything works: +
        +
      • We extract the final element (purple circle); move the last leaf (purple square) to the root + do a percolate-down; +store the final element (purple circle) where the last element of the unsorted list (purple square) was, +which is now free, and is the correct final location for the previously final element (purple circle); after which we have:
      • +
      +
        +
      • (Diagram of array with the “sorted” half extended one cell over to encompass the purple circle) + * But: we must re-code our heap implementation s.t. the root is at A[n-1], with the result that the indexing is now less intuitive.
      • +
      +
    4. +
    5. Instead, we use a max-heap, and this arrangement: +
        +
      • (Diagram showcasing, as previously, a sorted half to the right and an unsorted half on the left. An orange circle labeled “root of heap” is the very first element of the list and the unsorted half; an orange square labeled “last leaf” sits at the end (rightmost side) of the unsorted half.)
      • +
      • The heap root is at A[0]
      • +
      • Heap Extraction remove the root of the heap (orange circle), moves the last leaf (orange square) to A[0], +freeing up the spot where the root of the heap (orange circle) belongs.
      • +
      • This leaves us with: (Diagram of the orange circle near the middle of the array, at the leftmost portion of the sorted half. The orange square is in the center of the unsorted half.)
      • +
      • Re-coding a min heap into a max heap is just replacing < with > and vice versa.
      • +
      +
    6. +
    + +

    Heapsort (Selectioon Sort is crossed out)

    + +
      +
    • initially sorted part empty
    • +
    • (highlighted) make unsorted part into a max heap
    • +
    • repeat n-1 times: +
        +
      • find the largest (smallest is crossed out) element in the unsorted part
      • +
      • move it to the last (first is crossed out) position which becomes the new first (last is crossed out) position of the sorted part.
      • +
      +
    • +
    + +

    Code:

    + +
    heapsort(A){
    +  buildMaxHeap(A)
    +  for(i=1 to n-1){
    +    A[n-1] extractMax()
    +  }
    +}
    +
    + +

    Stages of sorting:

    + +
      +
    • (Diagram of unsorted array with first element labeled as “heap with max here”.)
    • +
    • (Diagram of a half-sorted array showing the swap between the first and last elements of the unsorted portion of the array. Labeled as “take max element from root…” and “take last leaf from end of heap” with arrows pointing to one another.)
    • +
    • (Diagram of a half+1 sorted array, displaying the new sorted element that has been swapped from the root element of the heap. Labeled as “newest element of sorted part” and “this is the final location” (the new element just swapped), and “y new root of heap (which then gets percolated down)” (what is now the first element of the array, which was also just swapped).)
    • +
    + +

    Unsorted heap of size 1 has smallest element.

    + +

    Heapsort with in-line percolate-down

    + +

    Code:

    + +
    heapsort(A){
    +  makeMaxHeap(A)
    +  for(i=1 to n-1){
    +    swap A[0] and A[n-1] // move last leaf to root and old root to where last leaf was
    +    size <- n-i+1 // size of heap = size of unsorted part
    +    // start of percolate down
    +    j <- 0
    +    while(2j+1 < size){
    +      child <- 2j+1
    +      if(2j+2 < size AND A[2j+2] < A[2j+1]){
    +        child <- 2j+2
    +      }
    +      if(A[child]<A[j]){
    +        swap A[child] and A[j]
    +        j <- child
    +      } else {
    +        j <- size // termite the while
    +      }
    +    } // end of percolate down
    +  }
    +}
    +
    + +

    Heapsort Example

    + +
      +
    • Original: +
        +
      • + + + + + + + + + + + +
        542613
        +
      • +
      +
    • +
    • Turn into heap: +
        +
      • + + + + + + + + + + + +
        653412
        +
      • +
      +
    • +
    • Swap root (6) and last unsorted element (2): +
        +
      • + + + + + + + + + + + +
        253416
        +
      • +
      +
    • +
    • Re-heap the unsorted portion: [0..4] +
        +
      • + + + + + + + + + + + +
        543216
        +
      • +
      +
    • +
    • Swap root (5) and the last unsorted element (2): +
        +
      • + + + + + + + + + + + +
        143256
        +
      • +
      +
    • +
    • Re-heap the unsorted portion: [0..3] +
        +
      • + + + + + + + + + + + +
        423156
        +
      • +
      +
    • +
    • Swap root (4) and the last unsorted element (1): +
        +
      • + + + + + + + + + + + +
        123456
        +
      • +
      +
    • +
    • Re-heap unsorted portion: [0..2] +
        +
      • + + + + + + + + + + + +
        321456
        +
      • +
      +
    • +
    • Swap root (3) and last unsorted element (1): +
        +
      • + + + + + + + + + + + +
        123456
        +
      • +
      +
    • +
    • Re-heap unsorted portion: [0..1] +
        +
      • + + + + + + + + + + + +
        213456
        +
      • +
      +
    • +
    • Swap root (2) and last unsorted element (1): +
        +
      • + + + + + + + + + + + +
        123456
        +
      • +
      +
    • +
    • Array is sorted because unsorted portion is only 1 element.
    • +
    + +

    Tree version of above (heap):

    + +

    Original:

    + + +
      +
    • + + 5 + +
        +
      • + + 4 + +
          +
        • + + 6 + +
        • +
        • + + 1 + +
        • +
        +
      • +
      • + + 2 + +
          +
        • + + 3 (left) + +
        • +
        +
      • +
      +
    • +
    + + +

    After re-heap and one removal:

    + + +
      +
    • + + 2 + +
        +
      • + + 5 + +
          +
        • + + 2 + +
        • +
        • + + 1 + +
        • +
        +
      • +
      • + + 3 + +
      • +
      +
    • +
    + + +

    After a second re-heap and removal:

    + + +
      +
    • + + 1 + +
        +
      • + + 4 + +
          +
        • + + 2 (left) + +
        • +
        +
      • +
      • + + 3 + +
      • +
      +
    • +
    + + +

    After a third:

    + + +
      +
    • + + 1 + +
        +
      • + + 2 + +
      • +
      • + + 3 + +
      • +
      +
    • +
    + + +

    Examples stop here.

    + +

    Heapsort Example (2)

    + +

    (Repeat same as above, except with different trees.)

    + +

    Trees (Transcriber’s note: these trees don’t seem relavant to me…. but maybe I’m wrong):

    + + +
      +
    • + + 2 (crossed out with orange 5) + +
        +
      • + + 5 (crossed out next to orange 2 which is also crossed out; an orange 4 is not crossed out) + +
          +
        • + + 4 (crossed out with orange 2) + +
        • +
        • + + 1 + +
        • +
        +
      • +
      • + + 3 + +
      • +
      +
    • +
    + + +
      +
    • + + 1 (crossed out with an orange 4) + +
        +
      • + + 4 (crossed out with orange 1, which is also crossed out; an orange 2, not crossed out is next to it) + +
          +
        • + + 2 (left; crossed out with orange 1) + +
        • +
        +
      • +
      • + + 3 + +
      • +
      +
    • +
    + + +
      +
    • + + 1 (orange 2) + +
        +
      • + + 2 (left; orange 1) + +
      • +
      +
    • +
    + + +

    Time Complexity of Iterative Sorting Algorithms

    + +
      +
    • each algorithm does exactly n-1 stages
    • +
    • the work done at the ith stage varies with the algorithm (& input).
    • +
    • we take # from item comparisons as a measure of work/time*.
    • +
    + +
    +
    Selection Sort
    +
    exactly n-i comparisons to find num element in unsorted part
    +
    Insertion Sort
    +
    between 1 and i comparisons to find location for pivot
    +
    HeapSort
    +
    between 1 and 2log2(ni1)2\log_{2} (n-i-1) comparisons for percolate-down
    +
    + +

    * Number of comparisons

    + +
      +
    • We must verify # comparisons (or some constant times # comparisons) is an upper bound on work done by each algorithm.
    • +
    • +

      of assignments (& swaps) also matters in actual run time.

      +
    • +
    + +

    Selection Sort

    + +

    On input of size n, # of comparisons is always (regardless of input):

    + +

    i=1n1(ni)=i=1n1i=S(ni)=(n1)(n)2=n2n2=Θ(n2) +\begin{aligned} +\sum_{i=1}^{n-1} (n-i) & = \sum_{i=1}^{n-1} i\\ +& = S(n-i)\\ +& = \frac{(n-1)(n)}{2}\\ +& = \frac{n^2 -n}{2}\\ +& = \Theta(n^2) +\end{aligned} +

    + +

    Insertion Sort – Worst Case

    + +

    Upper Bound: # comparisonsi=1n1i=n2n2=O(n2)\text{\# comparisons} \leq \sum_{i=1}^{n-1} i = \frac{n^{2} -n}{2} = O(n^{2})

    + +

    Lower Bound:

    + +
      +
    • Worst case initial sequence is in reverse order. e.g.: +
        +
      • + + + + + + + + + + +
        nn-1n-21
        +
      • +
      +
    • +
    • In the ith stage we have: +
        +
      • + + + + + + + + + + + + + + +
        n-i+1n-1+2nn-1n-1-121
        +
      • +
      • + + + + + + + + + + + + + + +
        n-in-i+1n-1nn-i-121
        +
      • +
      +
    • +
    • This takes i comparisons, because the sorted part is of size i.
    • +
    • So, # comparisonsi=1n1=Ω(n2)\text{\# comparisons} \leq \sum{i=1}^{n-1} = \Omega(n^{2})
    • +
    + +

    So, insertion sort worst case is Θ(n2)\Theta(n^{2})

    + +

    (Transcriber’s note: I’m fairly certain you can only use big-O notation when talking about worst case scenario, not Theta. But I’m leaving it as written.)

    + +

    Insertion Sort Best Case

    + +

    Best case: initial sequence is fully ordered.

    + +

    Then: In each stage, exactly 1 comparison is made.

    + +

    So, # comparisons=n1=Θ(n)\text{\# comparisons} = n-1 = \Theta(n).

    + +

    Heapsort Worst Case

    + +

    Upper bound:

    + +

    # comparisonsi=1n12log2(ni+1)=2i=1n1log2(i+1)=2i=1n1log2n=2nlog2n=O(nlogn) +\begin{aligned} +\text{\# comparisons} & \leq \sum_{i=1}^{n-1} 2\log_{2} (n-i+1)\\ +& = 2\sum_{i=1}^{n-1} \log_{2} (i+1)\\ +& = \leq 2\sum_{i=1}^{n-1} \log_{2} n\\ +& = \leq 2n\log_{2} n\\ +& = O(n \log n) +\end{aligned} +

    + +

    Lower Bound? (empty space)

    + +

    Base Case? (What input would lead to no movement during percolate-down? +What if we exclude this case?)

    + +

    Recursive Divide & Conquer Sorting

    + +

    TODO

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-225/15/225 L15 - Sorting.pdf b/_site/melody/cmpt-225/15/225 L15 - Sorting.pdf new file mode 100644 index 0000000..0138489 Binary files /dev/null and b/_site/melody/cmpt-225/15/225 L15 - Sorting.pdf differ diff --git a/_site/melody/cmpt-225/cmpt-225-08.zip b/_site/melody/cmpt-225/cmpt-225-08.zip new file mode 100644 index 0000000..0d06127 Binary files /dev/null and b/_site/melody/cmpt-225/cmpt-225-08.zip differ diff --git a/_site/melody/cmpt-225/cmpt-225-09.zip b/_site/melody/cmpt-225/cmpt-225-09.zip new file mode 100644 index 0000000..fc04ce5 Binary files /dev/null and b/_site/melody/cmpt-225/cmpt-225-09.zip differ diff --git a/_site/melody/cmpt-225/cmpt-225-10.zip b/_site/melody/cmpt-225/cmpt-225-10.zip new file mode 100644 index 0000000..078f267 Binary files /dev/null and b/_site/melody/cmpt-225/cmpt-225-10.zip differ diff --git a/_site/melody/cmpt-225/cmpt-225-11-interim.zip b/_site/melody/cmpt-225/cmpt-225-11-interim.zip new file mode 100644 index 0000000..21f6942 Binary files /dev/null and b/_site/melody/cmpt-225/cmpt-225-11-interim.zip differ diff --git a/_site/melody/cmpt-225/cmpt-225-11.zip b/_site/melody/cmpt-225/cmpt-225-11.zip new file mode 100644 index 0000000..e7edbd7 Binary files /dev/null and b/_site/melody/cmpt-225/cmpt-225-11.zip differ diff --git a/_site/melody/cmpt-225/cmpt-225-12.zip b/_site/melody/cmpt-225/cmpt-225-12.zip new file mode 100644 index 0000000..93869b5 Binary files /dev/null and b/_site/melody/cmpt-225/cmpt-225-12.zip differ diff --git a/_site/melody/cmpt-225/cmpt-225-13-interim.zip b/_site/melody/cmpt-225/cmpt-225-13-interim.zip new file mode 100644 index 0000000..edb6bde Binary files /dev/null and b/_site/melody/cmpt-225/cmpt-225-13-interim.zip differ diff --git a/_site/melody/cmpt-225/cmpt-225-13.zip b/_site/melody/cmpt-225/cmpt-225-13.zip new file mode 100644 index 0000000..e4fbff3 Binary files /dev/null and b/_site/melody/cmpt-225/cmpt-225-13.zip differ diff --git a/_site/melody/cmpt-225/cmpt-225-14.zip b/_site/melody/cmpt-225/cmpt-225-14.zip new file mode 100644 index 0000000..2741e08 Binary files /dev/null and b/_site/melody/cmpt-225/cmpt-225-14.zip differ diff --git a/_site/melody/cmpt-225/cmpt-225-15-interim.zip b/_site/melody/cmpt-225/cmpt-225-15-interim.zip new file mode 100644 index 0000000..f84475a Binary files /dev/null and b/_site/melody/cmpt-225/cmpt-225-15-interim.zip differ diff --git a/_site/melody/cmpt-225/cmpt-225-l5.zip b/_site/melody/cmpt-225/cmpt-225-l5.zip new file mode 100644 index 0000000..4664848 Binary files /dev/null and b/_site/melody/cmpt-225/cmpt-225-l5.zip differ diff --git a/_site/melody/cmpt-225/cmpt-225-l6.zip b/_site/melody/cmpt-225/cmpt-225-l6.zip new file mode 100644 index 0000000..9aef2ad Binary files /dev/null and b/_site/melody/cmpt-225/cmpt-225-l6.zip differ diff --git a/_site/melody/cmpt-225/cmpt-225-l7.zip b/_site/melody/cmpt-225/cmpt-225-l7.zip new file mode 100644 index 0000000..6d9f574 Binary files /dev/null and b/_site/melody/cmpt-225/cmpt-225-l7.zip differ diff --git a/_site/melody/cmpt-225/cmpt225-04.zip b/_site/melody/cmpt-225/cmpt225-04.zip new file mode 100644 index 0000000..9d04494 Binary files /dev/null and b/_site/melody/cmpt-225/cmpt225-04.zip differ diff --git a/_site/melody/cmpt-295/01/01/index.html b/_site/melody/cmpt-295/01/01/index.html new file mode 100644 index 0000000..fa1042f --- /dev/null +++ b/_site/melody/cmpt-295/01/01/index.html @@ -0,0 +1,24 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    00112&11102=00102 +0011_{2} \And 1110_{2} = 0010_{2} +

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/01/Lecture_01_Activity_Code.pdf b/_site/melody/cmpt-295/01/Lecture_01_Activity_Code.pdf new file mode 100644 index 0000000..5abb1e8 Binary files /dev/null and b/_site/melody/cmpt-295/01/Lecture_01_Activity_Code.pdf differ diff --git a/_site/melody/cmpt-295/01/Lecture_01_Activity_Code.pdf.txt b/_site/melody/cmpt-295/01/Lecture_01_Activity_Code.pdf.txt new file mode 100644 index 0000000..ebb2c7d --- /dev/null +++ b/_site/melody/cmpt-295/01/Lecture_01_Activity_Code.pdf.txt @@ -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 diff --git a/_site/melody/cmpt-295/01/Lecture_01_Course_Overview.pdf b/_site/melody/cmpt-295/01/Lecture_01_Course_Overview.pdf new file mode 100644 index 0000000..2a28cfb Binary files /dev/null and b/_site/melody/cmpt-295/01/Lecture_01_Course_Overview.pdf differ diff --git a/_site/melody/cmpt-295/01/Lecture_01_Course_Overview_Annotated.md b/_site/melody/cmpt-295/01/Lecture_01_Course_Overview_Annotated.md new file mode 100644 index 0000000..a7444e8 --- /dev/null +++ b/_site/melody/cmpt-295/01/Lecture_01_Course_Overview_Annotated.md @@ -0,0 +1,105 @@ + + +# Welcome to CMPT 295 +## Introduction to Computer Systems + +My name is Anne Lavergne + +Lecture 1 – Course Overview + Activity + + + +## Today’s Menu [1] + +* COVID Protocol +* What is CMPT 295? + * What shall we learn in CMPT 295? + * What should we already know? + * Which resources do we have to help us learn all this? +* Activity +* Questions + +## COVID protocol – About masks! [2] +Here is a message from Elizabeth Elle, SFU Vice Provost Learning & Teaching, based on the public health order: + +* 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. + * If we forget our mask, disposable masks are available from Student Central in Burnaby and at the information desks in Vancouver and Surrey. + * 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. + * 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. +* 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. + +## What is CMPT 295? [3] + +* 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 => microprocessor, memory, … a car with its hood up. +* This knowledge will allow us to become more efficient software developers + +## The big picture: [4] + +In CMPT 295, we shall learn … + +* C programs (.c) -- How our code and data are represented in memory +* Assembly programs (.s) -- How a compiler transforms our code into machine executable code in several steps +* Object (.o) file an executable -- How a compiler optimizes (or not) our code +* Computer executes it -- How a microprocessor is designed and how it executes our code +* CPU, Memory -- How memory is designed + +How all of this can impact the execution of our code How to write more efficient and reliable code: +* Be able to find and eliminate bugs +more efficiently +* Be able to ascertain program performance and tune it by optimizing our code + +## What should we already know? [5] + +* Write correct C programs + * C constructs (variables, data types, pointers, if/else, switch/case, for/while/do while, function calls, arrays, …) +* What a stack is and how it works +* Binary/decimal/hexadecimal numeral systems + * How to convert from one numeral system to the others + * Basic arithmetic +* Perform Boolean algebra using and, or, not, xor + +## Which resources do we have? [6] + +* Course web site +https://www2.cs.sfu.ca/CourseCentral/295/alavergn/index.html +* Textbook + * Computer Systems: A Programmer's Perspective, 3/E, Randal E. Bryant, David R. O'Hallaron, Pearson, 2016 +* Labs in CSIL (Computing Science Instructional Lab) + * Target Machine: CSIL workstation + * Linux platform (or OS) + * C programming language + * x86-64 assembly language + * gcc compiler +* Instructor and TAs - Office hours + +## Activity - Discover our resources [7] + +Instructions: +1. Form teams of 3 to 4 +2. Do Lecture 1 Activity on CourSys +3. Time: about 30 minutes + +## Question? [8] + +Blank page. + +## Summary [9] + +* COVID Protocol +* What is CMPT 295? + * What shall we learn in CMPT 295? + * What should we already know? + * Which resources do we have to help us learn all this? +* Activity +* Questions + +## Next Lecture [10] + +* Data Representation + * Representing information as bits + +* To get ready for our next lecture: + * Optional: Read Chapter 1 of textbook + * Not so optional: Read Section 2.1 of Chapter 2 + * Download the partial lecture notes found under the column Lecture in the table on our course web site + diff --git a/_site/melody/cmpt-295/01/Lecture_01_Course_Overview_Annotated.pdf b/_site/melody/cmpt-295/01/Lecture_01_Course_Overview_Annotated.pdf new file mode 100644 index 0000000..13a0bf7 Binary files /dev/null and b/_site/melody/cmpt-295/01/Lecture_01_Course_Overview_Annotated.pdf differ diff --git a/_site/melody/cmpt-295/01/Lecture_01_Course_Overview_annotated.html b/_site/melody/cmpt-295/01/Lecture_01_Course_Overview_annotated.html new file mode 100644 index 0000000..a747f24 --- /dev/null +++ b/_site/melody/cmpt-295/01/Lecture_01_Course_Overview_annotated.html @@ -0,0 +1,170 @@ +

    + +

    Welcome to CMPT 295

    + +

    Introduction to Computer Systems

    + +

    My name is Anne Lavergne

    + +

    Lecture 1 – Course Overview + Activity

    + +

    + +

    Today’s Menu [1]

    + +
      +
    • COVID Protocol
    • +
    • What is CMPT 295? + +
        +
      • What shall we learn in CMPT 295?
      • +
      • What should we already know?
      • +
      • Which resources do we have to help us learn all this?
      • +
      +
    • +
    • Activity
    • +
    • Questions
    • +
    + + +

    COVID protocol – About masks! [2]

    + +

    Here is a message from Elizabeth Elle, SFU Vice Provost Learning & Teaching, based on the public health order:

    + +
      +
    • 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. + +
        +
      • If we forget our mask, disposable masks are available from Student Central in Burnaby and at the information desks in Vancouver and Surrey.
      • +
      • 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.
      • +
      • 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.
      • +
      +
    • +
    • 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.
    • +
    + + +

    What is CMPT 295? [3]

    + +
      +
    • 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 => microprocessor, memory, … a car with its hood up.
    • +
    • This knowledge will allow us to become more efficient software developers
    • +
    + + +

    The big picture: [4]

    + +

    In CMPT 295, we shall learn …

    + +

    C programs (.c) – How our code and data are represented in memory

    + +

    Assembly programs (.s) – How a compiler transforms our code into machine executable code in several steps

    + +

    Object (.o) file an executable – How a compiler optimizes (or not) our code

    + +

    Computer executes it – How a microprocessor is designed and how it executes our code

    + +

    CPU, Memory – How memory is designed

    + +

    How all of this can impact the execution of our code How to write more efficient and reliable code: +* Be able to find and eliminate bugs +more efficiently +* Be able to ascertain program performance and tune it by optimizing our code

    + +

    What should we already know? [5]

    + +
      +
    • Write correct C programs + +
        +
      • C constructs (variables, data types, pointers, if/else, switch/case, for/while/do while, function calls, arrays, …)
      • +
      +
    • +
    • What a stack is and how it works
    • +
    • Binary/decimal/hexadecimal numeral systems + +
        +
      • How to convert from one numeral system to the others
      • +
      • Basic arithmetic
      • +
      +
    • +
    • Perform Boolean algebra using and, or, not, xor
    • +
    + + +

    Which resources do we have? [6]

    + +
      +
    • Course web site +https://www2.cs.sfu.ca/CourseCentral/295/alavergn/index.html
    • +
    • Textbook + +
        +
      • Computer Systems: A Programmer’s Perspective, 3/E, Randal E. Bryant, David R. O'Hallaron, Pearson, 2016
      • +
      +
    • +
    • Labs in CSIL (Computing Science Instructional Lab) + +
        +
      • Target Machine: CSIL workstation + +
          +
        • Linux platform (or OS)
        • +
        • C programming language
        • +
        • x86-64 assembly language
        • +
        • gcc compiler
        • +
        +
      • +
      +
    • +
    • Instructor and TAs - Office hours
    • +
    + + +

    Activity - Discover our resources [7]

    + +

    Instructions: +1. Form teams of 3 to 4 +2. Do Lecture 1 Activity on CourSys +3. Time: about 30 minutes

    + +

    Question? [8]

    + +

    Blank page.

    + +

    Summary [9]

    + +
      +
    • COVID Protocol
    • +
    • What is CMPT 295? + +
        +
      • What shall we learn in CMPT 295?
      • +
      • What should we already know?
      • +
      • Which resources do we have to help us learn all this?
      • +
      +
    • +
    • Activity
    • +
    • Questions
    • +
    + + +

    Next Lecture [10]

    + +
      +
    • Data Representation

      + +
        +
      • Representing information as bits
      • +
      +
    • +
    • To get ready for our next lecture:

      + +
        +
      • Optional: Read Chapter 1 of textbook
      • +
      • Not so optional: Read Section 2.1 of Chapter 2
      • +
      • Download the partial lecture notes found under the column Lecture in the table on our course web site
      • +
      +
    • +
    + diff --git a/_site/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro.pdf b/_site/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro.pdf new file mode 100644 index 0000000..03015a0 Binary files /dev/null and b/_site/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro.pdf differ diff --git a/_site/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro_Annotated.html b/_site/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro_Annotated.html new file mode 100644 index 0000000..af38496 --- /dev/null +++ b/_site/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro_Annotated.html @@ -0,0 +1,1214 @@ + + + + + | tait.tech + + + + +
    +
    +

    CMPT 295

    + +

    Unit: Textbook Chapter 2 - Data Representation

    + +

    Lecture 2 - Representing data in memory

    + +

    Data = information = data, instructions (code, programs)

    + +

    CAL Volunteer Note-Taker Position [1]

    + +
      +
    • If you are taking lecture notes in CMPT 295 and your hand writing is you may be interested in applying for the following volunteer note-taker position: +
        +
      • The Centre for Accessible Learning (CAL) is looking for a CMPT 295 notetaker
      • +
      • CAL volunteer lecture note-takers are provided with a $100 credit applied to their student account in acknowledgment of their assistance
      • +
      +
    • +
    • Interested?
    • +
    • Please see the email CAL has sent us
    • +
    • Please feel free to call 778-782-3112 or email calexams@sfu.ca the Centre if +you have any questions
    • +
    + +

    Last Lecture [2]

    +
      +
    • COVID Protocol
    • +
    • What is CMPT 295? +
        +
      • What shall we learn in CMPT 295?
      • +
      • What should we already know?
      • +
      • Which resources do we have to help us learn all this?
      • +
      +
    • +
    • Activity
    • +
    • Questions
    • +
    + +

    Feedback on Lecture 1 Activity [3]

    +
      +
    • Thank you for participating in the Lecture 1 Activity!
    • +
    • Feedback now posted on our course web site
    • +
    • Check it out!
    • +
    + +

    Unit Objectives [4]

    + +

    Chapter 2 of our textbook

    + +
      +
    • Understand how a computer represents (encodes) data in (fixed-size) memory
    • +
    • Become aware of the impact this fixed size has on … +
        +
      • Range of values represented in memory
      • +
      • Results of arithmetic operations
      • +
      +
    • +
    • Become aware of … +
        +
      • How one data type is converted to another
      • +
      • And the impact this conversion has on the values
      • +
      +
    • +
    • Bottom Line: allow software developers to write more reliable code
    • +
    + +

    Today’s Menu [5]

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +6 +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    “Under the hood” Von Neumann architecture [6]

    + +

    Architecture of most computers

    + +

    Its features:

    +
      +
    • CPU, memory, +input and ouput, bus
    • +
    • +

      (circled in red) Data and instructions +(code/programs) both stored in memory

      +
    • +
    • C programs (.c) – How our code and data are represented in memory
    • +
    • Assembly programs (.s) – How a compiler transforms our code into machine executable code in several steps
    • +
    • Object (.o) file an executable – How a compiler optimizes (or not) our code
    • +
    • Computer executes it – How a microprocessor is designed and how it executes our code
    • +
    • CPU, Memory – How memory is designed
    • +
    + +

    “Computer executes it” has a diagram attached.

    + +

    Two I/O devices are shown (represented by a cylindar), one on the left, one one the right. +On the left is “input peripheral”, on the right is “output peripheral”.

    + +

    A red box surrounds the two central nodes (represented by squares), labeled “motherboard”. +It contains two items inside it. +On the left is the “Central Processing Unit (CPU)”, which executes instructions. +On the right is “Memory”, which is “volatile” and stores data and instructions.

    + +

    An arrow points from the far left input peripheral to the CPU. +A bi-directional arrow points between the CPU and memory. +A final arrow points from memory to the output peripheral.

    + +

    How to diagram memory [7]

    +
      +
    • Seen as a linear (contiguous) array of bytes
    • +
    • 1 byte (8 bits) smallest addressable unit of memory +
        +
      • Each byte has a unique address
      • +
      • Byte-addressable memory
      • +
      +
    • +
    • Computer reads a word worth of bits at a time (=> word size)
    • +
    • Questions: +
        +
      1. If word size is 8, how many bytes are read at a time from memory? +Answer: (annotated) 1 byte
      2. +
      3. If a computer can read 4 bytes at a time, its +word size is (annotated) 32 bits.
      4. +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size−1 
     
    0x0008 
    0x0007 
    0x0006 
    0x0005 
    0x0004 
    0x0003 
    0x0002 
    0x0001 
    0x00001 byte
    + +

    Closer look at memory [8]

    + +

    Typically, in a diagram, we represent memory (memory content) as a series of memory “cells” (or bits) in which one of two possible values (‘0’ and ‘1’) is stored

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size−1 
     
    0x0008 
    0x0007 
    0x0006 
    0x0005 
    0x0004 
    0x0003 
    0x0002 
    0x0001 
    0x000001000000
    + +

    0x0000 is labled as “1 memory cell”

    + +

    Compressed view of memory [9]

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size−1 
     
    0x0008 
    0x00077
    0x00066
    0x00055
    0x00044
    0x00033
    0x00022
    0x00011
    0x00000
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]       
    size−8        
            
    0x0018        
    0x0010 (annotation: why is this not `0x0016)        
    0x0008        
    0x000001234567
    + +

    Each cell, no matter which diagram is used, represents 1 byte, or 8 bits. +0x0000 to 0x0008 is eight bytes, or 64 bits.

    + +

    Why can only two possible values be stored in a memory “cell”?

    + +
      +
    • As electronic machines, computers use two voltage levels +
        +
      • Transmitted on noisy wires -> value of two voltage levels vary over a range
      • +
      • These ranges are abstracted using “0” and “1”
      • +
      +
    • +
    + +

    A diagram shows two horizontal green bars. +Bar one has a minimum of 0.0V, and a maximum of 0.2V. “0” +Bar two has a minimum of 0.9V, and a maximum of 1.1V. “1” +A wiggly line jiggles around somewhat randomly within the confines of the lower “0” bar–moving to the right with time, it moves up, temporarily traversing the whitespace between the two bars before settling back down in its jiggly pattern within the “1” bar. +It then reverses this step, through the whitespace back to the “0” bar where it started.

    + +
      +
    • Back to the question Why can only two possible values be stored in a +memory “cell”? +
        +
      • Because computers manipulate two-valued information
      • +
      +
    • +
    + +

    A bit of history [11]

    + +

    ENIAC: Electronic Numerical Integrator And Calculator

    +
      +
    • U. Penn by Eckert + Mauchly (1946)
    • +
    • Data: 20 × 10-digit regs + ~18,000 vacuum tubes
    • +
    • To code: manually set switches and plugged cables +
        +
      • Debugging was manual
      • +
      • No method to save program for later use
      • +
      • Separated code from the data
      • +
      +
    • +
    + +

    Source: https://en.wikipedia.org/wiki/ENIAC#/media/File:ENIAC_Penn1.jpg

    + +

    Review [12] – Back to our bits – How to represent series of bits

    +
      +
    • From binary numeral system
    • +
    • Base: 2
    • +
    • Bit values: 0 and 1
    • +
    • Possible bit patterns in a byte: 000000002 to 111111112
    • +
    • Drawback of manipulating binary numbers? +
        +
      • What number is this? +
          +
        • 1001100 11001001 01000101 010010002
        • +
        +
      • +
      • Lengthy to write -> not very compact
      • +
      • Difficult to read
      • +
      +
    • +
    + +

    Annotation: “Error prone!” encompasses “lengthy to write” and “difficult to read”.

    + +

    Review – A solution: hexadecimal numbers

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    DecimalBinaryHexidecimal
    000000
    100011
    200102
    30011 (circled in annotation)3
    401004
    50101 (circled in annotation)5
    601106
    701117
    810008
    910019
    101010A
    111011B
    121100C
    131101 (circled in annotation)D
    141110E
    151111 (circled in annotation)F
    + +
      +
    • Base: 16
    • +
    • Values: 0, 1, 2, …, 9, A, B, C, D, E, F
    • +
    • Possible patterns in a byte: 0016 to FF16
    • +
    • Conversion binary -> hex (hex is in annotation, binary numbers grouped by 4 by green lines) +
        +
      • e.g.: 0100(0x4) 1100(0xC) 1100(0xC) 1001(0x9) 0100(0x4) 0101(0x5) 0100(0x4) 1000(0xB)
      • +
      • annotation: conversion algorithm?
      • +
      +
    • +
    • Conversion hex -> binary (binary in annotation, an arrow points off from each hex character to the binary sequence representing it) +
        +
      • e.g.: 3(0011) D(1101) 5(0101) F(1111) (in C: 0x3D5F)
      • +
      • annotation: conversion algorithm?
      • +
      +
    • +
    + +

    What could these 32 bits represent? – What kind of information could they encode?

    + +

    011000100110100101110100011100112

    + +

    Answer:

    +
      +
    • Integer
    • +
    • string of cleartexts
    • +
    • colour
    • +
    + +

    What kind of information (data) do series of bits represent?

    + +

    Encoding Scheme

    + +

    Bit pattern: +01100010 01101001 +01110100 011100112

    + +

    An arrow pointing to a box labeled “encoding schemes”:

    + +
      +
    • ASCII character
    • +
    • Unsigned integer
    • +
    • Two’s complement (signed) integer
    • +
    • Floating point
    • +
    • Memory Address
    • +
    • Assembly language
    • +
    • RGB
    • +
    • MP3
    • +
    • +
    + +

    An arrow pointing to the following list:

    + +
      +
    • Letters and symbols
    • +
    • Positive numbers
    • +
    • Negative numbers
    • +
    • Real numbers
    • +
    • C pointers
    • +
    • Machine-level instructions
    • +
    • Colour
    • +
    • Audio/Sound
    • +
    • +
    + +

    Definition: An encoding scheme is an interpretation (representation) of a series of bits

    + +

    Bottom line: Which encoding scheme is used to interpret a series of bits depends on the application currently executing (the “context”) not the computer

    + +

    Endian – Order of bytes in memory

    +
      +
    • It is straight forward to store a byte in memory +
        +
      • All we need is the byte (series of bits) and a memory address
      • +
      • For example, let’s store byte 011100112 at address 0x0000
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size−1 
     
    0x0003 
    0x0002 
    0x0001 
    0x0000011100112
    + +

    Endian – Order of bytes in memory

    + +

    Question: But how do we store several bytes in memory?

    +
      +
    • For example, let’s store these 4 bytes starting at address 0x0000
    • +
    + +

    01000010 01101001 01110100 011100112

    + + + +

    Way 1: Little endian +Address|M[]|Hex (filled out as annotation) +size-1|| +…|| +0x0003|01000010|42 +0x0002|01101001|69 +0x0001|01110100|74 +0x0000|01110011|73

    + +

    Way 2: Big endian

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]Hex
    size−1  
      
    0x00030111001173
    0x00020111010074
    0x00010110100169
    0x00000100001042
    + +

    Compressed view of memory:

    + +

    Little-endian:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Address       
    size-8       
           
    0x0008       
    0x000073746942   
    + +

    Big-endian:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Address       
           
    0x0008       
    0x000042697473   
    + +

    Review Bit – Bit Manipulation - Boolean algebra

    + +

    No matter what a series of bits represent, they can be manipulated using bit-level operations:

    +
      +
    • Boolean algebra
    • +
    • +

      Shifting

      +
    • +
    • Developed by George Boole in 19th Century +
        +
      • Algebraic representation of logic +
          +
        • Encode “True” as 1 and “False” as 0
        • +
        +
      • +
      +
    • +
    + +

    AND -> A&B = 1 when both A=1 and B=1

    + + + + + + + + + + + + + + + + + + + +
    &01
    000
    101
    + +

    NOT -> ~A = 1 when A=0

    + + + + + + + + + + + + + + + + +
    ~ 
    01
    10
    + + + + + + + + +
    OR -> AB = 1 when either A=1 or B=1
    + + + + + + + + + + + + + + + + + + + +
    |01
    001
    111
    + +

    XOR (Exclusive-Or) -> A^B = 1 when either A=1 or B=1, but not both

    + + + + + + + + + + + + + + + + + + + +
    ^01
    001
    110
    + +

    Interesting fact about Boolean algebra and digital logic

    + +
      +
    • Claude Shannon – 1937 master’s thesis
    • +
    • Made connection between Boolean algebra and digital logic +
        +
      • Boolean algebra could be applied to design and analysis of digital +systems (digital circuits)
      • +
      +
    • +
    • Example:
    • +
    + +

    Diagram of an AND gate.

    + +

    Two lines go into a black box. +One is 1/high, one is 0/low. +An output comes out the other side: 0/low.

    + +

    Review (annotation: HW2)

    + +

    Let’s try some Boolean algebra!

    + +
      +
    • Operations applied bitwise -> to each bit
    • +
    • Spot the error(s):
    • +
    + +
    +  01101001
    +& 01010101
    += 01000001
    +
    + +
    +  01101001
    +| 01010101
    += 01111101
    +
    + +
    +  01101001
    +^ 01010101
    += 00111100
    +
    +(notation on 2nd last bit of equals: error!)
    +
    + +
    +~ 01010101
    += 10101010
    +
    + +

    Useful bit manipulations

    +
      +
    • Using a binary mask (or bit mask) as an operand +
        +
      1. AND: Extracts particular bit(s) so we can test whether they are set. Example:
      2. +
      +
    • +
    +
    +  10110011 <- some value x
    +& 00000001 <- binary mask
    +  00000001
    +
    +

    The result tells us that the least significant bit (LSb) is set.

    +
      +
    1. XOR: Toggle specific bits + Example: The result tells us that the least significant bit (LSb) of x is set
    2. +
    +
    +  10110011 <- some value x
    +^ 00011100 <- binary mask
    += 10101111 We get a toggled version of the 3 original bits (of x) that
    +
    +

    (red square around bits 4-6): “We get a toggled version of the three original bits (of x) that corresponds to the 3 set bits of the binary mask.”

    +
      +
    • Using two operands +
        +
      1. OR: Merge all set bits of operands +
          +
        • Example:
        • +
        +
      2. +
      +
    • +
    +
    +  10110011 <- some value x
    +| 00011100 <- some value y
    += 10111111
    +
    +

    The result contains all the set bits of x and y.

    + +

    Bit Manipulation - Shift operations

    + +
      +
    • LSb: least significant bit is the rightmost bit of a series of bits (or bit vector)
    • +
    • +

      MSb: most significant bit is the leftmost bit of a series of bits (or bit vector)

      +
    • +
    • Left Shift: x « y +
        +
      • Shift bit vector (a series of bits) x left y positions. +
          +
        • Effect: +
            +
          • Throw away y most significant bits (MSb) of x on left
          • +
          • Fill x with y 0’s on right
          • +
          +
        • +
        +
      • +
      +
    • +
    • Right Shift: x » y +
        +
      • Shift bit vector x right y positions
      • +
      • Effect: +
          +
        • Throw away y least significant bits (LSb) of x on right
        • +
        +
      • +
      • Logical shift: Fill x with y 0’s on left
      • +
      • Arithmetic shift: Fill x with y copies of x‘s sign bit on left
      • +
      • Sign bit: most significant bit (MSb) of x (before shifting occurred)
      • +
      +
    • +
    + +

    Bit Manipulation - Shift operations – Let’s try! (HW3)

    + +
      +
    • Left Shift: 10111001 « 4 = 10010000
    • +
    • +

      Left Shift: 10111001 « 2 = 11100100

      +
    • +
    • Right Shift (logical): 00111001 » 4 = 00000011
    • +
    • Right Shift (arithmatic): 10111001 » 4 = 11111011
    • +
    • Right Shift (logical/arithmatic): 10111001 » 2 = 00101110/11101110
    • +
    + +

    Summary

    + +
      +
    • Von Neumann architecture +
        +
      • Architecture of most computers
      • +
      • Its components: CPU, memory, input and ouput, bus
      • +
      • One of its characteristics: Data and code (programs) both stored in memory
      • +
      +
    • +
    • A look at memory: defined byte-addressable memory, diagram of (compressed) memory +
        +
      • Word size (w): size of a series of bits (or bit vector) we manipulate, also size of machine words (see Section 2.1.2)
      • +
      +
    • +
    • A look at bits in memory +
        +
      • Why binary numeral system (0 and 1 -> two values) is used to represent information in memory
      • +
      • Algorithm for converting binary to hexadecimal (hex) +
          +
        1. Partition bit vector into groups of 4 bits, starting from right, i.e., least significant byte (LSB) +
            +
          • If most significant “byte” (MSB) does not have 8 bits, pad it: add 0’s to its left
          • +
          +
        2. +
        3. Translate each group of 4 bits into its hex value
        4. +
        +
      • +
      • What do bits represent? Encoding scheme gives meaning to bits
      • +
      • Order of bytes in memory: little endian versus big endian
      • +
      +
    • +
    • Bit manipulation – regardless of what bit vectors represent +
        +
      • + + + + + + + +
        Boolean algebra: bitwise operations => AND (&), OR (), XOR (^), NOT (~)
        +
      • +
      • Shift operations: left shift, right logical shift and right arithmetic shift +
          +
        • Logical shift: Fill x with y 0’s on left
        • +
        • Arithmetic shift: Fill x with y copies of x‘s sign bit on left
        • +
        • Sign bit: Most significant bit (MSb) before shifting occurred
        • +
        +
      • +
      +
    • +
    + + + + + + + + + + +
    NOTE: C logical operators and C bitwise (bit-level) operators behave differently! Watch out for && versus &, versus, …
    + +

    Next Lecture

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro_Annotated.pdf b/_site/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro_Annotated.pdf new file mode 100644 index 0000000..b6a0d56 Binary files /dev/null and b/_site/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro_Annotated.pdf differ diff --git a/_site/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro_Annotated/index.html b/_site/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro_Annotated/index.html new file mode 100644 index 0000000..42c800a --- /dev/null +++ b/_site/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro_Annotated/index.html @@ -0,0 +1,1216 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    CMPT 295

    + +

    Unit: Textbook Chapter 2 - Data Representation

    + +

    Lecture 2 - Representing data in memory

    + +

    Data = information = data, instructions (code, programs)

    + +

    CAL Volunteer Note-Taker Position [1]

    + +
      +
    • If you are taking lecture notes in CMPT 295 and your hand writing is you may be interested in applying for the following volunteer note-taker position: +
        +
      • The Centre for Accessible Learning (CAL) is looking for a CMPT 295 notetaker
      • +
      • CAL volunteer lecture note-takers are provided with a $100 credit applied to their student account in acknowledgment of their assistance
      • +
      +
    • +
    • Interested?
    • +
    • Please see the email CAL has sent us
    • +
    • Please feel free to call 778-782-3112 or email calexams@sfu.ca the Centre if +you have any questions
    • +
    + +

    Last Lecture [2]

    +
      +
    • COVID Protocol
    • +
    • What is CMPT 295? +
        +
      • What shall we learn in CMPT 295?
      • +
      • What should we already know?
      • +
      • Which resources do we have to help us learn all this?
      • +
      +
    • +
    • Activity
    • +
    • Questions
    • +
    + +

    Feedback on Lecture 1 Activity [3]

    +
      +
    • Thank you for participating in the Lecture 1 Activity!
    • +
    • Feedback now posted on our course web site
    • +
    • Check it out!
    • +
    + +

    Unit Objectives [4]

    + +

    Chapter 2 of our textbook

    + +
      +
    • Understand how a computer represents (encodes) data in (fixed-size) memory
    • +
    • Become aware of the impact this fixed size has on … +
        +
      • Range of values represented in memory
      • +
      • Results of arithmetic operations
      • +
      +
    • +
    • Become aware of … +
        +
      • How one data type is converted to another
      • +
      • And the impact this conversion has on the values
      • +
      +
    • +
    • Bottom Line: allow software developers to write more reliable code
    • +
    + +

    Today’s Menu [5]

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +6 +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    “Under the hood” Von Neumann architecture [6]

    + +

    Architecture of most computers

    + +

    Its features:

    +
      +
    • CPU, memory, +input and ouput, bus
    • +
    • +

      (circled in red) Data and instructions +(code/programs) both stored in memory

      +
    • +
    • C programs (.c) – How our code and data are represented in memory
    • +
    • Assembly programs (.s) – How a compiler transforms our code into machine executable code in several steps
    • +
    • Object (.o) file an executable – How a compiler optimizes (or not) our code
    • +
    • Computer executes it – How a microprocessor is designed and how it executes our code
    • +
    • CPU, Memory – How memory is designed
    • +
    + +

    “Computer executes it” has a diagram attached.

    + +

    Two I/O devices are shown (represented by a cylindar), one on the left, one one the right. +On the left is “input peripheral”, on the right is “output peripheral”.

    + +

    A red box surrounds the two central nodes (represented by squares), labeled “motherboard”. +It contains two items inside it. +On the left is the “Central Processing Unit (CPU)”, which executes instructions. +On the right is “Memory”, which is “volatile” and stores data and instructions.

    + +

    An arrow points from the far left input peripheral to the CPU. +A bi-directional arrow points between the CPU and memory. +A final arrow points from memory to the output peripheral.

    + +

    How to diagram memory [7]

    +
      +
    • Seen as a linear (contiguous) array of bytes
    • +
    • 1 byte (8 bits) smallest addressable unit of memory +
        +
      • Each byte has a unique address
      • +
      • Byte-addressable memory
      • +
      +
    • +
    • Computer reads a word worth of bits at a time (=> word size)
    • +
    • Questions: +
        +
      1. If word size is 8, how many bytes are read at a time from memory? +Answer: (annotated) 1 byte
      2. +
      3. If a computer can read 4 bytes at a time, its +word size is (annotated) 32 bits.
      4. +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size−1 
     
    0x0008 
    0x0007 
    0x0006 
    0x0005 
    0x0004 
    0x0003 
    0x0002 
    0x0001 
    0x00001 byte
    + +

    Closer look at memory [8]

    + +

    Typically, in a diagram, we represent memory (memory content) as a series of memory “cells” (or bits) in which one of two possible values (‘0’ and ‘1’) is stored

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size−1 
     
    0x0008 
    0x0007 
    0x0006 
    0x0005 
    0x0004 
    0x0003 
    0x0002 
    0x0001 
    0x000001000000
    + +

    0x0000 is labled as “1 memory cell”

    + +

    Compressed view of memory [9]

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size−1 
     
    0x0008 
    0x00077
    0x00066
    0x00055
    0x00044
    0x00033
    0x00022
    0x00011
    0x00000
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]       
    size−8        
            
    0x0018        
    0x0010 (annotation: why is this not `0x0016)        
    0x0008        
    0x000001234567
    + +

    Each cell, no matter which diagram is used, represents 1 byte, or 8 bits. +0x0000 to 0x0008 is eight bytes, or 64 bits.

    + +

    Why can only two possible values be stored in a memory “cell”?

    + +
      +
    • As electronic machines, computers use two voltage levels +
        +
      • Transmitted on noisy wires -> value of two voltage levels vary over a range
      • +
      • These ranges are abstracted using “0” and “1”
      • +
      +
    • +
    + +

    A diagram shows two horizontal green bars. +Bar one has a minimum of 0.0V, and a maximum of 0.2V. “0” +Bar two has a minimum of 0.9V, and a maximum of 1.1V. “1” +A wiggly line jiggles around somewhat randomly within the confines of the lower “0” bar–moving to the right with time, it moves up, temporarily traversing the whitespace between the two bars before settling back down in its jiggly pattern within the “1” bar. +It then reverses this step, through the whitespace back to the “0” bar where it started.

    + +
      +
    • Back to the question Why can only two possible values be stored in a +memory “cell”? +
        +
      • Because computers manipulate two-valued information
      • +
      +
    • +
    + +

    A bit of history [11]

    + +

    ENIAC: Electronic Numerical Integrator And Calculator

    +
      +
    • U. Penn by Eckert + Mauchly (1946)
    • +
    • Data: 20 × 10-digit regs + ~18,000 vacuum tubes
    • +
    • To code: manually set switches and plugged cables +
        +
      • Debugging was manual
      • +
      • No method to save program for later use
      • +
      • Separated code from the data
      • +
      +
    • +
    + +

    Source: https://en.wikipedia.org/wiki/ENIAC#/media/File:ENIAC_Penn1.jpg

    + +

    Review [12] – Back to our bits – How to represent series of bits

    +
      +
    • From binary numeral system
    • +
    • Base: 2
    • +
    • Bit values: 0 and 1
    • +
    • Possible bit patterns in a byte: 000000002 to 111111112
    • +
    • Drawback of manipulating binary numbers? +
        +
      • What number is this? +
          +
        • 1001100 11001001 01000101 010010002
        • +
        +
      • +
      • Lengthy to write -> not very compact
      • +
      • Difficult to read
      • +
      +
    • +
    + +

    Annotation: “Error prone!” encompasses “lengthy to write” and “difficult to read”.

    + +

    Review – A solution: hexadecimal numbers

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    DecimalBinaryHexidecimal
    000000
    100011
    200102
    30011 (circled in annotation)3
    401004
    50101 (circled in annotation)5
    601106
    701117
    810008
    910019
    101010A
    111011B
    121100C
    131101 (circled in annotation)D
    141110E
    151111 (circled in annotation)F
    + +
      +
    • Base: 16
    • +
    • Values: 0, 1, 2, …, 9, A, B, C, D, E, F
    • +
    • Possible patterns in a byte: 0016 to FF16
    • +
    • Conversion binary -> hex (hex is in annotation, binary numbers grouped by 4 by green lines) +
        +
      • e.g.: 0100(0x4) 1100(0xC) 1100(0xC) 1001(0x9) 0100(0x4) 0101(0x5) 0100(0x4) 1000(0xB)
      • +
      • annotation: conversion algorithm?
      • +
      +
    • +
    • Conversion hex -> binary (binary in annotation, an arrow points off from each hex character to the binary sequence representing it) +
        +
      • e.g.: 3(0011) D(1101) 5(0101) F(1111) (in C: 0x3D5F)
      • +
      • annotation: conversion algorithm?
      • +
      +
    • +
    + +

    What could these 32 bits represent? – What kind of information could they encode?

    + +

    011000100110100101110100011100112

    + +

    Answer:

    +
      +
    • Integer
    • +
    • string of cleartexts
    • +
    • colour
    • +
    + +

    What kind of information (data) do series of bits represent?

    + +

    Encoding Scheme

    + +

    Bit pattern: +01100010 01101001 +01110100 011100112

    + +

    An arrow pointing to a box labeled “encoding schemes”:

    + +
      +
    • ASCII character
    • +
    • Unsigned integer
    • +
    • Two’s complement (signed) integer
    • +
    • Floating point
    • +
    • Memory Address
    • +
    • Assembly language
    • +
    • RGB
    • +
    • MP3
    • +
    • +
    + +

    An arrow pointing to the following list:

    + +
      +
    • Letters and symbols
    • +
    • Positive numbers
    • +
    • Negative numbers
    • +
    • Real numbers
    • +
    • C pointers
    • +
    • Machine-level instructions
    • +
    • Colour
    • +
    • Audio/Sound
    • +
    • +
    + +

    Definition: An encoding scheme is an interpretation (representation) of a series of bits

    + +

    Bottom line: Which encoding scheme is used to interpret a series of bits depends on the application currently executing (the “context”) not the computer

    + +

    Endian – Order of bytes in memory

    +
      +
    • It is straight forward to store a byte in memory +
        +
      • All we need is the byte (series of bits) and a memory address
      • +
      • For example, let’s store byte 011100112 at address 0x0000
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size−1 
     
    0x0003 
    0x0002 
    0x0001 
    0x0000011100112
    + +

    Endian – Order of bytes in memory

    + +

    Question: But how do we store several bytes in memory?

    +
      +
    • For example, let’s store these 4 bytes starting at address 0x0000
    • +
    + +

    01000010 01101001 01110100 011100112

    + + + +

    Way 1: Little endian +Address|M[]|Hex (filled out as annotation) +size-1|| +…|| +0x0003|01000010|42 +0x0002|01101001|69 +0x0001|01110100|74 +0x0000|01110011|73

    + +

    Way 2: Big endian

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]Hex
    size−1  
      
    0x00030111001173
    0x00020111010074
    0x00010110100169
    0x00000100001042
    + +

    Compressed view of memory:

    + +

    Little-endian:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Address       
    size-8       
           
    0x0008       
    0x000073746942   
    + +

    Big-endian:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Address       
           
    0x0008       
    0x000042697473   
    + +

    Review Bit – Bit Manipulation - Boolean algebra

    + +

    No matter what a series of bits represent, they can be manipulated using bit-level operations:

    +
      +
    • Boolean algebra
    • +
    • +

      Shifting

      +
    • +
    • Developed by George Boole in 19th Century +
        +
      • Algebraic representation of logic +
          +
        • Encode “True” as 1 and “False” as 0
        • +
        +
      • +
      +
    • +
    + +

    AND -> A&B = 1 when both A=1 and B=1

    + + + + + + + + + + + + + + + + + + + +
    &01
    000
    101
    + +

    NOT -> ~A = 1 when A=0

    + + + + + + + + + + + + + + + + +
    ~ 
    01
    10
    + + + + + + + + +
    OR -> AB = 1 when either A=1 or B=1
    + + + + + + + + + + + + + + + + + + + +
    |01
    001
    111
    + +

    XOR (Exclusive-Or) -> A^B = 1 when either A=1 or B=1, but not both

    + + + + + + + + + + + + + + + + + + + +
    ^01
    001
    110
    + +

    Interesting fact about Boolean algebra and digital logic

    + +
      +
    • Claude Shannon – 1937 master’s thesis
    • +
    • Made connection between Boolean algebra and digital logic +
        +
      • Boolean algebra could be applied to design and analysis of digital +systems (digital circuits)
      • +
      +
    • +
    • Example:
    • +
    + +

    Diagram of an AND gate.

    + +

    Two lines go into a black box. +One is 1/high, one is 0/low. +An output comes out the other side: 0/low.

    + +

    Review (annotation: HW2)

    + +

    Let’s try some Boolean algebra!

    + +
      +
    • Operations applied bitwise -> to each bit
    • +
    • Spot the error(s):
    • +
    + +
    +  01101001
    +& 01010101
    += 01000001
    +
    + +
    +  01101001
    +| 01010101
    += 01111101
    +
    + +
    +  01101001
    +^ 01010101
    += 00111100
    +
    +(notation on 2nd last bit of equals: error!)
    +
    + +
    +~ 01010101
    += 10101010
    +
    + +

    Useful bit manipulations

    +
      +
    • Using a binary mask (or bit mask) as an operand +
        +
      1. AND: Extracts particular bit(s) so we can test whether they are set. Example:
      2. +
      +
    • +
    +
    +  10110011 <- some value x
    +& 00000001 <- binary mask
    +  00000001
    +
    +

    The result tells us that the least significant bit (LSb) is set.

    +
      +
    1. XOR: Toggle specific bits + Example: The result tells us that the least significant bit (LSb) of x is set
    2. +
    +
    +  10110011 <- some value x
    +^ 00011100 <- binary mask
    += 10101111 We get a toggled version of the 3 original bits (of x) that
    +
    +

    (red square around bits 4-6): “We get a toggled version of the three original bits (of x) that corresponds to the 3 set bits of the binary mask.”

    +
      +
    • Using two operands +
        +
      1. OR: Merge all set bits of operands +
          +
        • Example:
        • +
        +
      2. +
      +
    • +
    +
    +  10110011 <- some value x
    +| 00011100 <- some value y
    += 10111111
    +
    +

    The result contains all the set bits of x and y.

    + +

    Bit Manipulation - Shift operations

    + +
      +
    • LSb: least significant bit is the rightmost bit of a series of bits (or bit vector)
    • +
    • +

      MSb: most significant bit is the leftmost bit of a series of bits (or bit vector)

      +
    • +
    • Left Shift: x « y +
        +
      • Shift bit vector (a series of bits) x left y positions. +
          +
        • Effect: +
            +
          • Throw away y most significant bits (MSb) of x on left
          • +
          • Fill x with y 0’s on right
          • +
          +
        • +
        +
      • +
      +
    • +
    • Right Shift: x » y +
        +
      • Shift bit vector x right y positions
      • +
      • Effect: +
          +
        • Throw away y least significant bits (LSb) of x on right
        • +
        +
      • +
      • Logical shift: Fill x with y 0’s on left
      • +
      • Arithmetic shift: Fill x with y copies of x‘s sign bit on left
      • +
      • Sign bit: most significant bit (MSb) of x (before shifting occurred)
      • +
      +
    • +
    + +

    Bit Manipulation - Shift operations – Let’s try! (HW3)

    + +
      +
    • Left Shift: 10111001 « 4 = 10010000
    • +
    • +

      Left Shift: 10111001 « 2 = 11100100

      +
    • +
    • Right Shift (logical): 00111001 » 4 = 00000011
    • +
    • Right Shift (arithmatic): 10111001 » 4 = 11111011
    • +
    • Right Shift (logical/arithmatic): 10111001 » 2 = 00101110/11101110
    • +
    + +

    Summary

    + +
      +
    • Von Neumann architecture +
        +
      • Architecture of most computers
      • +
      • Its components: CPU, memory, input and ouput, bus
      • +
      • One of its characteristics: Data and code (programs) both stored in memory
      • +
      +
    • +
    • A look at memory: defined byte-addressable memory, diagram of (compressed) memory +
        +
      • Word size (w): size of a series of bits (or bit vector) we manipulate, also size of machine words (see Section 2.1.2)
      • +
      +
    • +
    • A look at bits in memory +
        +
      • Why binary numeral system (0 and 1 -> two values) is used to represent information in memory
      • +
      • Algorithm for converting binary to hexadecimal (hex) +
          +
        1. Partition bit vector into groups of 4 bits, starting from right, i.e., least significant byte (LSB) +
            +
          • If most significant “byte” (MSB) does not have 8 bits, pad it: add 0’s to its left
          • +
          +
        2. +
        3. Translate each group of 4 bits into its hex value
        4. +
        +
      • +
      • What do bits represent? Encoding scheme gives meaning to bits
      • +
      • Order of bytes in memory: little endian versus big endian
      • +
      +
    • +
    • Bit manipulation – regardless of what bit vectors represent +
        +
      • + + + + + + + +
        Boolean algebra: bitwise operations => AND (&), OR (), XOR (^), NOT (~)
        +
      • +
      • Shift operations: left shift, right logical shift and right arithmetic shift +
          +
        • Logical shift: Fill x with y 0’s on left
        • +
        • Arithmetic shift: Fill x with y copies of x‘s sign bit on left
        • +
        • Sign bit: Most significant bit (MSb) before shifting occurred
        • +
        +
      • +
      +
    • +
    + + + + + + + + + + +
    NOTE: C logical operators and C bitwise (bit-level) operators behave differently! Watch out for && versus &, versus, …
    + +

    Next Lecture

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/03/03.html b/_site/melody/cmpt-295/03/03.html new file mode 100644 index 0000000..5c3a0fa --- /dev/null +++ b/_site/melody/cmpt-295/03/03.html @@ -0,0 +1,857 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295

    + +

    Unit - Data Representation

    + +

    Lecture 3 – Representing integral numbers in memory - unsigned and signed

    + +

    Last Lecture

    +
      +
    • Von Neumann architecture +
        +
      • Architecture of most computers
      • +
      • Its components: CPU, memory, input and ouput, bus
      • +
      • One of its characteristics: Data and code (programs) both stored in memory
      • +
      +
    • +
    • A look at memory: defined byte-addressable memory, diagram of (compressed) memory +
        +
      • Word size (w): size of a series of bits (or bit vector) we manipulate, also size of machine words (see Section 2.1.2)
      • +
      +
    • +
    • A look at bits in memory +
        +
      • Why binary numeral system (0 and 1 -> two values) is used to represent information in memory
      • +
      • Algorithm for converting binary to hexadecimal (hex) +
          +
        1. Partition bit vector into groups of 4 bits, starting from right, i.e., least significant byte (LSB) +
            +
          • If most significant “byte” (MSB) does not have 8 bits, pad it: add 0’s to its left
          • +
          +
        2. +
        3. Translate each group of 4 bits into its hex value
        4. +
        +
      • +
      • What do bits represent? Encoding scheme gives meaning to bits
      • +
      • Order of bytes in memory: little endian versus big endian
      • +
      +
    • +
    • Bit manipulation – regardless of what bit vectors represent +
        +
      • Boolean algebra: bitwise operations => AND (&), OR (|), XOR (^), NOT (~)
      • +
      • Shift operations: left shift, right logical shift and right arithmetic shift +
          +
        • Logical shift: Fill x with y 0’s on left
        • +
        • Arithmetic shift: Fill x with y copies of x‘s sign bit on left
        • +
        • Sign bit: Most significant bit (MSb) before shifting occurred
        • +
        +
      • +
      +
    • +
    + +

    NOTE: C logical operators and C bitwise (bit-level) operators behave differently! Watch out for && versus &, || versus |, …

    + +

    Today’s Menu

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    Warm up exercise!

    + +

    As a warm up exercise, fill in the blanks!

    + +
      +
    • If the context is C (on our target machine) +
        +
      • char => _____ bits/ _____ byte
      • +
      • short => _____ bits/ _____ bytes
      • +
      • int => _____ bits/ _____ bytes
      • +
      • long => _____ bits/ _____ bytes
      • +
      • float => _____ bits/ _____ bytes
      • +
      • double => _____ bits/ _____ bytes
      • +
      • pointer (e.g. char *) => _____ bits/ _____ bytes
      • +
      +
    • +
    + +

    Unsigned integral numbers

    + +

    Remember:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-1 
     
    0x000301000010
    0x000201101001
    0x000101110100
    0x000001110011
    + +
      +
    • What if the byte at M[0x0002] represented an unsigned integral number, what would be its value? +x = a series of bits = bit vector +w = width of bit vector
    • +
    • + X=011010012,w=8X = 01101001_{2}, w=8 +
    • +
    • Let’s apply the encoding scheme:
    • +
    + +

    B2U(X)=i=0w1Xi×2i +\it \text{B2U}(X) = \sum_{i=0}^{w-1} X_{i} \times 2^i +

    + +

    Example: 0×27+1×26+1×25+0×24+1×23+0×22+1×20=0 \times 2^7 + 1 \times 2^6 + 1 \times 2^5 + 0 \times 2^4 + 1 \times 2^3 + 0 \times 2^2 + 1 \times 2^0 =

    + +
      +
    • For w = 8, range of possible unsigned values: [ blank ]
    • +
    • For any w, range of possible unsigned values: [ blank ]
    • +
    • Conclusion: w bits can only represent a fixed # of possible values, but these w bits represent these values exactly
    • +
    + +

    B2U(X) Conversion (Encoding scheme)

    +
      +
    • Positional notation: expand and sum all terms
    • +
    + +

    Decimal:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    did_{i}10i10^{i}
    di1d_{i-1}10i110_{i-1}
    d2d_{2}100
    d1d_{1}10
    d0d_{0}1
    + +

    Binary:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    bib_{i}2i2^{i}
    bi1b_{i-1}2i12^{i-1}
    b2b_{2}4
    b1b_{1}2
    b0b_{0}1
    + +

    Remember: +B2U(X)=i=0w1Xi×2i +\it \text{B2U}(X) = \sum_{i=0}^{w-1} X_{i} \times 2^i +

    + +

    Example: 24610=2×102+4×101+6×100246_{10} = 2 \times 10^{2} + 4 \times 10^{1} + 6 \times 10^{0}

    + +

    Range of possible values?

    + +
      +
    • If the context is C (on our target machine) +
        +
      • unsigned char?
      • +
      • unsigned short?
      • +
      • unsigned int?
      • +
      • unsigned long?
      • +
      +
    • +
    + +

    Examples of “Show your work”

    + +

    U2B(X) Conversion (into 8-bit binary # => w = 8)

    + +

    Method 1 - Using subtraction: subtracting decreasing power of 2 until reach 0:

    + +

    Starting number: 246

    + +
      +
    • 246128=118246 – 128 = 118 -> 128=1×27128 = 1 \times 2^{7}
    • +
    • 11864=54118 – 64 = 54 -> 64=1×2664 = 1 \times 2^{6}
    • +
    • 5432=2254 – 32 = 22 -> 32=1×2532 = 1 \times 2^{5}
    • +
    • 2216=622 – 16 = 6 -> 16=1×2416 = 1 \times 2^{4}
    • +
    • 68=nop!6 – 8 = \text{nop!} -> 8=0×238 = 0 \times 2^{3}
    • +
    • 64=26 – 4 = 2 -> 4=1×224 = 1 \times 2^{2}
    • +
    • 22=02 – 2 = 0 -> 2=1×212 = 1 \times 2^{1}
    • +
    • 01=nop!0 – 1 = \text{nop!} -> 1=0×201 = 0 \times 2^{0}
    • +
    • + 24610=111101102246_{10} = 11110110_{2} +
    • +
    + +

    Method 2 - Using division: dividing by 2 until reach 0

    + +

    Start with 246

    + +
      +
    • + 246÷2=123,R=0246 \div 2 = 123,R=0 +
    • +
    • + 123÷2=61,R=1123 \div 2 = 61,R=1 +
    • +
    • + 61÷2=30,R=161 \div 2 = 30,R=1 +
    • +
    • + 30÷2=15,R=030 \div 2 = 15,R=0 +
    • +
    • + 15÷2=7,R=115 \div 2 = 7,R=1 +
    • +
    • + 7÷2=3,R=17 \div 2 = 3,R=1 +
    • +
    • + 3÷2=1,R=13 \div 2 = 1,R=1 +
    • +
    • + 1÷2=0,R=11 \div 2 = 0,R=1 +
    • +
    • + 24610=111101102246_{10} = 11110110_{2} +
    • +
    + +

    U2B(X) Conversion – A few tricks

    +
      +
    • Decimal -> binary +
        +
      • Trick: When decimal number is 2n, then its binary representation is 1 followed by n zero’s
      • +
      • Let’s try: if X = 32 => X = 25, then n = 5 => 100002 (w = 5) What if w = 8? Check: 1 x 24 = 32
      • +
      +
    • +
    • Decimal -> hex +
        +
      • Trick: When decimal number is 2n, then its hexadecimal representation is 2i followed by j zero’s, where n = i + 4j and 0 <= i <=3
      • +
      • Let try: if X = 8192 => X = 213, then n = 13 and 13 = i + 4j => 1 + 4 x 3 +=> 0x2000. Convert 0x2000 into a binary number: Check: 2 x 163 = 2 x 4096 = 8192
      • +
      +
    • +
    + +

    Signed integral numbers

    + +

    Remember:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-1 
     
    0x000301000010
    0x000201101001
    0x000101110100
    0x000001110011
    + +
      +
    • What if the byte at M[0x0001] represented a signed integral number, what would be its value?
    • +
    • X = 111101002 w = 8
    • +
    • T => Two’s Complement, w => width of the bit vector (annotation: first part of equaltion [everything before the plus sign] is the “sign bit”)
    • +
    • Let’s apply the encoding scheme:
    • +
    + +

    B2T(X)=xw1×2w1+i=0w2xi×2i +\it {B2T}(X) = -x_{w-1} \times 2^{w-1} + \sum_{i=0}^{w-2} x_{i} \times 2^{i} +

    + +

    Example: 1×27+1×26+1×25+1×24+0×23+1×22+0×21+0×20=?-1 \times 2^{7} + 1 \times 2^{6} + 1 \times 2^{5} + 1 \times 2^{4} + 0 \times 2^{3} + 1 \times 2^{2} + 0 \times 2^{1} + 0 \times 2^{0} = ?

    + +
      +
    • What would be the bit pattern of the … +
        +
      • Most negative value:
      • +
      • Most positive value:
      • +
      +
    • +
    • For w = 8, range of possible signed values: [ blank ]
    • +
    • For any w, range of possible signed values: [ blank ]
    • +
    • Conclusion: same as for unsigned integral numbers
    • +
    + +

    Examples of “Show your work”

    + +

    T2B(X) Conversion -> Two’s Complement

    + +

    Annotation: w = 8

    + +

    Method 1: If X < 0, (~(U2B(|X|)))+1

    + +

    If X = -14 (and 8 bit binary #s)

    + +
      +
    1. + X=>14=\lvert X\rvert => \lvert -14 \vert = +
    2. +
    3. U2B(14)\text{U2B}(14) =>
    4. +
    5. (first symbol is a tilde) (000011102)\sim(00001110_{2}) =>
    6. +
    7. (111100012)+1(11110001_{2})+1 =>
    8. +
    + +

    Binary addition:

    +
    +  11110001
    ++ 00000001
    += ????????
    +
    + +

    Method 2: If X = -14 (and 8 bit binary #s)

    + +
      +
    1. + X+2w=>14+X + 2^{w} => -14 + +
    2. +
    3. U2B(242)\text{U2B}(242) =>
    4. +
    + +

    Using subtraction:

    + +
      +
    1. 242128=114242 - 128 = 114 -> 1×271 \times 2^{7}
    2. +
    3. 11464=50114 - 64 = 50 -> 1×261 \times 2^{6}
    4. +
    5. 5032=1850 – 32 = 18 -> 1×251 \times 2^{5}
    6. +
    7. 1816=218 – 16 = 2 -> 1×241 \times 2^{4}
    8. +
    9. 28=nop!2 – 8 = \text{nop!} -> 0×230 \times 2^{3}
    10. +
    11. 24=nop!2 – 4 = \text{nop!} -> 0×220 \times 2^{2}
    12. +
    13. 22=02 – 2 = 0 -> 1×211 \times 2^{1}
    14. +
    15. 01=nop!0 – 1 = \text{nop!} -> 0×200 \times 2^{0}
    16. +
    + +

    Properties of unsigned & signed conversions

    + +

    Annotation: w = 4

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    XB2U(X)B2T(X)
    000000
    000111
    001022
    001133
    010044
    010155
    011066
    011177
    10008-8
    10019-7
    101010-6
    101111-5
    110012-4
    110113-3
    111014-2
    111115-1
    + +
      +
    • Equivalence +
        +
      • Both encoding schemes (B2U and B2T ) produce the same bit patterns for nonnegative values
      • +
      +
    • +
    • Uniqueness +
        +
      • Every bit pattern produced by these encoding schemes (B2U and B2T) represents a unique (and exact) integer value
      • +
      • Each representable integer has unique bit pattern
      • +
      +
    • +
    + +

    Converting between signed & unsigned of same size (same data type)

    + +
      +
    • Unsigned +
        +
      • w=8
      • +
      • if unsigned ux = 12910
      • +
      • U2T(X) = B2T(U2B(X))
      • +
      • then x = ???
      • +
      • Maintain Same Bit Pattern
      • +
      +
    • +
    • Signed (Two’s Complement) +
        +
      • w=4
      • +
      • if signed (2’s C) x=510x = -5_{10}
      • +
      • T2U(X) = B2U(T2B(X))
      • +
      • then unsigned ux = ???
      • +
      • Maintain Same Bit Pattern
      • +
      +
    • +
    • Conclusion - Converting between unsigned and signed numbers: +Both have same bit pattern, however, this bit pattern may be interpreted differently, i.e., producing a different value
    • +
    + +

    Converting signed to unsigned (and back) with w=4w = 4

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SignedBitsUnsignedNote
    000000All rows from 0-7 inclusive can be converted from signed to unsigned with T2U(X), and unsigned to signed with U2T(X).
    100011 
    200102 
    300113 
    401004 
    501015 
    601106 
    701117 
    -810008All rows from here to 15 inclusive can be converted to the other like so: T2U(signed + 16/242^{4}) -> unsigned, U2T(unsigned - 16/242^{4}) -> signed.
    -710019 
    -6101010 
    -5101111 
    -4110012 
    -3110113 
    -2111014 
    -1111115 
    + +

    Visualizing the relationship between signed & unsigned

    +

    If w=4,24=16w = 4, 2^{4} = 16

    + +
      +
    • Signed (2’s Complement) Range: TMin to TMax (0 is the center)
    • +
    • Unsigned range: 0 to UMax (TMax is the center)
    • +
    + +

    Sign extension

    +
      +
    • Converting unsigned (or signed) of different sizes (different data types) +
        +
      1. Small data type -> larger +
          +
        • Sign extension +
            +
          • Unsigned: zero extension
          • +
          • Signed: sign bit extension
          • +
          +
        • +
        +
      2. +
      +
    • +
    • Conclusion: Value unchanged
    • +
    • Let’s try: +
        +
      • Going from a data type that has a width of 3 bits (w = 3) to a data type that has a width of 5 bits (w = 5)
      • +
      +
    • +
    • Unsigned: X=3=0112,w=3X = 3 = 011_{2},w=3, X=4=1002,w=3X = 4 = 100_{2},w = 3 +
        +
      • New: X=?=?2,w=5X = ? = ?_{2},w=5, X=?+?2,w=5X = ? + ?_{2},w=5
      • +
      +
    • +
    • Signed: X=3=0112,w=3X = 3 = 011_{2},w=3, X=3=1012,w=3X=-3 = 101_{2},w=3 +
        +
      • New: X=?=?2,w=5X = ? = ?_{2},w=5, x=?=?2,w=5x = ? = ?_{2}, w=5
      • +
      +
    • +
    + +

    Truncation

    + +
      +
    • Converting unsigned (or signed) of different sizes(different data types) +
        +
      1. Large data type -> smaller +
          +
        • Truncation
        • +
        +
      2. +
      +
    • +
    • Conclusion: Value may be altered +
        +
      • A form of overflow
      • +
      +
    • +
    • Let’s try: +
        +
      • Going from a data type that has a width of 5 bits (w = 5) to a data type that has a width of 3 bits (w = 3)
      • +
      +
    • +
    • Unsigned: X=27=110112,w=5X = 27 = 11011_{2},w = 5 +
        +
      • New: X=?=?2,w=3X = ? = ?_{2},w=3
      • +
      +
    • +
    • Signed: X=15=100012,w=3X = -15 = 10001_{2},w=3, X=1=111112,w=5X = -1 = 11111_{2}, w=5 +
        +
      • New: X=?=?2,w=3X = ? = ?_{2}, w=3, X=?=?2,w=3X = ? = ?_{2}, w=3
      • +
      +
    • +
    + +

    Summary

    + +
      +
    • Interpretation of bit pattern B into either unsigned value U or signed value T +
        +
      • B2U(X) and U2B(X) encoding schemes (conversion)
      • +
      • B2T(X) and T2B(X) encoding schemes (conversion) +
          +
        • Signed value expressed as two’s complement => T
        • +
        +
      • +
      +
    • +
    • Conversions from unsigned <-> signed values +
        +
      • U2T(X) and T2U(X) => adding or subtracting 2w2^{w}
      • +
      +
    • +
    • Implication in C: when converting (implicitly via promotion and explicitly via casting): +
        +
      • Sign: +
          +
        • Unsigned <-> signed (of same size) -> Both have same bit pattern, however, this bit pattern may be interpreted differently +
            +
          • Can have unexpected effects -> producing a different value
          • +
          +
        • +
        +
      • +
      • Size: +
          +
        • Small -> large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int) +
            +
          • sign extension: For unsigned -> zeros extension, for signed -> sign bit extension
          • +
          • Both yield expected result –> resulting value unchanged
          • +
          +
        • +
        • Large -> small (e.g., unsigned int to unsigned short) +
            +
          • truncation: Unsigned/signed -> most significant bits are truncated (discarded)
          • +
          • May not yield expected results -> original value may be altered
          • +
          +
        • +
        +
      • +
      • Both (sign and size): 1) size conversion is first done then 2) sign conversion is done
      • +
      +
    • +
    + +

    Next Lecture

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +19 +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/03/03/index.html b/_site/melody/cmpt-295/03/03/index.html new file mode 100644 index 0000000..c2763ee --- /dev/null +++ b/_site/melody/cmpt-295/03/03/index.html @@ -0,0 +1,859 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295

    + +

    Unit - Data Representation

    + +

    Lecture 3 – Representing integral numbers in memory - unsigned and signed

    + +

    Last Lecture

    +
      +
    • Von Neumann architecture +
        +
      • Architecture of most computers
      • +
      • Its components: CPU, memory, input and ouput, bus
      • +
      • One of its characteristics: Data and code (programs) both stored in memory
      • +
      +
    • +
    • A look at memory: defined byte-addressable memory, diagram of (compressed) memory +
        +
      • Word size (w): size of a series of bits (or bit vector) we manipulate, also size of machine words (see Section 2.1.2)
      • +
      +
    • +
    • A look at bits in memory +
        +
      • Why binary numeral system (0 and 1 -> two values) is used to represent information in memory
      • +
      • Algorithm for converting binary to hexadecimal (hex) +
          +
        1. Partition bit vector into groups of 4 bits, starting from right, i.e., least significant byte (LSB) +
            +
          • If most significant “byte” (MSB) does not have 8 bits, pad it: add 0’s to its left
          • +
          +
        2. +
        3. Translate each group of 4 bits into its hex value
        4. +
        +
      • +
      • What do bits represent? Encoding scheme gives meaning to bits
      • +
      • Order of bytes in memory: little endian versus big endian
      • +
      +
    • +
    • Bit manipulation – regardless of what bit vectors represent +
        +
      • Boolean algebra: bitwise operations => AND (&), OR (|), XOR (^), NOT (~)
      • +
      • Shift operations: left shift, right logical shift and right arithmetic shift +
          +
        • Logical shift: Fill x with y 0’s on left
        • +
        • Arithmetic shift: Fill x with y copies of x‘s sign bit on left
        • +
        • Sign bit: Most significant bit (MSb) before shifting occurred
        • +
        +
      • +
      +
    • +
    + +

    NOTE: C logical operators and C bitwise (bit-level) operators behave differently! Watch out for && versus &, || versus |, …

    + +

    Today’s Menu

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    Warm up exercise!

    + +

    As a warm up exercise, fill in the blanks!

    + +
      +
    • If the context is C (on our target machine) +
        +
      • char => _____ bits/ _____ byte
      • +
      • short => _____ bits/ _____ bytes
      • +
      • int => _____ bits/ _____ bytes
      • +
      • long => _____ bits/ _____ bytes
      • +
      • float => _____ bits/ _____ bytes
      • +
      • double => _____ bits/ _____ bytes
      • +
      • pointer (e.g. char *) => _____ bits/ _____ bytes
      • +
      +
    • +
    + +

    Unsigned integral numbers

    + +

    Remember:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-1 
     
    0x000301000010
    0x000201101001
    0x000101110100
    0x000001110011
    + +
      +
    • What if the byte at M[0x0002] represented an unsigned integral number, what would be its value? +x = a series of bits = bit vector +w = width of bit vector
    • +
    • + X=011010012,w=8X = 01101001_{2}, w=8 +
    • +
    • Let’s apply the encoding scheme:
    • +
    + +

    B2U(X)=i=0w1Xi×2i +\it \text{B2U}(X) = \sum_{i=0}^{w-1} X_{i} \times 2^i +

    + +

    Example: 0×27+1×26+1×25+0×24+1×23+0×22+1×20=0 \times 2^7 + 1 \times 2^6 + 1 \times 2^5 + 0 \times 2^4 + 1 \times 2^3 + 0 \times 2^2 + 1 \times 2^0 =

    + +
      +
    • For w = 8, range of possible unsigned values: [ blank ]
    • +
    • For any w, range of possible unsigned values: [ blank ]
    • +
    • Conclusion: w bits can only represent a fixed # of possible values, but these w bits represent these values exactly
    • +
    + +

    B2U(X) Conversion (Encoding scheme)

    +
      +
    • Positional notation: expand and sum all terms
    • +
    + +

    Decimal:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    did_{i}10i10^{i}
    di1d_{i-1}10i110_{i-1}
    d2d_{2}100
    d1d_{1}10
    d0d_{0}1
    + +

    Binary:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    bib_{i}2i2^{i}
    bi1b_{i-1}2i12^{i-1}
    b2b_{2}4
    b1b_{1}2
    b0b_{0}1
    + +

    Remember: +B2U(X)=i=0w1Xi×2i +\it \text{B2U}(X) = \sum_{i=0}^{w-1} X_{i} \times 2^i +

    + +

    Example: 24610=2×102+4×101+6×100246_{10} = 2 \times 10^{2} + 4 \times 10^{1} + 6 \times 10^{0}

    + +

    Range of possible values?

    + +
      +
    • If the context is C (on our target machine) +
        +
      • unsigned char?
      • +
      • unsigned short?
      • +
      • unsigned int?
      • +
      • unsigned long?
      • +
      +
    • +
    + +

    Examples of “Show your work”

    + +

    U2B(X) Conversion (into 8-bit binary # => w = 8)

    + +

    Method 1 - Using subtraction: subtracting decreasing power of 2 until reach 0:

    + +

    Starting number: 246

    + +
      +
    • 246128=118246 – 128 = 118 -> 128=1×27128 = 1 \times 2^{7}
    • +
    • 11864=54118 – 64 = 54 -> 64=1×2664 = 1 \times 2^{6}
    • +
    • 5432=2254 – 32 = 22 -> 32=1×2532 = 1 \times 2^{5}
    • +
    • 2216=622 – 16 = 6 -> 16=1×2416 = 1 \times 2^{4}
    • +
    • 68=nop!6 – 8 = \text{nop!} -> 8=0×238 = 0 \times 2^{3}
    • +
    • 64=26 – 4 = 2 -> 4=1×224 = 1 \times 2^{2}
    • +
    • 22=02 – 2 = 0 -> 2=1×212 = 1 \times 2^{1}
    • +
    • 01=nop!0 – 1 = \text{nop!} -> 1=0×201 = 0 \times 2^{0}
    • +
    • + 24610=111101102246_{10} = 11110110_{2} +
    • +
    + +

    Method 2 - Using division: dividing by 2 until reach 0

    + +

    Start with 246

    + +
      +
    • + 246÷2=123,R=0246 \div 2 = 123,R=0 +
    • +
    • + 123÷2=61,R=1123 \div 2 = 61,R=1 +
    • +
    • + 61÷2=30,R=161 \div 2 = 30,R=1 +
    • +
    • + 30÷2=15,R=030 \div 2 = 15,R=0 +
    • +
    • + 15÷2=7,R=115 \div 2 = 7,R=1 +
    • +
    • + 7÷2=3,R=17 \div 2 = 3,R=1 +
    • +
    • + 3÷2=1,R=13 \div 2 = 1,R=1 +
    • +
    • + 1÷2=0,R=11 \div 2 = 0,R=1 +
    • +
    • + 24610=111101102246_{10} = 11110110_{2} +
    • +
    + +

    U2B(X) Conversion – A few tricks

    +
      +
    • Decimal -> binary +
        +
      • Trick: When decimal number is 2n, then its binary representation is 1 followed by n zero’s
      • +
      • Let’s try: if X = 32 => X = 25, then n = 5 => 100002 (w = 5) What if w = 8? Check: 1 x 24 = 32
      • +
      +
    • +
    • Decimal -> hex +
        +
      • Trick: When decimal number is 2n, then its hexadecimal representation is 2i followed by j zero’s, where n = i + 4j and 0 <= i <=3
      • +
      • Let try: if X = 8192 => X = 213, then n = 13 and 13 = i + 4j => 1 + 4 x 3 +=> 0x2000. Convert 0x2000 into a binary number: Check: 2 x 163 = 2 x 4096 = 8192
      • +
      +
    • +
    + +

    Signed integral numbers

    + +

    Remember:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-1 
     
    0x000301000010
    0x000201101001
    0x000101110100
    0x000001110011
    + +
      +
    • What if the byte at M[0x0001] represented a signed integral number, what would be its value?
    • +
    • X = 111101002 w = 8
    • +
    • T => Two’s Complement, w => width of the bit vector (annotation: first part of equaltion [everything before the plus sign] is the “sign bit”)
    • +
    • Let’s apply the encoding scheme:
    • +
    + +

    B2T(X)=xw1×2w1+i=0w2xi×2i +\it {B2T}(X) = -x_{w-1} \times 2^{w-1} + \sum_{i=0}^{w-2} x_{i} \times 2^{i} +

    + +

    Example: 1×27+1×26+1×25+1×24+0×23+1×22+0×21+0×20=?-1 \times 2^{7} + 1 \times 2^{6} + 1 \times 2^{5} + 1 \times 2^{4} + 0 \times 2^{3} + 1 \times 2^{2} + 0 \times 2^{1} + 0 \times 2^{0} = ?

    + +
      +
    • What would be the bit pattern of the … +
        +
      • Most negative value:
      • +
      • Most positive value:
      • +
      +
    • +
    • For w = 8, range of possible signed values: [ blank ]
    • +
    • For any w, range of possible signed values: [ blank ]
    • +
    • Conclusion: same as for unsigned integral numbers
    • +
    + +

    Examples of “Show your work”

    + +

    T2B(X) Conversion -> Two’s Complement

    + +

    Annotation: w = 8

    + +

    Method 1: If X < 0, (~(U2B(|X|)))+1

    + +

    If X = -14 (and 8 bit binary #s)

    + +
      +
    1. + X=>14=\lvert X\rvert => \lvert -14 \vert = +
    2. +
    3. U2B(14)\text{U2B}(14) =>
    4. +
    5. (first symbol is a tilde) (000011102)\sim(00001110_{2}) =>
    6. +
    7. (111100012)+1(11110001_{2})+1 =>
    8. +
    + +

    Binary addition:

    +
    +  11110001
    ++ 00000001
    += ????????
    +
    + +

    Method 2: If X = -14 (and 8 bit binary #s)

    + +
      +
    1. + X+2w=>14+X + 2^{w} => -14 + +
    2. +
    3. U2B(242)\text{U2B}(242) =>
    4. +
    + +

    Using subtraction:

    + +
      +
    1. 242128=114242 - 128 = 114 -> 1×271 \times 2^{7}
    2. +
    3. 11464=50114 - 64 = 50 -> 1×261 \times 2^{6}
    4. +
    5. 5032=1850 – 32 = 18 -> 1×251 \times 2^{5}
    6. +
    7. 1816=218 – 16 = 2 -> 1×241 \times 2^{4}
    8. +
    9. 28=nop!2 – 8 = \text{nop!} -> 0×230 \times 2^{3}
    10. +
    11. 24=nop!2 – 4 = \text{nop!} -> 0×220 \times 2^{2}
    12. +
    13. 22=02 – 2 = 0 -> 1×211 \times 2^{1}
    14. +
    15. 01=nop!0 – 1 = \text{nop!} -> 0×200 \times 2^{0}
    16. +
    + +

    Properties of unsigned & signed conversions

    + +

    Annotation: w = 4

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    XB2U(X)B2T(X)
    000000
    000111
    001022
    001133
    010044
    010155
    011066
    011177
    10008-8
    10019-7
    101010-6
    101111-5
    110012-4
    110113-3
    111014-2
    111115-1
    + +
      +
    • Equivalence +
        +
      • Both encoding schemes (B2U and B2T ) produce the same bit patterns for nonnegative values
      • +
      +
    • +
    • Uniqueness +
        +
      • Every bit pattern produced by these encoding schemes (B2U and B2T) represents a unique (and exact) integer value
      • +
      • Each representable integer has unique bit pattern
      • +
      +
    • +
    + +

    Converting between signed & unsigned of same size (same data type)

    + +
      +
    • Unsigned +
        +
      • w=8
      • +
      • if unsigned ux = 12910
      • +
      • U2T(X) = B2T(U2B(X))
      • +
      • then x = ???
      • +
      • Maintain Same Bit Pattern
      • +
      +
    • +
    • Signed (Two’s Complement) +
        +
      • w=4
      • +
      • if signed (2’s C) x=510x = -5_{10}
      • +
      • T2U(X) = B2U(T2B(X))
      • +
      • then unsigned ux = ???
      • +
      • Maintain Same Bit Pattern
      • +
      +
    • +
    • Conclusion - Converting between unsigned and signed numbers: +Both have same bit pattern, however, this bit pattern may be interpreted differently, i.e., producing a different value
    • +
    + +

    Converting signed to unsigned (and back) with w=4w = 4

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SignedBitsUnsignedNote
    000000All rows from 0-7 inclusive can be converted from signed to unsigned with T2U(X), and unsigned to signed with U2T(X).
    100011 
    200102 
    300113 
    401004 
    501015 
    601106 
    701117 
    -810008All rows from here to 15 inclusive can be converted to the other like so: T2U(signed + 16/242^{4}) -> unsigned, U2T(unsigned - 16/242^{4}) -> signed.
    -710019 
    -6101010 
    -5101111 
    -4110012 
    -3110113 
    -2111014 
    -1111115 
    + +

    Visualizing the relationship between signed & unsigned

    +

    If w=4,24=16w = 4, 2^{4} = 16

    + +
      +
    • Signed (2’s Complement) Range: TMin to TMax (0 is the center)
    • +
    • Unsigned range: 0 to UMax (TMax is the center)
    • +
    + +

    Sign extension

    +
      +
    • Converting unsigned (or signed) of different sizes (different data types) +
        +
      1. Small data type -> larger +
          +
        • Sign extension +
            +
          • Unsigned: zero extension
          • +
          • Signed: sign bit extension
          • +
          +
        • +
        +
      2. +
      +
    • +
    • Conclusion: Value unchanged
    • +
    • Let’s try: +
        +
      • Going from a data type that has a width of 3 bits (w = 3) to a data type that has a width of 5 bits (w = 5)
      • +
      +
    • +
    • Unsigned: X=3=0112,w=3X = 3 = 011_{2},w=3, X=4=1002,w=3X = 4 = 100_{2},w = 3 +
        +
      • New: X=?=?2,w=5X = ? = ?_{2},w=5, X=?+?2,w=5X = ? + ?_{2},w=5
      • +
      +
    • +
    • Signed: X=3=0112,w=3X = 3 = 011_{2},w=3, X=3=1012,w=3X=-3 = 101_{2},w=3 +
        +
      • New: X=?=?2,w=5X = ? = ?_{2},w=5, x=?=?2,w=5x = ? = ?_{2}, w=5
      • +
      +
    • +
    + +

    Truncation

    + +
      +
    • Converting unsigned (or signed) of different sizes(different data types) +
        +
      1. Large data type -> smaller +
          +
        • Truncation
        • +
        +
      2. +
      +
    • +
    • Conclusion: Value may be altered +
        +
      • A form of overflow
      • +
      +
    • +
    • Let’s try: +
        +
      • Going from a data type that has a width of 5 bits (w = 5) to a data type that has a width of 3 bits (w = 3)
      • +
      +
    • +
    • Unsigned: X=27=110112,w=5X = 27 = 11011_{2},w = 5 +
        +
      • New: X=?=?2,w=3X = ? = ?_{2},w=3
      • +
      +
    • +
    • Signed: X=15=100012,w=3X = -15 = 10001_{2},w=3, X=1=111112,w=5X = -1 = 11111_{2}, w=5 +
        +
      • New: X=?=?2,w=3X = ? = ?_{2}, w=3, X=?=?2,w=3X = ? = ?_{2}, w=3
      • +
      +
    • +
    + +

    Summary

    + +
      +
    • Interpretation of bit pattern B into either unsigned value U or signed value T +
        +
      • B2U(X) and U2B(X) encoding schemes (conversion)
      • +
      • B2T(X) and T2B(X) encoding schemes (conversion) +
          +
        • Signed value expressed as two’s complement => T
        • +
        +
      • +
      +
    • +
    • Conversions from unsigned <-> signed values +
        +
      • U2T(X) and T2U(X) => adding or subtracting 2w2^{w}
      • +
      +
    • +
    • Implication in C: when converting (implicitly via promotion and explicitly via casting): +
        +
      • Sign: +
          +
        • Unsigned <-> signed (of same size) -> Both have same bit pattern, however, this bit pattern may be interpreted differently +
            +
          • Can have unexpected effects -> producing a different value
          • +
          +
        • +
        +
      • +
      • Size: +
          +
        • Small -> large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int) +
            +
          • sign extension: For unsigned -> zeros extension, for signed -> sign bit extension
          • +
          • Both yield expected result –> resulting value unchanged
          • +
          +
        • +
        • Large -> small (e.g., unsigned int to unsigned short) +
            +
          • truncation: Unsigned/signed -> most significant bits are truncated (discarded)
          • +
          • May not yield expected results -> original value may be altered
          • +
          +
        • +
        +
      • +
      • Both (sign and size): 1) size conversion is first done then 2) sign conversion is done
      • +
      +
    • +
    + +

    Next Lecture

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +19 +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/03/Lecture_03_Data_Representation_Integers.pdf b/_site/melody/cmpt-295/03/Lecture_03_Data_Representation_Integers.pdf new file mode 100644 index 0000000..bd146ef Binary files /dev/null and b/_site/melody/cmpt-295/03/Lecture_03_Data_Representation_Integers.pdf differ diff --git a/_site/melody/cmpt-295/03/Lecture_03_Data_Representation_Integers.txt b/_site/melody/cmpt-295/03/Lecture_03_Data_Representation_Integers.txt new file mode 100644 index 0000000..ba654c0 --- /dev/null +++ b/_site/melody/cmpt-295/03/Lecture_03_Data_Representation_Integers.txt @@ -0,0 +1,781 @@ +CMPT 295 +Unit - Data Representation + +Lecture 3 – Representing integral numbers in memory - unsigned and signed + +1 + + Last Lecture + Von Neumann architecture + Architecture of most computers + Its components: CPU, memory, input and ouput, bus + One of its characteristics: Data and code (programs) both stored in memory + A look at memory: defined byte-addressable memory, diagram of (compressed) memory + + Word size (w): size of a series of bits (or bit vector) we manipulate, also size of machine words +(see Section 2.1.2) + A look at bits in memory + Why binary numeral system (0 and 1 -> two values) is used to represent information in memory + Algorithm for converting binary to hexadecimal (hex) + +1. Partition bit vector into groups of 4 bits, starting from right, i.e., least significant byte (LSB) + If most significant “byte” (MSB) does not have 8 bits, pad it: add 0’s to its left +2. Translate each group of 4 bits into its hex value + + What do bits represent? Encoding scheme gives meaning to bits + Order of bytes in memory: little endian versus big endian + Bit manipulation – regardless of what bit vectors represent + Boolean algebra: bitwise operations => AND (&), OR (|), XOR (^), NOT (~) + Shift operations: left shift, right logical shift and right arithmetic shift + +2 + + Logical shift: Fill x with y 0’s on left + Arithmetic shift: Fill x with y copies of x‘s sign bit on left + Sign bit: Most significant bit (MSb) before shifting occurred + +NOTE: +C logical operators +and C bitwise (bit-level) +operators behave +differently! +Watch out for && versus +&, || versus |, … + + Today’s Menu + Representing data in memory – Most of this is review + “Under the Hood” - Von Neumann architecture + + Bits and bytes in memory + How to diagram memory -> Used in this course and other references + How to represent series of bits -> In binary, in hexadecimal (conversion) + What kind of information (data) do series of bits represent -> Encoding scheme + Order of bytes in memory -> Endian + + Bit manipulation – bitwise operations + Boolean algebra + Shifting + + Representing integral numbers in memory + Unsigned and signed + Converting, expanding and truncating + Arithmetic operations + + Representing real numbers in memory +3 + + IEEE floating point representation + Floating point in C – casting, rounding, addition, … + + Warm up exercise! +As a warm up exercise, fill in the blanks! + If the context is C (on our target machine) + char + +=> _____ bits/ _____ byte + + short => _____ bits/ _____ bytes + int + +=> _____ bits/ _____ bytes + + long + +=> _____ bits/ _____ bytes + + float => _____ bits/ _____ bytes + double=> _____ bits/ _____ bytes + pointer (e.g. char *) +4 + +=> _____ bits/ _____ bytes + + Remember: + +Unsigned integral numbers + What if the byte at M[0x0002] represented an unsigned integral +A series of bits +number, what would be its value? +=> bit vector +w =>width of + + X = 011010012 + +the bit vector + +w=8 + + Let’s apply the encoding scheme: + +B2U(X)  + +w1 + + xi 2 + +i + +i0 + +0 x 27 + 1 x 26 + 1 x 25 + 0 x 24 + 1 x 23 + 0 x 22 + 0 x 21 + 1 x 20 = + +5 + + For w = 8, range of possible unsigned values: [ + +] + + For any w, range of possible unsigned values: [ + +] + + Conclusion: w bits can only represent a fixed # of possible values, +but these w bits represent these values exactly + + B2U(X) Conversion (Encoding scheme) + Positional notation: expand and sum all terms + +••• + +10i + +2i + +10i-1 + +2i-1 + +100 +10 +1 + +4 +2 +1 + +••• + +di di-1 ••• d2 d1 d0 +Example: 24610 = 2 x 102 + 4 x 101 + 6 x 100 +6 + +1’s = 100 +10’s = 101 +100’s = 102 + +B2U(X )  + +w1 + + xi 2 +i0 + +i + + Range of possible values? + If the context is C (on our target machine) +unsigned char? +unsigned short? + +unsigned int? +unsigned long? + +7 + + Examples of “Show your work” + +U2B(X) Conversion (into 8-bit binary # => w = 8) + +8 + +Method 1 - Using subtraction: +subtracting decreasing +power of 2 until reach 0 +246 => 246 – 128 = 118 ->128 = 1 x 27 +118 – 64 = 54 +-> 64 = 1 x 26 +54 – 32 = 22 +-> 32 = 1 x 25 +22 – 16 = 6 +-> 16 = 1 x 24 +6 – 8 = nop! -> 8 = 0 x 23 +6 – 4 =2 +-> 4 = 1 x 22 +2– 2=0 +-> 2 = 1 x 21 +0 – 1 = nop! -> 1 = 0 x 20 + +Method 2 - Using division: +dividing by 2 +until reach 0 +246 => 246 / 2 = 123 -> R = 0 +123 / 2 = 61 -> R = 1 +61 / 2 = 30 -> R = 1 +30 / 2 = 15 -> R = 0 +15 / 2 = 7 +-> R = 1 +7/2 =3 +-> R = 1 +3/2 =1 +-> R = 1 +1/2 =0 +-> R = 1 + +246 => 1 1 1 1 0 1 1 02 + +246 => 1 1 1 1 0 1 1 02 + + U2B(X) Conversion – A few tricks + Decimal -> binary + Trick: When decimal number is 2n, then its binary representation is 1 followed +by n zero’s + Let’s try: if X = 32 => X = 25, then n = 5 => 100002 (w = 5) + +What if w = 8? +Check: 1 x 24 = 32 + + Decimal -> hex + Trick: When decimal number is 2n, then its hexadecimal representation is 2i +followed by j zero’s, where n = i + 4j and 0 <= i <=3 + Let try: if X = 8192 => X = 213, then n = 13 and 13 = i + 4j => 1 + 4 x 3 +=> 0x2000 +9 + +Convert 0x2000 into a binary number: +Check: 2 x 163 = 2 x 4096 = 8192 + + Remember: + +Signed integral numbers + What if the byte at M[0x0001] represented a signed integral +number, what would be its value? T => Two’s Complement w =>width of +the bit vector + X = 111101002 w = 8 +w2 +w1 +i +B2T +(X +) + + +x +2 + +x +2 + +w1 +i + Let’s apply the encoding scheme: +Sign bit + +i0 + +-1 x 27 + 1 x 26 + 1 x 25 + 1 x 24 + 0 x 23 + 1 x 22 + 0 x 21 + 0 x 20 = + + What would be the bit pattern of the … + Most negative value: + Most positive value: + +10 + + For w = 8, range of possible signed values: [ + +] + + For any w, range of possible signed values: [ + +] + + Conclusion: same as for unsigned integral numbers + + Examples of “Show your work” + +T2B(X) Conversion -> Two’s Complement +w=8 +Method 1 If X < 0, (~(U2B(|X|)))+1 + +Method 2 + +If X = -14 (and 8 bit binary #s) + +If X = -14 (and 8 bit binary #s) + +1. |X| => |-14| = + +1. + +2. U2B(14) => + +2. U2B(242) => + +3. ~(000011102) => +4. (111100012)+1 => +Binary addition: +11110001 ++ 00000001 +11 +Check: + +If X < 0, U2B(X + 2w) + +X + 2w => -14 + + +Using subtraction: + +242 – 128 = 114 -> 1 x 27 +114 – 64 = 50 -> 1 x 26 +50 – 32 = 18 +-> 1 x 25 +18 – 16 = 2 +-> 1 x 24 +2 – 8 -> nop! -> 0 x 23 +2 – 4 -> nop! -> 0 x 22 +2–2=0 +-> 1 x 21 +0 – 1 -> nop! -> 0 x 20 + + Properties of unsigned & signed conversions +w=4 + +12 + +X +0000 +0001 +0010 +0011 +0100 +0101 +0110 +0111 +1000 +1001 +1010 +1011 +1100 +1101 +1110 +1111 + +B2U(X) +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 + +B2T(X) +0 +1 +2 +3 +4 +5 +6 +7 +–8 +–7 +–6 +–5 +–4 +–3 +–2 +–1 + + Equivalence + Both encoding schemes (B2U +and B2T ) produce the same bit +patterns for nonnegative values + Uniqueness + +Every bit pattern produced by +these encoding schemes (B2U +and B2T ) represents a unique +(and exact) integer value + + Each representable integer has +unique bit pattern + + Converting between signed & unsigned +of same size (same data type) +Unsigned + +w=8 + +ux + +If ux = 12910 +Signed (Two’s Complement) + +w=4 + +13 + +x + +If x = -510 + +U2T +U2B X + +B2T + +Maintain Same Bit Pattern + +then x = +Unsigned + +T2U + +T2B X + +Signed (Two’s Complement) +x + +B2U + +Maintain Same Bit Pattern + +ux + +then ux = + + Conclusion - Converting between unsigned and signed numbers: +Both have same bit pattern, however, this bit pattern may be interpreted +differently, i.e., producing a different value + + Converting signed  unsigned with w = 4 +Signed + +Bits + +Unsigned + +0 + +0000 + +0 + +1 + +0001 + +2 + +0010 + +2 + +3 + +0011 + +3 + +0100 + +4 + +5 + +0101 + +5 + +6 + +0110 + +6 + +7 + +0111 + +7 + +-8 + +1000 + +8 + +-7 + +1001 + +9 + +-6 + +1010 + +10 + +4 + +-5 + +U2T(X) + ++ 16 (+24) + +-4 +-3 + +14 + +14 + +-2 +-1 + +U2T(X) + +T2U(X) + +1 + +1011 + +T2U(X) + +11 + +1100 + +- 16 (+24) + +12 + +1101 + +13 + +1110 + +14 + +1111 + +15 + + Visualizing the relationship between +signed & unsigned +If w = 4, + +24 + +UMax +UMax – 1 + += 16 + +TMax + +Signed +(2’s Complement) +Range +15 + +0 +–1 +–2 + +TMin + +TMax + 1 +TMax + +0 + +Unsigned +Range + + Sign extension + Converting unsigned (or signed) of different sizes (different data types) +1. Small data type -> larger + +Sign bit + + Sign extension + +X + +Unsigned: zero extension + +••• + +Signed: sign bit extension + + Conclusion: Value unchanged + +••• + +X + + Let’s try: + +••• + +••• + + Going from a data type that has a width of 3 bits (w = 3) to a data type +that has a width of 5 bits (w = 5) + Unsigned: X = 3 => +new X = +16 + + Signed: + +0112 w = 3 + +<= + +w=5 + +X = 3 => + +0112 w = 3 + +new X = + +<= + +w=5 + +X = 4 => +new X = + +1002 w = 3 + +<= + +w=5 + +X = -3 => + +1012 w = 3 + +new X = + +<= + +w=5 + + Truncation + Converting unsigned (or signed) of different sizes(different data types) + +2. Large data type -> smaller +••• + +X + + Truncation + +••• + + Conclusion: Value may be altered +A form of overflow + + Let’s try: + +X + +••• + + Going from a data type that has a width of 5 bits (w = 5) to a data type +that has a width of 3 bits (w = 3) + Unsigned: X = 27 => 110112 w = 5 + +new X = + Signed: +17 + +<= + +w=3 + +X = -15 => 100012 w = 5 + +new X = + +<= + +w=3 + +X = -1 => 111112 w = 5 +new X = + +<= + +w=3 + + Summary + Interpretation of bit pattern B into either unsigned value U or signed value T + B2U(X) and U2B(X) encoding schemes (conversion) + B2T(X) and T2B(X) encoding schemes (conversion) + Signed value expressed as two’s complement => T + + Conversions from unsigned <-> signed values + U2T(X) and T2U(X) => adding or subtracting 2w + + Implication in C: when converting (implicitly via promotion and explicitly via casting): + Sign: + Unsigned <-> signed (of same size) -> Both have same bit pattern, however, this bit pattern may + +be interpreted differently + + Can have unexpected effects -> producing a different value + + Size: + Small -> large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int) + sign extension: For unsigned -> zeros extension, for signed -> sign bit extension + Both yield expected result –> resulting value unchanged + + Large -> small (e.g., unsigned int to unsigned short) + truncation: Unsigned/signed -> most significant bits are truncated (discarded) + May not yield expected results -> original value may be altered + +18 + + Both (sign and size): 1) size conversion is first done then 2) sign conversion is done + + Next Lecture + Representing data in memory – Most of this is review + “Under the Hood” - Von Neumann architecture + + Bits and bytes in memory + How to diagram memory -> Used in this course and other references + How to represent series of bits -> In binary, in hexadecimal (conversion) + What kind of information (data) do series of bits represent -> Encoding scheme + Order of bytes in memory -> Endian + + Bit manipulation – bitwise operations + Boolean algebra + Shifting + + Representing integral numbers in memory + Unsigned and signed + Converting, expanding and truncating + Arithmetic operations + + Representing real numbers in memory +19 + + IEEE floating point representation + Floating point in C – casting, rounding, addition, … + + diff --git a/_site/melody/cmpt-295/04/04.html b/_site/melody/cmpt-295/04/04.html new file mode 100644 index 0000000..2ce4504 --- /dev/null +++ b/_site/melody/cmpt-295/04/04.html @@ -0,0 +1,584 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295

    + +

    Unit - Data Representation

    + +

    Lecture 4 – Representing integral numbers in memory – Arithmetic operations

    + +

    Warm up question

    +
      +
    • What is the value of … +
        +
      • TMin (in hex) for signed char in C: _________________
      • +
      • TMax (in hex) for signed int in C: _________________
      • +
      • TMin (in hex) for signed short in C: ________________
      • +
      +
    • +
    + +

    Last Lecture

    +
      +
    • Interpretation of bit pattern B into either unsigned value U or signed value T +
        +
      • B2U(X) and U2B(X) encoding schemes (conversion)
      • +
      • B2T(X) and T2B(X) encoding schemes (conversion) +
          +
        • Signed value expressed as two’s complement => T
        • +
        +
      • +
      +
    • +
    • Conversions from unsigned <-> signed values +
        +
      • U2T(X) and T2U(X) => adding or subtracting 2w
      • +
      +
    • +
    • Implication in C: when converting (implicitly via promotion and explicitly via casting): +
        +
      • Sign: +
          +
        • Unsigned <-> signed (of same size) -> Both have same bit pattern, however, this bit pattern may be interpreted differently +
            +
          • Can have unexpected effects -> producing a different value
          • +
          +
        • +
        +
      • +
      • Size: +
          +
        • Small -> large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int) +
            +
          • sign extension: For unsigned -> zeros extension, for signed -> sign bit extension
          • +
          • Both yield expected result –> resulting value unchanged
          • +
          +
        • +
        • Large -> small (for signed, e.g., int to short and for unsigned, e.g., unsigned int to unsigned short) +
            +
          • truncation: Unsigned/signed -> most significant bits are truncated (discarded)
          • +
          • May not yield expected results -> original value may be altered
          • +
          +
        • +
        +
      • +
      • Both (sign and size): 1) size conversion is first done then 2) sign conversion is done
      • +
      +
    • +
    + +

    Today’s Menu

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    Let’s first illustrate what we covered last lecture with a demo!

    + +

    Demo – Looking at size and sign conversions in C

    + +
      +
    • What does the demo illustrate? +
        +
      • Size conversion: +
          +
        • Converting to a larger (wider) data type -> Converting short to int
        • +
        • Converting to a smaller (narrower) data type -> Converting short to char
        • +
        +
      • +
      • Sign conversion: +
          +
        • Converting from signed to unsigned -> Converting short to unsigned short
        • +
        • Converting from unsigned to signed -> Converting unsigned short to short
        • +
        +
      • +
      • Size and Sign conversion: +
          +
        • Converting from signed to unsigned larger (wider) data type -> Converting short to unsigned int
        • +
        • Converting from signed to unsigned smaller (narrower) data type -> +Converting short to unsigned char
        • +
        +
      • +
      +
    • +
    • This demo (code and results) posted on our course web site
    • +
    + +

    Integer addition (unlimited space)

    + +
      +
    • +

      What happens when we add two decimal numbers? (Highlights show carring the one to complete the equation) +10710+93810=104510 +107_{10} + 938_{10} = 1045_{10}

      +
    • +
    • +

      Same thing happens when we add two binary numbers:

      +
    • +
    + +

    1011002+1011102=10110102 +101100_{2} + 101110_{2} = 1011010_{2} +

    + +

    Unsigned addition (limited space, i.e., fixed size in memory)

    + +

    What happens when we add two unsigned values?

    + +w=8w=8 + +

    a)

    + +

    Binary:

    + +

    001110112+010110102=? +00111011_{2} + 01011010_{2} = ? +

    + +

    Decimal:

    + +

    5910+9010=14910 +59_{10} + 90_{10} = 149_{10} +

    + +

    b)

    + +

    Binary:

    + +

    101011102+110010112=? +10101110_{2} + 11001011_{2} = ? +

    + +

    Decimal:

    + +

    17410+20310=37710 +174_{10} + 203_{10} = 377_{10} +

    + +

    Unsigned addition (+wu+^{u}_{w}) and overflow

    + +
      +
    • True/expected sum is the result of integer addition with unlimited space.
    • +
    • Actual sum is the result of unsigned addition with limited space. Discarding the carry out bit.
    • +
    • Discarding carry out bit has same effect as applying modular arithmetic
    • +
    + +

    s=U+wuv=(u+v)mod2w +s = U+^{u}_{w}v = (u + v) \mod{2^{w}} +

    + +
      +
    • Operands: w bits
    • +
    • True Sum: w+1 bits
    • +
    + +

    Closer look at unsigned addition overflow

    + +
      +
    • w = 8 -> [0..255]
    • +
    • + 25510=111111112255_{10} = 11111111_{2} +
    • +
    • + 9010=01011010290_{10} = 01011010_{2} +
    • +
    • + 4510=00101101245_{10} = 00101101_{2} +
    • +
    + +

    Example 1:

    + +

    Decimal (carry out the 1 in 135):

    + +

    9010+4510=13510 +90_{10} + 45_{10} = 135_{10} +

    + +

    Binary (carry out the 1 in 10000111210000111_{2}):

    + +

    010110102+001011012=100001112 +01011010_{2} + 00101101_{2} = 10000111_{2} +

    + +
      +
    • True sum, w=8: 10000111210000111_{2}
    • +
    • Actual (overflowed) sum, w=8: 100001112=1351010000111_{2} = 135_{10}
    • +
    + +

    Example 2:

    + +

    Decimal (carry 1 to the 3 in 300):

    + +

    25510+4510=30010 +255_{10} + 45_{10} = 300_{10} +

    + +

    Binary (carry the 1 at the beginning of the result):

    + +

    111111112+001011012=1001011002 +11111111_{2} + 00101101_{2} = 100101100_{2} +

    + +
      +
    • True sum, w=9: 1001011002=30010100101100_{2} = 300_{10}
    • +
    • Actual (overflowed) sum, w=8: 001011002=441000101100_{2} = 44_{10}
    • +
    + +

    Comparing integer addition with Overflow: Effect of unsigned addition (w = 4)

    + +

    Annotation: with unlimited space:

    + +

    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):

    + +

    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)

    + +

    An overflow occurs when there is a carry out

    + +

    For example: 15 (111121111_{2}) + 15 (111121111_{2}) = 30 (11110211110_{2}) as a true sum, and 14 (11110211110_{2}) as an actual sum.

    + +

    Signed addition (limited space, i.e., fixed size in memory)

    + +

    What happens when we add two signed values:

    + +

    w=8

    + +

    a)

    + +

    Binary: +001110112+010110102=?00111011_{2} + 01011010_{2} = ?

    + +

    Decimal: +5910+9010=1491059_{10} + 90_{10} = 149_{10}

    + +

    b)

    + +

    Binary: 101011102+110010112=?10101110_{2} + 11001011_{2} = ?

    + +

    Decimal: 8210+5310=13510-82_{10} + -53_{10} = -135_{10}

    + +

    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

    + +

    Signed addition (+wt+^{t}_{w}) and overflow

    + +

    True sum would be the result of integer addition with unlimited space: +Actual sum is the result of signed addition with limited space:

    + +

    Operands: w bits +True Sum: w+1 bits

    + +

    After discarduing carry out bit: w bits (overflow)

    + +
      +
    • Discarding carry out bit has same effect as applying modular arithmetic
    • +
    + +

    s=u+wtv=U2Tw[(u+v)mod2w] +s = u +^{t}_{w} v = \text{U2T}_{w}[(u + v) \mod 2^{w}] +

    + +

    Negative overflow and positive overflows are possible. +Diagram showing negative overflows becoming positive, and positive overflows becoming negative.

    + +

    Closer look at signed addition overflow

    +
      +
    • w = 8 -> [-128..127]
    • +
    • + 9010=01011010290_{10} = 01011010_{2} +
    • +
    • + 4510=00101101245_{10} = 00101101_{2} +
    • +
    • + 4510=110100112-45_{10} = 11010011_{2} +
    • +
    • + 9010=101001102-90_{10} = 10100110_{2} +
    • +
    + +

    Example 1:

    + +

    Decimal: 9010+4510=1351090_{10} + 45_{10} = 135_{10}

    + +

    Binary: 010110102+001011012=010000111201011010_{2} + 00101101_{2} = 010000111_{2}

    + +

    The binary result is -121

    + +

    Example 2:

    + +

    Decimal: 9010+4510=13510-90_{10} + -45_{10} = -135_{10}

    + +

    Binary: 101001102+110100112=101111001210100110_{2} + 11010011_{2} = 101111001_{2}

    + +

    Binary result is 121

    + +

    Example 3:

    + +

    Decimal: 9010+4510=4510-90_{10} + 45_{10} = -45_{10}

    + +

    Binary: 101001102+001011012=011010011210100110_{2} + 00101101_{2} = 011010011_{2}

    + +

    Example 4:

    + +

    Decimal: 9010+4510=451090_{10} + -45_{10} = 45_{10}

    + +

    Binary: 010110102+110100112=100101101201011010_{2} + 11010011_{2} = 100101101_{2}

    + +

    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.

    + +

    Visualizing signed addition overflow (w = 4)

    + +

    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

    + +

    Positive Overflow

    + +

    For example: 7 (011120111_{2}) + 1 (000120001_{2}) = 8 (100021000_{2}) is the true sum and = -8 (100021000_{2}) is the actual sum

    + +

    What about subtraction? -> Addition

    + +

    x + (-x) = 0

    + +
      +
    • Subtracting a number is equivalent to adding its additive inverse +
        +
      • Instead of subtracting a positive number, we could add its negative version:
      • +
      +
    • +
    + +

    107118=11 +107 - 118 = -11 +

    + +

    becomes:

    + +

    107+(118)=18 +107 + (-118) = -18 +

    + +
      +
    • Let‘s try:
    • +
    + +

    Decimal:

    + +1071011810=11107_{10} - 118_{10} = -11 + +

    Binary:

    + +011010112011101102=01101011_{2} - 01110110_{2} = + +

    Binary subtraction by addition:

    + +011010112+100010102=11110101201101011_{2} + 10001010_{2} = 11110101_{2} + +

    Binary subtraction by addition is equal to -11

    + +

    Check: 128+64+32+16+4+1=1110-128 + 64 + 32 + 16 + 4 + 1 = -11_{10}

    + +

    T2B(X) conversion:

    +
      +
    1. + ( (U2B(X)))+1(~(\text{U2B}(|X|)))+1 +
    2. +
    3. + ( (U2B(118)))+1(~(\text{U2B}(|-118|)))+1 +
    4. +
    5. + ( (U2B(118)))+1(~(\text{U2B}(118)))+1 +
    6. +
    7. + ((011101102))+1(\sim(01110110_{2}))+1 +
    8. +
    9. + (100010012)+1(10001001_{2})+1 +
    10. +
    11. + 10001010210001010_{2} +
    12. +
    + +

    Multiplication (wu,wt*^{u}_{w}, *^{t}_{w}) and overflow

    + +

    True product would be the result of integer multiplication with unlimited space: expected product +Actual product is the result of multiplication with limited space.

    + +
      +
    • Operands: w bits
    • +
    • True Product: 2w bits u×vu \times v
    • +
    • +

      Discard: w bits

      +
    • +
    • Discarding high order w bits has same effect as applying modular arithmetic
    • +
    + +p=uwuv=(u×v)mod2wp = u *^{u}_{w}v = (u \times v) \mod 2^{w} + +p=uwtv=U2Tw[(u×v)mod2w]p = u *^{t}_{w}v = \text{U2T}_{w}[(u \times v) \mod 2^{w}] + +
      +
    • Example: w = 4
    • +
    + +

    Decimal:

    + +510×510=25105_{10} \times 5_{10} = 25_{10} + +

    Binary:

    + +01012×01012=001100120101_{2} \times 0101_{2} = \sout{001}1001_{2} + +

    Multiplication with power-of-2 versus shifting

    + +
      +
    • If x×yx \times y where y=2ky = 2^{k} then x << k +
        +
      • For both signed and unsigned
      • +
      +
    • +
    • Example: +
        +
      • x×8=x×23x \times 8 = x \times 2^{3} = x << 3
      • +
      • x×24=(x×25)(x×23)=(x×32)(x×8)x \times 24 = (x \times 25) – (x \times 23) = (x \times 32) – (x \times 8) = (x « 5) – (x « 3) (decompose 24 in powers of 2 => 32 – 8)
      • +
      +
    • +
    • Most machines shift and add faster than multiply +
        +
      • We’ll soon see that compiler generates this code automatically +17
      • +
      +
    • +
    + +

    Summary

    + +
      +
    • Demo of size and sign conversion in C: code and results posted!
    • +
    • Addition: +
        +
      • Unsigned/signed: +
          +
        • Behave the same way at the bit level
        • +
        • Interpretation of resulting bit vector (sum) may differ
        • +
        +
      • +
      • Unsigned addition -> may overflow, i.e., (w+1)th bit is set +
          +
        • If so, then actual sum obtained => (x+y)mod2w(x + y) \mod 2^{w}
        • +
        +
      • +
      • Signed addition -> may overflow, i.e., (w+1)th bit is set +
          +
        • If so, then true sum may be too +ve -> positive overflow OR too -ve -> negative overflow
        • +
        • Then actual sum obtained => U2Tw[(x+y)mod2w]\text{U2T}_{w}[(x + y) \mod 2^{w}]
        • +
        +
      • +
      +
    • +
    • Subtraction +
        +
      • Becomes an addition where negative operands are transformed into their additive inverse (in two’s complement)
      • +
      +
    • +
    • Multiplication: +
        +
      • Unsigned: actual product obtained -> (x×y)mod2w(x \times y) \mod 2^{w}
      • +
      • Signed: actual product obtained -> $$\text{U2T}_{w}[(x \times y) \mod 2^{w}]
      • +
      • Can be replaced by additions and shifts
      • +
      +
    • +
    + +

    Next lecture

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    We’ll illustrate what we covered today by having a demo!

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/04/04/index.html b/_site/melody/cmpt-295/04/04/index.html new file mode 100644 index 0000000..57d2528 --- /dev/null +++ b/_site/melody/cmpt-295/04/04/index.html @@ -0,0 +1,586 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295

    + +

    Unit - Data Representation

    + +

    Lecture 4 – Representing integral numbers in memory – Arithmetic operations

    + +

    Warm up question

    +
      +
    • What is the value of … +
        +
      • TMin (in hex) for signed char in C: _________________
      • +
      • TMax (in hex) for signed int in C: _________________
      • +
      • TMin (in hex) for signed short in C: ________________
      • +
      +
    • +
    + +

    Last Lecture

    +
      +
    • Interpretation of bit pattern B into either unsigned value U or signed value T +
        +
      • B2U(X) and U2B(X) encoding schemes (conversion)
      • +
      • B2T(X) and T2B(X) encoding schemes (conversion) +
          +
        • Signed value expressed as two’s complement => T
        • +
        +
      • +
      +
    • +
    • Conversions from unsigned <-> signed values +
        +
      • U2T(X) and T2U(X) => adding or subtracting 2w
      • +
      +
    • +
    • Implication in C: when converting (implicitly via promotion and explicitly via casting): +
        +
      • Sign: +
          +
        • Unsigned <-> signed (of same size) -> Both have same bit pattern, however, this bit pattern may be interpreted differently +
            +
          • Can have unexpected effects -> producing a different value
          • +
          +
        • +
        +
      • +
      • Size: +
          +
        • Small -> large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int) +
            +
          • sign extension: For unsigned -> zeros extension, for signed -> sign bit extension
          • +
          • Both yield expected result –> resulting value unchanged
          • +
          +
        • +
        • Large -> small (for signed, e.g., int to short and for unsigned, e.g., unsigned int to unsigned short) +
            +
          • truncation: Unsigned/signed -> most significant bits are truncated (discarded)
          • +
          • May not yield expected results -> original value may be altered
          • +
          +
        • +
        +
      • +
      • Both (sign and size): 1) size conversion is first done then 2) sign conversion is done
      • +
      +
    • +
    + +

    Today’s Menu

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    Let’s first illustrate what we covered last lecture with a demo!

    + +

    Demo – Looking at size and sign conversions in C

    + +
      +
    • What does the demo illustrate? +
        +
      • Size conversion: +
          +
        • Converting to a larger (wider) data type -> Converting short to int
        • +
        • Converting to a smaller (narrower) data type -> Converting short to char
        • +
        +
      • +
      • Sign conversion: +
          +
        • Converting from signed to unsigned -> Converting short to unsigned short
        • +
        • Converting from unsigned to signed -> Converting unsigned short to short
        • +
        +
      • +
      • Size and Sign conversion: +
          +
        • Converting from signed to unsigned larger (wider) data type -> Converting short to unsigned int
        • +
        • Converting from signed to unsigned smaller (narrower) data type -> +Converting short to unsigned char
        • +
        +
      • +
      +
    • +
    • This demo (code and results) posted on our course web site
    • +
    + +

    Integer addition (unlimited space)

    + +
      +
    • +

      What happens when we add two decimal numbers? (Highlights show carring the one to complete the equation) +10710+93810=104510 +107_{10} + 938_{10} = 1045_{10}

      +
    • +
    • +

      Same thing happens when we add two binary numbers:

      +
    • +
    + +

    1011002+1011102=10110102 +101100_{2} + 101110_{2} = 1011010_{2} +

    + +

    Unsigned addition (limited space, i.e., fixed size in memory)

    + +

    What happens when we add two unsigned values?

    + +w=8w=8 + +

    a)

    + +

    Binary:

    + +

    001110112+010110102=? +\mathit{00111011}_{2} + \mathit{01011010}_{2} = ? +

    + +

    Decimal:

    + +

    5910+9010=14910 +\mathit{59}_{10} + {90}_{10} = {149}_{10} +

    + +

    b)

    + +

    Binary:

    + +

    101011102+110010112=? +\mathit{10101110}_{2} + \mathit{11001011}_{2} = ? +

    + +

    Decimal:

    + +

    17410+20310=37710 +174_{10} + 203_{10} = 377_{10} +

    + +

    Unsigned addition (+wu+^{u}_{w}) and overflow

    + +
      +
    • True/expected sum is the result of integer addition with unlimited space.
    • +
    • Actual sum is the result of unsigned addition with limited space. Discarding the carry out bit.
    • +
    • Discarding carry out bit has same effect as applying modular arithmetic
    • +
    + +

    s=U+wuv=(u+v)mod2w +s = U+^{u}_{w}v = (u + v) \mod{2^{w}} +

    + +
      +
    • Operands: w bits
    • +
    • True Sum: w+1 bits
    • +
    + +

    Closer look at unsigned addition overflow

    + +
      +
    • w = 8 -> [0..255]
    • +
    • + 25510=111111112255_{10} = 11111111_{2} +
    • +
    • + 9010=01011010290_{10} = 01011010_{2} +
    • +
    • + 4510=00101101245_{10} = 00101101_{2} +
    • +
    + +

    Example 1:

    + +

    Decimal (carry out the 1 in 135):

    + +

    9010+4510=13510 +90_{10} + 45_{10} = 135_{10} +

    + +

    Binary (carry out the 1 in 10000111210000111_{2}):

    + +

    010110102+001011012=100001112 +01011010_{2} + 00101101_{2} = 10000111_{2} +

    + +
      +
    • True sum, w=8: 10000111210000111_{2}
    • +
    • Actual (overflowed) sum, w=8: 100001112=1351010000111_{2} = 135_{10}
    • +
    + +

    Example 2:

    + +

    Decimal (carry 1 to the 3 in 300):

    + +

    25510+4510=30010 +255_{10} + 45_{10} = 300_{10} +

    + +

    Binary (carry the 1 at the beginning of the result):

    + +

    111111112+001011012=1001011002 +11111111_{2} + 00101101_{2} = 100101100_{2} +

    + +
      +
    • True sum, w=9: 1001011002=30010100101100_{2} = 300_{10}
    • +
    • Actual (overflowed) sum, w=8: 001011002=441000101100_{2} = 44_{10}
    • +
    + +

    Comparing integer addition with Overflow: Effect of unsigned addition (w = 4)

    + +

    Annotation: with unlimited space:

    + +

    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):

    + +

    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)

    + +

    An overflow occurs when there is a carry out

    + +

    For example: 15 (111121111_{2}) + 15 (111121111_{2}) = 30 (11110211110_{2}) as a true sum, and 14 (11110211110_{2}) as an actual sum.

    + +

    Signed addition (limited space, i.e., fixed size in memory)

    + +

    What happens when we add two signed values:

    + +

    w=8

    + +

    a)

    + +

    Binary: +001110112+010110102=?00111011_{2} + 01011010_{2} = ?

    + +

    Decimal: +5910+9010=1491059_{10} + 90_{10} = 149_{10}

    + +

    b)

    + +

    Binary: 101011102+110010112=?10101110_{2} + 11001011_{2} = ?

    + +

    Decimal: 8210+5310=13510-82_{10} + -53_{10} = -135_{10}

    + +

    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

    + +

    Signed addition (+wt+^{t}_{w}) and overflow

    + +

    True sum would be the result of integer addition with unlimited space: +Actual sum is the result of signed addition with limited space:

    + +

    Operands: w bits +True Sum: w+1 bits

    + +

    After discarduing carry out bit: w bits (overflow)

    + +
      +
    • Discarding carry out bit has same effect as applying modular arithmetic
    • +
    + +

    s=u+wtv=U2Tw[(u+v)mod2w] +s = u +^{t}_{w} v = \text{U2T}_{w}[(u + v) \mod 2^{w}] +

    + +

    Negative overflow and positive overflows are possible. +Diagram showing negative overflows becoming positive, and positive overflows becoming negative.

    + +

    Closer look at signed addition overflow

    +
      +
    • w = 8 -> [-128..127]
    • +
    • + 9010=01011010290_{10} = 01011010_{2} +
    • +
    • + 4510=00101101245_{10} = 00101101_{2} +
    • +
    • + 4510=110100112-45_{10} = 11010011_{2} +
    • +
    • + 9010=101001102-90_{10} = 10100110_{2} +
    • +
    + +

    Example 1:

    + +

    Decimal: 9010+4510=1351090_{10} + 45_{10} = 135_{10}

    + +

    Binary: 010110102+001011012=010000111201011010_{2} + 00101101_{2} = 010000111_{2}

    + +

    The binary result is -121

    + +

    Example 2:

    + +

    Decimal: 9010+4510=13510-90_{10} + -45_{10} = -135_{10}

    + +

    Binary: 101001102+110100112=101111001210100110_{2} + 11010011_{2} = 101111001_{2}

    + +

    Binary result is 121

    + +

    Example 3:

    + +

    Decimal: 9010+4510=4510-90_{10} + 45_{10} = -45_{10}

    + +

    Binary: 101001102+001011012=011010011210100110_{2} + 00101101_{2} = 011010011_{2}

    + +

    Example 4:

    + +

    Decimal: 9010+4510=451090_{10} + -45_{10} = 45_{10}

    + +

    Binary: 010110102+110100112=100101101201011010_{2} + 11010011_{2} = 100101101_{2}

    + +

    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.

    + +

    Visualizing signed addition overflow (w = 4)

    + +

    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

    + +

    Positive Overflow

    + +

    For example: 7 (011120111_{2}) + 1 (000120001_{2}) = 8 (100021000_{2}) is the true sum and = -8 (100021000_{2}) is the actual sum

    + +

    What about subtraction? -> Addition

    + +

    x + (-x) = 0

    + +
      +
    • Subtracting a number is equivalent to adding its additive inverse +
        +
      • Instead of subtracting a positive number, we could add its negative version:
      • +
      +
    • +
    + +

    107118=11 +107 - 118 = -11 +

    + +

    becomes:

    + +

    107+(118)=18 +107 + (-118) = -18 +

    + +
      +
    • Let‘s try:
    • +
    + +

    Decimal:

    + +1071011810=11107_{10} - 118_{10} = -11 + +

    Binary:

    + +011010112011101102=01101011_{2} - 01110110_{2} = + +

    Binary subtraction by addition:

    + +011010112+100010102=11110101201101011_{2} + 10001010_{2} = 11110101_{2} + +

    Binary subtraction by addition is equal to -11

    + +

    Check: 128+64+32+16+4+1=1110-128 + 64 + 32 + 16 + 4 + 1 = -11_{10}

    + +

    T2B(X) conversion:

    +
      +
    1. + ( (U2B(X)))+1(~(\text{U2B}(|X|)))+1 +
    2. +
    3. + ( (U2B(118)))+1(~(\text{U2B}(|-118|)))+1 +
    4. +
    5. + ( (U2B(118)))+1(~(\text{U2B}(118)))+1 +
    6. +
    7. + ((011101102))+1(\sim(01110110_{2}))+1 +
    8. +
    9. + (100010012)+1(10001001_{2})+1 +
    10. +
    11. + 10001010210001010_{2} +
    12. +
    + +

    Multiplication (wu,wt*^{u}_{w}, *^{t}_{w}) and overflow

    + +

    True product would be the result of integer multiplication with unlimited space: expected product +Actual product is the result of multiplication with limited space.

    + +
      +
    • Operands: w bits
    • +
    • True Product: 2w bits u×vu \times v
    • +
    • +

      Discard: w bits

      +
    • +
    • Discarding high order w bits has same effect as applying modular arithmetic
    • +
    + +p=uwuv=(u×v)mod2wp = u *^{u}_{w}v = (u \times v) \mod 2^{w} + +p=uwtv=U2Tw[(u×v)mod2w]p = u *^{t}_{w}v = \text{U2T}_{w}[(u \times v) \mod 2^{w}] + +
      +
    • Example: w = 4
    • +
    + +

    Decimal:

    + +510×510=25105_{10} \times 5_{10} = 25_{10} + +

    Binary:

    + +01012×01012=001100120101_{2} \times 0101_{2} = \sout{001}1001_{2} + +

    Multiplication with power-of-2 versus shifting

    + +
      +
    • If x×yx \times y where y=2ky = 2^{k} then x << k +
        +
      • For both signed and unsigned
      • +
      +
    • +
    • Example: +
        +
      • x×8=x×23x \times 8 = x \times 2^{3} = x << 3
      • +
      • x×24=(x×25)(x×23)=(x×32)(x×8)x \times 24 = (x \times 25) – (x \times 23) = (x \times 32) – (x \times 8) = (x « 5) – (x « 3) (decompose 24 in powers of 2 => 32 – 8)
      • +
      +
    • +
    • Most machines shift and add faster than multiply +
        +
      • We’ll soon see that compiler generates this code automatically +17
      • +
      +
    • +
    + +

    Summary

    + +
      +
    • Demo of size and sign conversion in C: code and results posted!
    • +
    • Addition: +
        +
      • Unsigned/signed: +
          +
        • Behave the same way at the bit level
        • +
        • Interpretation of resulting bit vector (sum) may differ
        • +
        +
      • +
      • Unsigned addition -> may overflow, i.e., (w+1)th bit is set +
          +
        • If so, then actual sum obtained => (x+y)mod2w(x + y) \mod 2^{w}
        • +
        +
      • +
      • Signed addition -> may overflow, i.e., (w+1)th bit is set +
          +
        • If so, then true sum may be too +ve -> positive overflow OR too -ve -> negative overflow
        • +
        • Then actual sum obtained => U2Tw[(x+y)mod2w]\text{U2T}_{w}[(x + y) \mod 2^{w}]
        • +
        +
      • +
      +
    • +
    • Subtraction +
        +
      • Becomes an addition where negative operands are transformed into their additive inverse (in two’s complement)
      • +
      +
    • +
    • Multiplication: +
        +
      • Unsigned: actual product obtained -> (x×y)mod2w(x \times y) \mod 2^{w}
      • +
      • Signed: actual product obtained -> $$\text{U2T}_{w}[(x \times y) \mod 2^{w}]
      • +
      • Can be replaced by additions and shifts
      • +
      +
    • +
    + +

    Next lecture

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    We’ll illustrate what we covered today by having a demo!

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/04/Lecture_04_Data_Representation_Integers.pdf b/_site/melody/cmpt-295/04/Lecture_04_Data_Representation_Integers.pdf new file mode 100644 index 0000000..482f040 Binary files /dev/null and b/_site/melody/cmpt-295/04/Lecture_04_Data_Representation_Integers.pdf differ diff --git a/_site/melody/cmpt-295/04/Lecture_4_Demo.c b/_site/melody/cmpt-295/04/Lecture_4_Demo.c new file mode 100644 index 0000000..69cd8b9 --- /dev/null +++ b/_site/melody/cmpt-295/04/Lecture_4_Demo.c @@ -0,0 +1,97 @@ +#include +#include +#include + + +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; + +} \ No newline at end of file diff --git a/_site/melody/cmpt-295/04/Lecture_4_Demo_RESULTS.txt b/_site/melody/cmpt-295/04/Lecture_4_Demo_RESULTS.txt new file mode 100644 index 0000000..a00e1ac --- /dev/null +++ b/_site/melody/cmpt-295/04/Lecture_4_Demo_RESULTS.txt @@ -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 \ No newline at end of file diff --git a/_site/melody/cmpt-295/05/Lecture_05_Data_Representation_Fractional_Numbers.html b/_site/melody/cmpt-295/05/Lecture_05_Data_Representation_Fractional_Numbers.html new file mode 100644 index 0000000..f02fbbe --- /dev/null +++ b/_site/melody/cmpt-295/05/Lecture_05_Data_Representation_Fractional_Numbers.html @@ -0,0 +1,766 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295: Unit - Data Representation

    + +

    Lecture 5

    + +
      +
    • Representing fractional numbers in memory
    • +
    • IEEE floating point representation
    • +
    + +

    Last Lecture

    +
      +
    • Demo of size and sign conversion in C: code and results posted!
    • +
    • Addition: +
        +
      • Unsigned/signed: +
          +
        • Behave the same way at the bit level
        • +
        • Interpretation of resulting bit vector (sum) may differ
        • +
        +
      • +
      • Unsigned addition -> true sum may overflow its w bits in memory (annotation with diagram of unisnged overflows, see lecture 04 to see full description of diagram) +
          +
        • If so, then actual sum = (x+y)mod2w(x + y) \mod 2^{w} (equivalent to subtracting 2w2^{w} from true sum (x+y)(x + y))
        • +
        +
      • +
      • Signed addition -> true sum may overflow its w bits in memory (annotation attached displaying diagram of negative and positive overflows, see Lecture 04 for detailed description of diagram) +
          +
        • If so then … +
            +
          • actual sum = U2Tw[(x+y)mod2w]\text{U2T}_{w}[(x + y) \mod 2^{w}]
          • +
          • true sum may be too +ve -> positive overflow OR too –ve -> negative overflow
          • +
          +
        • +
        +
      • +
      +
    • +
    • Subtraction +
        +
      • Becomes an addition where the 2nd operand is transformed into its additive inverse in two’s complement
      • +
      +
    • +
    • Multiplication: +
        +
      • Unsigned: actual product = (x×y)mod2w(x \times y) \mod 2^{w}
      • +
      • Signed: actual product = U2Tw[(x×y)mod2w]\text{U2T}_{w}[(x \times y) \mod 2^{w}]
      • +
      • Can be replaced by additions and shifts
      • +
      +
    • +
    + +

    Conclusion: the same bit pattern is interpreted differently.

    + +

    Questions

    + +
      +
    • Why are we learning this?
    • +
    • What can we do in our program when we suspect that overflow may occur?
    • +
    + +

    Demo – Looking at integer additions in C

    +
      +
    • What does the demo illustrate? +
        +
      • Unsigned addition +
          +
        • Without overflow
        • +
        • With overflow
        • +
        • Can overflow be predicted?
        • +
        +
      • +
      • Signed addition +
          +
        • Without overflow
        • +
        • With positive overflow and negative overflow
        • +
        • Can overflow be predicted?
        • +
        +
      • +
      +
    • +
    • This demo (code and results) posted on our course web site +4
    • +
    + +

    Today’s Menu

    +
      +
    • (greyed out) Representing data in memory – Most of this is review +
        +
      • (greyed out) “Under the Hood” - Von Neumann architecture
      • +
      • (greyed out) Bits and bytes in memory +
          +
        • (greyed out) How to diagram memory -> Used in this course and other references
        • +
        • (greyed out) How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • (greyed out) What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • (greyed out) Order of bytes in memory -> Endian
        • +
        +
      • +
      • (greyed out) Bit manipulation – bitwise operations +
          +
        • (greyed out) Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • (greyed out) Representing integral numbers in memory +
        +
      • (greyed out) Unsigned and signed
      • +
      • (greyed out) Converting, expanding and truncating
      • +
      • (greyed out) Arithmetic operations
      • +
      +
    • +
    • (highlighted) Representing real numbers in memory
    • +
    • (highlighted) IEEE floating point representation
    • +
    • (greyed out) Floating point in C – casting, rounding, addition, …
    • +
    + +

    We’ll illustrate what we covered today by having a demo!

    + +

    Converting a fractional decimal number into a binary number (bit vector) [R2B(X)]

    + +
      +
    • How would 346.625 (34658346 \frac{5}{8}) be represented as a binary number?
    • +
    • Expanding the subtraction method we have already seen:
    • +
    + +

    Starting number: 346.625

    + +

    Whole:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueAttempted Addition (subtraction) valueResultBinary ImplicationNote
    346-256901×281 \times 2^{8}MSb
    90-1280×270 \times 2^{7} 
    90-64261×261 \times 2^{6} 
    26-320×250 \times 2^{5} 
    26-16101×241 \times 2^{4} 
    10-821×231 \times 2^{3} 
    2-40×220 \times 2^{2} 
    2-201×211 \times 2^{1} 
    0-10×210 \times 2^{1}LSb
    + +

    Fractional:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueAttempted addition (subtraction) valueresultbinary implicationnotes
    .625-0.5.1251×211 \times 2^{-1}MSb
    .125-0.250×220 \times 2^{-2} 
    .125-0.12501×231 \times 2^{-3}LSb
    + +

    Binary representation is: 101011010.1012\text{101011010.101}_{2}

    + +

    First, last binary digit before the period are MSb, LSb respectively. +First, last binary digit after the period are also MSb, LSb respectively.

    + +

    Negative Powers of 2:

    + +
      +
    • 212^{-1} = 0.5
    • +
    • 222^{−2} = 0.25
    • +
    • 232^{−3} = 0.125
    • +
    • 242^{−4} = 0.0625
    • +
    • 252^{−5} = 0.03125
    • +
    + +

    Converting a binary number into a fractional decimal number [R2B(X)]

    + +
      +
    • How would 1011.1012\text{1011.101}_{2} be represented as a fractional decimal number?
    • +
    + +

    Review: Fractional decimal numbers

    + +

    Positional notation:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NotationValue
    did_{i}10i\text{10}^{i}
    di1d_{i-1}10i1\text{10}^{i-1}
    d2d_{2}100
    d1d_{1}10
    d0d_{0}1
    d1d_{-1}110\frac{1}{10}
    d2d_{-2}1100\frac{1}{100}
    d3d_{-3}11000\frac{1}{1000}
    djd_{-j}10j\text{10}^{-j}
    + +

    Example: 2.345

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Digit in numberNote
    2100\text{10}^{0}
    . 
    3101\text{10}^{-1}
    4102\text{10}^{-2}
    5103\text{10}^{-3}
    + +

    2.345=2×100+3×101+4×102+5×103 +2.345 = 2 \times \text{10}^{0} + 3 \times \text{10}^{−1} + 4 \times \text{10}^{−2} + 5 \times \text{10}^{−3} +

    + +

    Converting a binary number into a fractional decimal number [B2R(X)]

    + +

    Positional notation: can this be a possible encoding scheme?

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    bib_{i}2i2^{i}
    bi1b_{i-1}2i12^{i-1}
    b2b_{2}4
    b1b_{1}2
    b0b_{0}1
    b1b_{-1}12\frac{1}{2}
    b2b_{-2}14\frac{1}{4}
    b3b_{-3}18\frac{1}{8}
    bjb_{-j}2j2^{-j}
    + +

    Converting a binary number into a fractional decimal number [B2R(X)]

    + +
      +
    • How would 1011.1012\text{1011.101}_{2} be represented as a fractional decimal number?
    • +
    • Using the positional encoding scheme:
    • +
    + +

    1011.1012=(10112=1×23+1×21+1×20=1110)+(.1012=1×21+1×23=0.5+0.125=0.62510) +\begin{aligned} +&\text{1011.101}_{2} = \\ +&(\text{1011}_{2} = 1 \times 2^{3} + 1 \times 2^{1} + 1 \times 2^{0} = \text{11}_{10}) +\\ +&(\text{.101}_{2} = 1 \times 2^{-1} + 1 \times 2^{-3} = 0.5 + 0.125 = \text{0.625}_{10}) +\end{aligned} +

    + +

    Result: ____

    + +

    Negative Powers of 2

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    212^{−1}0.5
    222^{−2}0.25
    232^{−3}0.125
    242^{−4}0.0625
    252^{−5}0.03125
    262^{−6}0.015625
    272^{−7}0.0078125
    282^{−8}0.00390625
    + +

    Positional notation as encoding scheme?

    + +
      +
    • One way to answer this question is to investigate whether the encoding scheme allows for arithmetic operations
    • +
    • Let’s see: Using the positional notation as an encoding scheme produces fractional binary numbers that can be +
        +
      • added
      • +
      • multiplied by 2 by shifting left
      • +
      • divided by 2 by shifting right (unsigned)
      • +
      +
    • +
    + +

    Example:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OperandBinaryFractionMakeup
     1011.1012\text{1011.101}_{2}115811\frac{5}{8}8+2+1+12+188 + 2 + 1 + \frac{1}{2} + \frac{1}{8}
    Divide by 2101.11012\text{101.1101}_{2}513165\frac{13}{16}4+1+12+14+1164 + 1 + \frac{1}{2} + \frac{1}{4} + \frac{1}{16}
    Divide by 210.111012\text{10.11101}_{2}229322\frac{29}{32}2+12+14+18+1322 + \frac{1}{2} + \frac{1}{4} + \frac{1}{8} + \frac{1}{32}
    Multiply by 41011.1012\text{1011.101}_{2}115811\frac{5}{8}8+2+1+12+188 + 2 + 1 + \frac{1}{2} + \frac{1}{8}
    Multiply by 210111.012\text{10111.01}_{2}231423\frac{1}{4}16+4+2+1+1416 + 4 + 2 + 1 + \frac{1}{4}
    + +

    So far so good!

    + +

    Positional notation as encoding scheme?

    + +
      +
    • Advantage (so far): +
        +
      • Straightforward arithmetic: can shift to multiply and divide, convert
      • +
      +
    • +
    • Disadvantage: +
        +
      • Cannot encode all fractional numbers: +
          +
        • Can only represent numbers of the form x÷2kx \div 2^{k} (what about 15\frac{1}{5} or -34.8)
        • +
        +
      • +
      • Only one setting of binary point within the w bits -> this limits the range of possible values +
          +
        • What is this range? +
            +
          • Example: w = 32 bits and binary point located at 16th bit: +
              +
            • Whole number range: [0 … 65535]
            • +
            • Fraction range: [0 … 1 - ε]
            • +
            • + 1bits=ϵ1 - \text{bits} = \epsilon +
            • +
            • Range: [0.0 … 65535.99998]
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    Not so good anymore!

    + +

    Representing fractional numbers in memory

    + +
      +
    • Here is another possible encoding scheme: IEEE floating point representation (IEEE Standard 754)
    • +
    • Overview: +
        +
      • Binary Numerical Form: V=(–1)sM2EV = \text{(–1)}^{s} M 2^{E} +
          +
        • s – Sign bit -> determines whether number is negative or positive
        • +
        • M – Significand (or Mantissa) -> fractional part of number
        • +
        • E – Exponent
        • +
        +
      • +
      +
    • +
    • Form of bit pattern (number of items not important, focus on scale): [s|eee|fffffffffffff] +
        +
      • Most significant bit (MSb) s (similar to sign-magnitude encoding)
      • +
      • exp (e) field encodes E (but is not equal to E)
      • +
      • frac (f) field encodes M (but is not equal to M)
      • +
      +
    • +
    + +

    IEEE Floating Point Representation – Precision options

    + +
      +
    • Single precision: 32 bits ≈ 7 decimal digits, range:10±38 (in C: float) +
        +
      • In C: diagram of memory showing: +
          +
        • 1 sign bit
        • +
        • 8 exp bits
        • +
        • 23 frac bits
        • +
        +
      • +
      +
    • +
    • Double precision: 64 bits ≈ 16 decimal digits, range:10±308 (in C: double) +
        +
      • In C: diagram of memory showing: +
          +
        • 1 sign bit
        • +
        • 11 exp bits
        • +
        • 52 frac bits
        • +
        +
      • +
      +
    • +
    + +

    IEEE Floating Point Representation – Three “kinds” of values

    + +

    Numerical Form: V=(–1)sM2EV = \text{(–1)}^{s} M 2^{E}

    + +
      +
    • 1 sign bit
    • +
    • k exp bits +
        +
      • denormalized: 00…00 (all 0’s)
      • +
      • normalized: exp != 0 and exp != 11..11
      • +
      • special cases: 11…11 (all 1’s)
      • +
      +
    • +
    • +

      n frac bits

      +
    • +
    • E = exp - bias, bias = 2k112^{k-1} -1
    • +
    • M = 1 + frac
    • +
    + +

    Why is E biased? Using single precision as an example:

    + +
      +
    • exp range: [00000001 .. 11111110] and bias = 28112^{8-1} – 1
    • +
    • E range: [-126 .. 127]
    • +
    • If no bias: E range: [1 .. 254] => 212^{1} to 22542^{254}
    • +
    + +

    Why adding 1 to frac? Because number V is first normalized before it is converted.

    + +

    Review: Scientific Notation and normalization

    + +
      +
    • From Wikipedia: +
        +
      • Scientific notation is a way of expressing numbers that are too large or too small (usually would result a long string of digits) to be conveniently written in decimal form.
      • +
      • In scientific notation, nonzero numbers are written in the form
      • +
      • In normalized notation, the exponent n is chosen so that the absolute value of the significand m is at least 1 but less than 10.
      • +
      +
    • +
    • Examples: +
        +
      • A proton’s mass is 0.0000000000000000000000000016726 kg -> 1.6726×10271.6726 \times \text{10}^{−27}kg
      • +
      • Speed of light is 299,792,458 m/s -> 2.99792,458×1082.99792,458 \times \text{10}^{8} m/s
      • +
      +
    • +
    + +

    Syntax:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NotationName
    +/−sign
    d0d_{0}, d1d_{-1}, d2d_{-2}, d3d_{-3}dnd_{-n}significand
    ×times
    bbase
    exp^{exp}exponent
    + +

    Let’s try: 101011010.1012\text{101011010.101}_{2} = ____

    + +

    Summary

    + +
      +
    • Representing integral numbers (signed/unsigned) in memory: +
        +
      • Encode schemes allow for small range of values exactly
      • +
      +
    • +
    • Representing fractional numbers in memory: +
        +
      1. Positional notation (advantages and disadvantages)
      2. +
      3. IEEE floating point representation: wider range, mostly approximately
      4. +
      +
    • +
    • Overview of IEEE Floating Point representation
    • +
    • + V=(-1)s×M×2EV = \text{(-1)}^{s} \times M \times 2^{E} +
    • +
    • Precision options
    • +
    • 3 kinds: normalized, denormalized and special values
    • +
    + +

    Today’s Menu

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +18 +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/05/Lecture_05_Data_Representation_Fractional_Numbers.pdf b/_site/melody/cmpt-295/05/Lecture_05_Data_Representation_Fractional_Numbers.pdf new file mode 100644 index 0000000..44cf646 Binary files /dev/null and b/_site/melody/cmpt-295/05/Lecture_05_Data_Representation_Fractional_Numbers.pdf differ diff --git a/_site/melody/cmpt-295/05/Lecture_05_Data_Representation_Fractional_Numbers/index.html b/_site/melody/cmpt-295/05/Lecture_05_Data_Representation_Fractional_Numbers/index.html new file mode 100644 index 0000000..bc353e0 --- /dev/null +++ b/_site/melody/cmpt-295/05/Lecture_05_Data_Representation_Fractional_Numbers/index.html @@ -0,0 +1,768 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295: Unit - Data Representation

    + +

    Lecture 5

    + +
      +
    • Representing fractional numbers in memory
    • +
    • IEEE floating point representation
    • +
    + +

    Last Lecture

    +
      +
    • Demo of size and sign conversion in C: code and results posted!
    • +
    • Addition: +
        +
      • Unsigned/signed: +
          +
        • Behave the same way at the bit level
        • +
        • Interpretation of resulting bit vector (sum) may differ
        • +
        +
      • +
      • Unsigned addition -> true sum may overflow its w bits in memory (annotation with diagram of unisnged overflows, see lecture 04 to see full description of diagram) +
          +
        • If so, then actual sum = (x+y)mod2w(x + y) \mod 2^{w} (equivalent to subtracting 2w2^{w} from true sum (x+y)(x + y))
        • +
        +
      • +
      • Signed addition -> true sum may overflow its w bits in memory (annotation attached displaying diagram of negative and positive overflows, see Lecture 04 for detailed description of diagram) +
          +
        • If so then … +
            +
          • actual sum = U2Tw[(x+y)mod2w]\text{U2T}_{w}[(x + y) \mod 2^{w}]
          • +
          • true sum may be too +ve -> positive overflow OR too –ve -> negative overflow
          • +
          +
        • +
        +
      • +
      +
    • +
    • Subtraction +
        +
      • Becomes an addition where the 2nd operand is transformed into its additive inverse in two’s complement
      • +
      +
    • +
    • Multiplication: +
        +
      • Unsigned: actual product = (x×y)mod2w(x \times y) \mod 2^{w}
      • +
      • Signed: actual product = U2Tw[(x×y)mod2w]\text{U2T}_{w}[(x \times y) \mod 2^{w}]
      • +
      • Can be replaced by additions and shifts
      • +
      +
    • +
    + +

    Conclusion: the same bit pattern is interpreted differently.

    + +

    Questions

    + +
      +
    • Why are we learning this?
    • +
    • What can we do in our program when we suspect that overflow may occur?
    • +
    + +

    Demo – Looking at integer additions in C

    +
      +
    • What does the demo illustrate? +
        +
      • Unsigned addition +
          +
        • Without overflow
        • +
        • With overflow
        • +
        • Can overflow be predicted?
        • +
        +
      • +
      • Signed addition +
          +
        • Without overflow
        • +
        • With positive overflow and negative overflow
        • +
        • Can overflow be predicted?
        • +
        +
      • +
      +
    • +
    • This demo (code and results) posted on our course web site +4
    • +
    + +

    Today’s Menu

    +
      +
    • (greyed out) Representing data in memory – Most of this is review +
        +
      • (greyed out) “Under the Hood” - Von Neumann architecture
      • +
      • (greyed out) Bits and bytes in memory +
          +
        • (greyed out) How to diagram memory -> Used in this course and other references
        • +
        • (greyed out) How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • (greyed out) What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • (greyed out) Order of bytes in memory -> Endian
        • +
        +
      • +
      • (greyed out) Bit manipulation – bitwise operations +
          +
        • (greyed out) Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • (greyed out) Representing integral numbers in memory +
        +
      • (greyed out) Unsigned and signed
      • +
      • (greyed out) Converting, expanding and truncating
      • +
      • (greyed out) Arithmetic operations
      • +
      +
    • +
    • (highlighted) Representing real numbers in memory
    • +
    • (highlighted) IEEE floating point representation
    • +
    • (greyed out) Floating point in C – casting, rounding, addition, …
    • +
    + +

    We’ll illustrate what we covered today by having a demo!

    + +

    Converting a fractional decimal number into a binary number (bit vector) [R2B(X)]

    + +
      +
    • How would 346.625 (34658346 \frac{5}{8}) be represented as a binary number?
    • +
    • Expanding the subtraction method we have already seen:
    • +
    + +

    Starting number: 346.625

    + +

    Whole:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueAttempted Addition (subtraction) valueResultBinary ImplicationNote
    346-256901×281 \times 2^{8}MSb
    90-1280×270 \times 2^{7} 
    90-64261×261 \times 2^{6} 
    26-320×250 \times 2^{5} 
    26-16101×241 \times 2^{4} 
    10-821×231 \times 2^{3} 
    2-40×220 \times 2^{2} 
    2-201×211 \times 2^{1} 
    0-10×210 \times 2^{1}LSb
    + +

    Fractional:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueAttempted addition (subtraction) valueresultbinary implicationnotes
    .625-0.5.1251×211 \times 2^{-1}MSb
    .125-0.250×220 \times 2^{-2} 
    .125-0.12501×231 \times 2^{-3}LSb
    + +

    Binary representation is: 101011010.1012\text{101011010.101}_{2}

    + +

    First, last binary digit before the period are MSb, LSb respectively. +First, last binary digit after the period are also MSb, LSb respectively.

    + +

    Negative Powers of 2:

    + +
      +
    • 212^{-1} = 0.5
    • +
    • 222^{−2} = 0.25
    • +
    • 232^{−3} = 0.125
    • +
    • 242^{−4} = 0.0625
    • +
    • 252^{−5} = 0.03125
    • +
    + +

    Converting a binary number into a fractional decimal number [R2B(X)]

    + +
      +
    • How would 1011.1012\text{1011.101}_{2} be represented as a fractional decimal number?
    • +
    + +

    Review: Fractional decimal numbers

    + +

    Positional notation:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NotationValue
    did_{i}10i\text{10}^{i}
    di1d_{i-1}10i1\text{10}^{i-1}
    d2d_{2}100
    d1d_{1}10
    d0d_{0}1
    d1d_{-1}110\frac{1}{10}
    d2d_{-2}1100\frac{1}{100}
    d3d_{-3}11000\frac{1}{1000}
    djd_{-j}10j\text{10}^{-j}
    + +

    Example: 2.345

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Digit in numberNote
    2100\text{10}^{0}
    . 
    3101\text{10}^{-1}
    4102\text{10}^{-2}
    5103\text{10}^{-3}
    + +

    2.345=2×100+3×101+4×102+5×103 +2.345 = 2 \times \text{10}^{0} + 3 \times \text{10}^{−1} + 4 \times \text{10}^{−2} + 5 \times \text{10}^{−3} +

    + +

    Converting a binary number into a fractional decimal number [B2R(X)]

    + +

    Positional notation: can this be a possible encoding scheme?

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    bib_{i}2i2^{i}
    bi1b_{i-1}2i12^{i-1}
    b2b_{2}4
    b1b_{1}2
    b0b_{0}1
    b1b_{-1}12\frac{1}{2}
    b2b_{-2}14\frac{1}{4}
    b3b_{-3}18\frac{1}{8}
    bjb_{-j}2j2^{-j}
    + +

    Converting a binary number into a fractional decimal number [B2R(X)]

    + +
      +
    • How would 1011.1012\text{1011.101}_{2} be represented as a fractional decimal number?
    • +
    • Using the positional encoding scheme:
    • +
    + +

    1011.1012=(10112=1×23+1×21+1×20=1110)+(.1012=1×21+1×23=0.5+0.125=0.62510) +\begin{aligned} +&\text{1011.101}_{2} = \\ +&(\text{1011}_{2} = 1 \times 2^{3} + 1 \times 2^{1} + 1 \times 2^{0} = \text{11}_{10}) +\\ +&(\text{.101}_{2} = 1 \times 2^{-1} + 1 \times 2^{-3} = 0.5 + 0.125 = \text{0.625}_{10}) +\end{aligned} +

    + +

    Result: ____

    + +

    Negative Powers of 2

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    212^{−1}0.5
    222^{−2}0.25
    232^{−3}0.125
    242^{−4}0.0625
    252^{−5}0.03125
    262^{−6}0.015625
    272^{−7}0.0078125
    282^{−8}0.00390625
    + +

    Positional notation as encoding scheme?

    + +
      +
    • One way to answer this question is to investigate whether the encoding scheme allows for arithmetic operations
    • +
    • Let’s see: Using the positional notation as an encoding scheme produces fractional binary numbers that can be +
        +
      • added
      • +
      • multiplied by 2 by shifting left
      • +
      • divided by 2 by shifting right (unsigned)
      • +
      +
    • +
    + +

    Example:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OperandBinaryFractionMakeup
     1011.1012\text{1011.101}_{2}115811\frac{5}{8}8+2+1+12+188 + 2 + 1 + \frac{1}{2} + \frac{1}{8}
    Divide by 2101.11012\text{101.1101}_{2}513165\frac{13}{16}4+1+12+14+1164 + 1 + \frac{1}{2} + \frac{1}{4} + \frac{1}{16}
    Divide by 210.111012\text{10.11101}_{2}229322\frac{29}{32}2+12+14+18+1322 + \frac{1}{2} + \frac{1}{4} + \frac{1}{8} + \frac{1}{32}
    Multiply by 41011.1012\text{1011.101}_{2}115811\frac{5}{8}8+2+1+12+188 + 2 + 1 + \frac{1}{2} + \frac{1}{8}
    Multiply by 210111.012\text{10111.01}_{2}231423\frac{1}{4}16+4+2+1+1416 + 4 + 2 + 1 + \frac{1}{4}
    + +

    So far so good!

    + +

    Positional notation as encoding scheme?

    + +
      +
    • Advantage (so far): +
        +
      • Straightforward arithmetic: can shift to multiply and divide, convert
      • +
      +
    • +
    • Disadvantage: +
        +
      • Cannot encode all fractional numbers: +
          +
        • Can only represent numbers of the form x÷2kx \div 2^{k} (what about 15\frac{1}{5} or -34.8)
        • +
        +
      • +
      • Only one setting of binary point within the w bits -> this limits the range of possible values +
          +
        • What is this range? +
            +
          • Example: w = 32 bits and binary point located at 16th bit: +
              +
            • Whole number range: [0 … 65535]
            • +
            • Fraction range: [0 … 1 - ε]
            • +
            • + 1bits=ϵ1 - \text{bits} = \epsilon +
            • +
            • Range: [0.0 … 65535.99998]
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    Not so good anymore!

    + +

    Representing fractional numbers in memory

    + +
      +
    • Here is another possible encoding scheme: IEEE floating point representation (IEEE Standard 754)
    • +
    • Overview: +
        +
      • Binary Numerical Form: V=(–1)sM2EV = \text{(–1)}^{s} M 2^{E} +
          +
        • s – Sign bit -> determines whether number is negative or positive
        • +
        • M – Significand (or Mantissa) -> fractional part of number
        • +
        • E – Exponent
        • +
        +
      • +
      +
    • +
    • Form of bit pattern (number of items not important, focus on scale): [s|eee|fffffffffffff] +
        +
      • Most significant bit (MSb) s (similar to sign-magnitude encoding)
      • +
      • exp (e) field encodes E (but is not equal to E)
      • +
      • frac (f) field encodes M (but is not equal to M)
      • +
      +
    • +
    + +

    IEEE Floating Point Representation – Precision options

    + +
      +
    • Single precision: 32 bits ≈ 7 decimal digits, range:10±38 (in C: float) +
        +
      • In C: diagram of memory showing: +
          +
        • 1 sign bit
        • +
        • 8 exp bits
        • +
        • 23 frac bits
        • +
        +
      • +
      +
    • +
    • Double precision: 64 bits ≈ 16 decimal digits, range:10±308 (in C: double) +
        +
      • In C: diagram of memory showing: +
          +
        • 1 sign bit
        • +
        • 11 exp bits
        • +
        • 52 frac bits
        • +
        +
      • +
      +
    • +
    + +

    IEEE Floating Point Representation – Three “kinds” of values

    + +

    Numerical Form: V=(–1)sM2EV = \text{(–1)}^{s} M 2^{E}

    + +
      +
    • 1 sign bit
    • +
    • k exp bits +
        +
      • denormalized: 00…00 (all 0’s)
      • +
      • normalized: exp != 0 and exp != 11..11
      • +
      • special cases: 11…11 (all 1’s)
      • +
      +
    • +
    • +

      n frac bits

      +
    • +
    • E = exp - bias, bias = 2k112^{k-1} -1
    • +
    • M = 1 + frac
    • +
    + +

    Why is E biased? Using single precision as an example:

    + +
      +
    • exp range: [00000001 .. 11111110] and bias = 28112^{8-1} – 1
    • +
    • E range: [-126 .. 127]
    • +
    • If no bias: E range: [1 .. 254] => 212^{1} to 22542^{254}
    • +
    + +

    Why adding 1 to frac? Because number V is first normalized before it is converted.

    + +

    Review: Scientific Notation and normalization

    + +
      +
    • From Wikipedia: +
        +
      • Scientific notation is a way of expressing numbers that are too large or too small (usually would result a long string of digits) to be conveniently written in decimal form.
      • +
      • In scientific notation, nonzero numbers are written in the form
      • +
      • In normalized notation, the exponent n is chosen so that the absolute value of the significand m is at least 1 but less than 10.
      • +
      +
    • +
    • Examples: +
        +
      • A proton’s mass is 0.0000000000000000000000000016726 kg -> 1.6726×10271.6726 \times \text{10}^{−27}kg
      • +
      • Speed of light is 299,792,458 m/s -> 2.99792,458×1082.99792,458 \times \text{10}^{8} m/s
      • +
      +
    • +
    + +

    Syntax:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NotationName
    +/−sign
    d0d_{0}, d1d_{-1}, d2d_{-2}, d3d_{-3}dnd_{-n}significand
    ×times
    bbase
    exp^{exp}exponent
    + +

    Let’s try: 101011010.1012\text{101011010.101}_{2} = ____

    + +

    Summary

    + +
      +
    • Representing integral numbers (signed/unsigned) in memory: +
        +
      • Encode schemes allow for small range of values exactly
      • +
      +
    • +
    • Representing fractional numbers in memory: +
        +
      1. Positional notation (advantages and disadvantages)
      2. +
      3. IEEE floating point representation: wider range, mostly approximately
      4. +
      +
    • +
    • Overview of IEEE Floating Point representation
    • +
    • + V=(-1)s×M×2EV = \text{(-1)}^{s} \times M \times 2^{E} +
    • +
    • Precision options
    • +
    • 3 kinds: normalized, denormalized and special values
    • +
    + +

    Today’s Menu

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +18 +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/06/06.html b/_site/melody/cmpt-295/06/06.html new file mode 100644 index 0000000..710fbfe --- /dev/null +++ b/_site/melody/cmpt-295/06/06.html @@ -0,0 +1,596 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Data Representation +
        +
      • Lecture 6 – Representing fractional numbers in memory
      • +
      • IEEE floating point representation – cont’d
      • +
      +
    • +
    + +

    Have you heard of that new band “1023 Megabytes”?

    + +

    They’re pretty good, +but they don’t have a gig just yet. +😭

    + +

    Last Lecture

    + +
      +
    • Representing integral numbers in memory +
        +
      • Can encode a small range of values exactly (in 1, 2, 4, 8 bytes) +
          +
        • For example: We can represent the values -128 to 127 exactly in 1 byte using a signed char in C
        • +
        +
      • +
      +
    • +
    • Representing fractional numbers in memory +
        +
      1. Positional notation has some advantages, but also disadvantages -> so not used!
      2. +
      3. 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)
      4. +
      +
    • +
    • Overview of IEEE floating point representation +
        +
      • Precision options (float 32-bit, double 64-bit)
      • +
      • V = (-1)s x M x 2E
      • +
      • s –> sign bit
      • +
      • exp encodes E (but != E)
      • +
      • frac encodes M (but != M)
      • +
      +
    • +
    + +

    We interpret the bit vector (expressed in IEEE floating point encoding) stored in memory using this equation.

    + +

    Today’s Menu

    + +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +4 +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    IEEE Floating Point Representation Three “kinds” of values

    + +

    We interpret the bit vector +(expressed in IEEE floating point encoding) stored in memory using this equation:

    + +

    V=(1)sM2E +V = (-1)^{s} M 2^{E} +

    + +

    Bit breakdown–exp and frac interpreted as unsigned:

    + +
      +
    • s = 1 bit
    • +
    • exp = k bits +
        +
      1. If exp != 0 and exp != 11…11 (exp range: [0000001…11111110]). Equations: +
          +
        • E=expbiasE = \text{exp} - \text{bias} and bias=2k11\text{bias} = 2^{k-1} - 1
        • +
        • + M=1+fracM = 1 + \text{frac} +
        • +
        +
      2. +
      3. If exp = 00…00 (all 0’s) => denormalized. Equations: +
          +
        • E=1biasE = 1 - \text{bias} and bias=2k11\text{bias} = 2^{k-1} - 1
        • +
        • + M=fracM = \text{frac} +
        • +
        +
      4. +
      5. If exp 11…11 (all 1’s) => special cases. +
          +
        • Case 1: frac = 000…0
        • +
        • Case 2: frac != 000…0
        • +
        +
      6. +
      +
    • +
    + +

    IEEE floating point representation - normalized

    + +

    Numerical Form: V=(1)sM2EV = (–1)^{s} M 2^{E}

    + +

    Bit breakdown:

    + +
      +
    • s = 1 bit
    • +
    • exp = k bits +
        +
      • If exp != 0 and exp != 11…11 (exp range: [00000001…11111110]) => normalized. Equations: +
          +
        • E=expbiasE = \text{exp} - \text{bias} and bias=2k11\text{bias} = 2^{k-1} - 1
        • +
        • + M=1+fracM = 1 + \text{frac} +
        • +
        +
      • +
      +
    • +
    + +

    Why is E biased?

    + +

    Using single precision as an example (s = 1 bit, exp = 8 bits, frac = 23 bits):

    + +
      +
    • (exp range: [00000001 .. 11111110]) => [110...25410][1_{10}...254_{10}]
    • +
    • If E is not biased (i.e. E = exp), then E range [110...25410][1_{10} ... 254_{10}]
    • +
    • V range [212^{1}22542^{254}] = [2…2.89×1076\approx 2.89 \times 10^{76}] (so cannot express numbers < 2)
    • +
    • By biasing E (i.e. E = exp - bias), then E range: [1-127…254-127] == [-126…127] (since k = 8, bias = 28112^{8-1} - 1 = 127)
    • +
    • V range: [21262^{-126}21272^{127}] = [1.18×1038\approx 1.18 \times 10^{-38}1.7×1038\approx 1.7 \times 10^{38} (so can now express very small (and very large) numbers)
    • +
    • Why adding 1 to frac? Because the number (or value) V is first normalized before it is converted.
    • +
    + +

    Review: Scientific Notation and normalization

    + +
      +
    • From Wikipedia: +
        +
      • 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).
      • +
      • In scientific notation, nonzero numbers are written in the form +/- M × 10n
      • +
      • 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 +
          +
        • M range for base 10 => [1.0 .. 10.0 – ε ]
        • +
        • M range for base 2 => [1.0 .. 2.0 – ε ]
        • +
        +
      • +
      +
    • +
    • +

      Examples:

      + +
        +
      • A proton’s mass is 0.0000000000000000000000000016726 kg -> 1.6726×10271.6726 \times 10^{−27} kg
      • +
      • Speed of light is 299,792,458 m/s -> 2.99792,458×1082.99792,458 \times 10^{8} m/s
      • +
      +
    • +
    + +

    Syntax of normalized notation:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameNotation
    Sign+/-
    Significantd0,d1,d2,d3d_{0}, d_{-1}, d_{-2}, d_{-3}dnd_{-n}
    Baseb
    Exponentexp^{\text{exp}}
    + +
      +
    • Let’s try: 101011010.1012101011010.101_{2} -> ___
    • +
    + +

    Let’s try normalizing these fractional binary numbers!

    + +
      +
    1. + 101011010.1012101011010.101_{2} +
    2. +
    3. + 0.00000000110120.000000001101_{2} +
    4. +
    5. + 11000000111001211000000111001_{2} +
    6. +
    + +

    IEEE floating point representation

    + +
      +
    • Once V is normalized, we apply the equations +
        +
      • + V=(1)sM2E=1.010110101012×28V = (–1)^{s} M 2^{E} = 1.01011010101_{2} \times 2^{8} +
      • +
      • s = ???
      • +
      • E=expbiasE = \text{exp} - \text{bias} where bias=2k11=271=1281=127\text{bias} = 2^{k-1} - 1 = 2^{7} - 1 = 128 - 1 = 127
      • +
      • exp = E + bias = ___
      • +
      • M = 1 + frac = ___
      • +
      • s = 1 bit, exp = k bits => 8 bits, frac n bits => 23 bits
      • +
      • bit vector in memory:
      • +
      +
    • +
    + +

    Why adding 1 to frac (or subtracting 1 from M)?

    + +
      +
    • Because the number (or value) V is first normalized before it is converted. +
        +
      • As part of this normalization process, we transform our binary number such that its significand M is within the range [1.0 .. 2.0 – ε ]
      • +
      • Remember: M range for base 2 => [1.0 … 2.0 – ε]
      • +
      • This implies that M is always at least 1.0, so its integral part always has the value 1
      • +
      • So since this bit is always part of M, IEEE 754 does not explicitly save it in its bit pattern (i.e., in memory)
      • +
      • Instead, this bit is implied!
      • +
      +
    • +
    + +

    Why adding 1 to frac (or subtracting 1 from M)?

    + +

    Implying this bit has the following effects:

    + +

    We get the +leading bit +for free!

    + +
      +
    1. We save 1 bit when we convert (represent) a fractional decimal number into a bit pattern using IEEE 754 floating point representation
    2. +
    3. 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
    4. +
    + +

    Example: V=(1)sM2E=1.01011010101×28V = (–1)^{s} M 2^{E} = 1.01011010101 \times 2^{8}

    + +

    M = 1. 01011010101 => M = 1 + frac

    + +

    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

    + +

    IEEE floating point representation (single precision)

    + +
      +
    • What if the 4 bytes starting at M[0x0000] represented a fractional +decimal number (encoded as an IEEE floating point number) -> value? +single precision
    • +
    + +

    Numerical Form: V=(1)sM2EV = (–1)^{s} M 2^{E}

    + + + + + + + + + + + + + + + + + + + + + + +
    ValueNotes
    1 
    10000111k=8 bits, interpreted as unsigned
    01011010101000000000000n=23 bits, interpreted as unsigned
    + +
      +
    • exp ≠ 0 and exp ≠ 111111112 -> normalized
    • +
    • s = ___
    • +
    • E = exp – bias where bias = 2k11=271=1281=1272^{k-1} – 1 = 2^{7} – 1 = 128 – 1 = 127$
    • +
    • E = ____ - 127 =
    • +
    • M = 1 + frac = 1 + ___
    • +
    • V = ____
    • +
    + +

    Little endian memory layout:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-1 
    0x000311000011
    0x000210101101
    0x000101010000
    0x000000000000
    + +

    Let’s give it a go!

    + +
      +
    • What if the 4 bytes starting at M[0x0000] represented a fractional +decimal number (encoded as an IEEE floating point number) -> value?
    • +
    + +

    Numerical form: V=(1)sM2EV = (-1)^{s} M 2^{E}

    + +

    single precision

    + + + + + + + + + + + + + + + + + + + + + + +
    ValueNotes
    0 
    10001100k=8 bits, interpreted as unsigned
    11011011011000000000000n=23 bits, interpreted as unsigned
    + +
      +
    • exp ≠ 0 and exp ≠ 111111112 -> normalized
    • +
    • s = ____
    • +
    • E = exp - bias where bias=271=1281=127\text{bias} = 2^{7} - 1 = 128 - 1 = 127
    • +
    • E = ____ - 127 = ___
    • +
    • M = 1 + frac = 1 + ____
    • +
    • V = ____
    • +
    + +

    Little endian memory map:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-1 
    0x000301000110
    0x000201101101
    0x000110110000
    0x000000000000
    + +

    IEEE floating point representation (single precision)

    + +

    How would 47.21875 be encoded as IEEE floating point number?

    + +
      +
    1. Convert 47.28 to binary (using the positional notation R2B(X)) => +
        +
      • + 47=101111247 = 101111_{2} +
      • +
      • + .21875=.001112.21875 = .00111_{2} +
      • +
      +
    2. +
    3. Normalize binary number: +101111.00111 => 1.01111001112×251.0111100111_{2} \times 2^{5} +
        +
      • + V=(1)sM2EV = (–1)^{s} M 2^{E} +
      • +
      +
    4. +
    5. Determine … +
        +
      • s = 0
      • +
      • E = exp – bias where bias=2k11=271=1281=127\text{bias} = 2^{k-1} – 1 = 2^{7} – 1 = 128 – 1 = 127
      • +
      • exp = E + bias = 5 + 127 = 132 => U2B(132) => 10000100
      • +
      • M = 1 + frac -> frac = M - 1 => 1.011110011121=.011110011121.0111100111_{2} - 1 = .0111100111_{2}
      • +
      +
    6. +
    7. 32 bits organized in s|exp|frac: [0|10000100|01111001110000000000000}
    8. +
    9. 0x423CE000
    10. +
    + +

    IEEE floating point representation (single precision)

    + +

    How would 12345.75 be encoded as IEEE floating point number?

    + +

    V=(1)sM2E +V = (-1)^{s} M 2^{E} +

    + +
      +
    1. Convert 12345.75 to binary +
        +
      • 12345 => ____ .75 => ____
      • +
      +
    2. +
    3. Normalize binary number:
    4. +
    5. Determine … +
        +
      • s = ____
      • +
      • E = exp – bias where bias=2k11=271=1281=127\text{bias} = 2^{k-1} – 1 = 2^{7} – 1 = 128 – 1 = 127
      • +
      • exp = E + bias = ____
      • +
      • M = 1 + frac -> frac = M - 1
      • +
      +
    6. +
    7. [_|________|_______________________]
    8. +
    9. Express in hex:
    10. +
    + +

    Summary

    + +
      +
    • IEEE Floating Point Representation +
        +
      1. Denormalized
      2. +
      3. Special cases
      4. +
      5. Normalized => exp ≠ 000…0 and exp ≠ 111…1 +
          +
        • Single precision: bias = 127, exp: [1..254], E: [-126..127] => [10-38 … 1038]
        • +
        • Called “normalized” because binary numbers are normalized
        • +
        +
          +
        • Effect: “We get the leading bit for free” +
            +
          • Leading bit is always assumed (never part of bit pattern)
          • +
          +
        • +
        +
      6. +
      +
    • +
    • IEEE floating point number as encoding scheme +
        +
      • Fractional decimal number  IEEE 754 (bit pattern)
      • +
      • + V=(1)sM2EV = (–1)^{s} M 2^{E} +
          +
        • s is sign bit, M = 1 + frac, E = exp – bias, bias=2k11\text{bias} = 2^{k-1} – 1 and k is width of exp
        • +
        +
      • +
      +
    • +
    + +

    Next Lecture

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/06/06/index.html b/_site/melody/cmpt-295/06/06/index.html new file mode 100644 index 0000000..ea09fbb --- /dev/null +++ b/_site/melody/cmpt-295/06/06/index.html @@ -0,0 +1,598 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Data Representation +
        +
      • Lecture 6 – Representing fractional numbers in memory
      • +
      • IEEE floating point representation – cont’d
      • +
      +
    • +
    + +

    Have you heard of that new band “1023 Megabytes”?

    + +

    They’re pretty good, +but they don’t have a gig just yet. +😭

    + +

    Last Lecture

    + +
      +
    • Representing integral numbers in memory +
        +
      • Can encode a small range of values exactly (in 1, 2, 4, 8 bytes) +
          +
        • For example: We can represent the values -128 to 127 exactly in 1 byte using a signed char in C
        • +
        +
      • +
      +
    • +
    • Representing fractional numbers in memory +
        +
      1. Positional notation has some advantages, but also disadvantages -> so not used!
      2. +
      3. 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)
      4. +
      +
    • +
    • Overview of IEEE floating point representation +
        +
      • Precision options (float 32-bit, double 64-bit)
      • +
      • V = (-1)s x M x 2E
      • +
      • s –> sign bit
      • +
      • exp encodes E (but != E)
      • +
      • frac encodes M (but != M)
      • +
      +
    • +
    + +

    We interpret the bit vector (expressed in IEEE floating point encoding) stored in memory using this equation.

    + +

    Today’s Menu

    + +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +4 +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    IEEE Floating Point Representation Three “kinds” of values

    + +

    We interpret the bit vector +(expressed in IEEE floating point encoding) stored in memory using this equation:

    + +

    V=(1)sM2E +V = (-1)^{s} M 2^{E} +

    + +

    Bit breakdown–exp and frac interpreted as unsigned:

    + +
      +
    • s = 1 bit
    • +
    • exp = k bits +
        +
      1. If exp != 0 and exp != 11…11 (exp range: [0000001…11111110]). Equations: +
          +
        • E=expbiasE = \text{exp} - \text{bias} and bias=2k11\text{bias} = 2^{k-1} - 1
        • +
        • + M=1+fracM = 1 + \text{frac} +
        • +
        +
      2. +
      3. If exp = 00…00 (all 0’s) => denormalized. Equations: +
          +
        • E=1biasE = 1 - \text{bias} and bias=2k11\text{bias} = 2^{k-1} - 1
        • +
        • + M=fracM = \text{frac} +
        • +
        +
      4. +
      5. If exp 11…11 (all 1’s) => special cases. +
          +
        • Case 1: frac = 000…0
        • +
        • Case 2: frac != 000…0
        • +
        +
      6. +
      +
    • +
    + +

    IEEE floating point representation - normalized

    + +

    Numerical Form: V=(1)sM2EV = (–1)^{s} M 2^{E}

    + +

    Bit breakdown:

    + +
      +
    • s = 1 bit
    • +
    • exp = k bits +
        +
      • If exp != 0 and exp != 11…11 (exp range: [00000001…11111110]) => normalized. Equations: +
          +
        • E=expbiasE = \text{exp} - \text{bias} and bias=2k11\text{bias} = 2^{k-1} - 1
        • +
        • + M=1+fracM = 1 + \text{frac} +
        • +
        +
      • +
      +
    • +
    + +

    Why is E biased?

    + +

    Using single precision as an example (s = 1 bit, exp = 8 bits, frac = 23 bits):

    + +
      +
    • (exp range: [00000001 .. 11111110]) => [110...25410][1_{10}...254_{10}]
    • +
    • If E is not biased (i.e. E = exp), then E range [110...25410][1_{10} ... 254_{10}]
    • +
    • V range [212^{1}22542^{254}] = [2…2.89×1076\approx 2.89 \times 10^{76}] (so cannot express numbers < 2)
    • +
    • By biasing E (i.e. E = exp - bias), then E range: [1-127…254-127] == [-126…127] (since k = 8, bias = 28112^{8-1} - 1 = 127)
    • +
    • V range: [21262^{-126}21272^{127}] = [1.18×1038\approx 1.18 \times 10^{-38}1.7×1038\approx 1.7 \times 10^{38} (so can now express very small (and very large) numbers)
    • +
    • Why adding 1 to frac? Because the number (or value) V is first normalized before it is converted.
    • +
    + +

    Review: Scientific Notation and normalization

    + +
      +
    • From Wikipedia: +
        +
      • 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).
      • +
      • In scientific notation, nonzero numbers are written in the form +/- M × 10n
      • +
      • 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 +
          +
        • M range for base 10 => [1.0 .. 10.0 – ε ]
        • +
        • M range for base 2 => [1.0 .. 2.0 – ε ]
        • +
        +
      • +
      +
    • +
    • +

      Examples:

      + +
        +
      • A proton’s mass is 0.0000000000000000000000000016726 kg -> 1.6726×10271.6726 \times 10^{−27} kg
      • +
      • Speed of light is 299,792,458 m/s -> 2.99792,458×1082.99792,458 \times 10^{8} m/s
      • +
      +
    • +
    + +

    Syntax of normalized notation:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameNotation
    Sign+/-
    Significantd0,d1,d2,d3d_{0}, d_{-1}, d_{-2}, d_{-3}dnd_{-n}
    Baseb
    Exponentexp^{\text{exp}}
    + +
      +
    • Let’s try: 101011010.1012101011010.101_{2} -> ___
    • +
    + +

    Let’s try normalizing these fractional binary numbers!

    + +
      +
    1. + 101011010.1012101011010.101_{2} +
    2. +
    3. + 0.00000000110120.000000001101_{2} +
    4. +
    5. + 11000000111001211000000111001_{2} +
    6. +
    + +

    IEEE floating point representation

    + +
      +
    • Once V is normalized, we apply the equations +
        +
      • + V=(1)sM2E=1.010110101012×28V = (–1)^{s} M 2^{E} = 1.01011010101_{2} \times 2^{8} +
      • +
      • s = ???
      • +
      • E=expbiasE = \text{exp} - \text{bias} where bias=2k11=271=1281=127\text{bias} = 2^{k-1} - 1 = 2^{7} - 1 = 128 - 1 = 127
      • +
      • exp = E + bias = ___
      • +
      • M = 1 + frac = ___
      • +
      • s = 1 bit, exp = k bits => 8 bits, frac n bits => 23 bits
      • +
      • bit vector in memory:
      • +
      +
    • +
    + +

    Why adding 1 to frac (or subtracting 1 from M)?

    + +
      +
    • Because the number (or value) V is first normalized before it is converted. +
        +
      • As part of this normalization process, we transform our binary number such that its significand M is within the range [1.0 .. 2.0 – ε ]
      • +
      • Remember: M range for base 2 => [1.0 … 2.0 – ε]
      • +
      • This implies that M is always at least 1.0, so its integral part always has the value 1
      • +
      • So since this bit is always part of M, IEEE 754 does not explicitly save it in its bit pattern (i.e., in memory)
      • +
      • Instead, this bit is implied!
      • +
      +
    • +
    + +

    Why adding 1 to frac (or subtracting 1 from M)?

    + +

    Implying this bit has the following effects:

    + +

    We get the +leading bit +for free!

    + +
      +
    1. We save 1 bit when we convert (represent) a fractional decimal number into a bit pattern using IEEE 754 floating point representation
    2. +
    3. 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
    4. +
    + +

    Example: V=(1)sM2E=1.01011010101×28V = (–1)^{s} M 2^{E} = 1.01011010101 \times 2^{8}

    + +

    M = 1. 01011010101 => M = 1 + frac

    + +

    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

    + +

    IEEE floating point representation (single precision)

    + +
      +
    • What if the 4 bytes starting at M[0x0000] represented a fractional +decimal number (encoded as an IEEE floating point number) -> value? +single precision
    • +
    + +

    Numerical Form: V=(1)sM2EV = (–1)^{s} M 2^{E}

    + + + + + + + + + + + + + + + + + + + + + + +
    ValueNotes
    1 
    10000111k=8 bits, interpreted as unsigned
    01011010101000000000000n=23 bits, interpreted as unsigned
    + +
      +
    • exp ≠ 0 and exp ≠ 111111112 -> normalized
    • +
    • s = ___
    • +
    • E = exp – bias where bias = 2k11=271=1281=1272^{k-1} – 1 = 2^{7} – 1 = 128 – 1 = 127$
    • +
    • E = ____ - 127 =
    • +
    • M = 1 + frac = 1 + ___
    • +
    • V = ____
    • +
    + +

    Little endian memory layout:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-1 
    0x000311000011
    0x000210101101
    0x000101010000
    0x000000000000
    + +

    Let’s give it a go!

    + +
      +
    • What if the 4 bytes starting at M[0x0000] represented a fractional +decimal number (encoded as an IEEE floating point number) -> value?
    • +
    + +

    Numerical form: V=(1)sM2EV = (-1)^{s} M 2^{E}

    + +

    single precision

    + + + + + + + + + + + + + + + + + + + + + + +
    ValueNotes
    0 
    10001100k=8 bits, interpreted as unsigned
    11011011011000000000000n=23 bits, interpreted as unsigned
    + +
      +
    • exp ≠ 0 and exp ≠ 111111112 -> normalized
    • +
    • s = ____
    • +
    • E = exp - bias where bias=271=1281=127\text{bias} = 2^{7} - 1 = 128 - 1 = 127
    • +
    • E = ____ - 127 = ___
    • +
    • M = 1 + frac = 1 + ____
    • +
    • V = ____
    • +
    + +

    Little endian memory map:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-1 
    0x000301000110
    0x000201101101
    0x000110110000
    0x000000000000
    + +

    IEEE floating point representation (single precision)

    + +

    How would 47.21875 be encoded as IEEE floating point number?

    + +
      +
    1. Convert 47.28 to binary (using the positional notation R2B(X)) => +
        +
      • + 47=101111247 = 101111_{2} +
      • +
      • + .21875=.001112.21875 = .00111_{2} +
      • +
      +
    2. +
    3. Normalize binary number: +101111.00111 => 1.01111001112×251.0111100111_{2} \times 2^{5} +
        +
      • + V=(1)sM2EV = (–1)^{s} M 2^{E} +
      • +
      +
    4. +
    5. Determine … +
        +
      • s = 0
      • +
      • E = exp – bias where bias=2k11=271=1281=127\text{bias} = 2^{k-1} – 1 = 2^{7} – 1 = 128 – 1 = 127
      • +
      • exp = E + bias = 5 + 127 = 132 => U2B(132) => 10000100
      • +
      • M = 1 + frac -> frac = M - 1 => 1.011110011121=.011110011121.0111100111_{2} - 1 = .0111100111_{2}
      • +
      +
    6. +
    7. 32 bits organized in s|exp|frac: [0|10000100|01111001110000000000000}
    8. +
    9. 0x423CE000
    10. +
    + +

    IEEE floating point representation (single precision)

    + +

    How would 12345.75 be encoded as IEEE floating point number?

    + +

    V=(1)sM2E +V = (-1)^{s} M 2^{E} +

    + +
      +
    1. Convert 12345.75 to binary +
        +
      • 12345 => ____ .75 => ____
      • +
      +
    2. +
    3. Normalize binary number:
    4. +
    5. Determine … +
        +
      • s = ____
      • +
      • E = exp – bias where bias=2k11=271=1281=127\text{bias} = 2^{k-1} – 1 = 2^{7} – 1 = 128 – 1 = 127
      • +
      • exp = E + bias = ____
      • +
      • M = 1 + frac -> frac = M - 1
      • +
      +
    6. +
    7. [_|________|_______________________]
    8. +
    9. Express in hex:
    10. +
    + +

    Summary

    + +
      +
    • IEEE Floating Point Representation +
        +
      1. Denormalized
      2. +
      3. Special cases
      4. +
      5. Normalized => exp ≠ 000…0 and exp ≠ 111…1 +
          +
        • Single precision: bias = 127, exp: [1..254], E: [-126..127] => [10-38 … 1038]
        • +
        • Called “normalized” because binary numbers are normalized
        • +
        +
          +
        • Effect: “We get the leading bit for free” +
            +
          • Leading bit is always assumed (never part of bit pattern)
          • +
          +
        • +
        +
      6. +
      +
    • +
    • IEEE floating point number as encoding scheme +
        +
      • Fractional decimal number  IEEE 754 (bit pattern)
      • +
      • + V=(1)sM2EV = (–1)^{s} M 2^{E} +
          +
        • s is sign bit, M = 1 + frac, E = exp – bias, bias=2k11\text{bias} = 2^{k-1} – 1 and k is width of exp
        • +
        +
      • +
      +
    • +
    + +

    Next Lecture

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/06/Lecture_06_Data_Representation_Fractional_Numbers.pdf b/_site/melody/cmpt-295/06/Lecture_06_Data_Representation_Fractional_Numbers.pdf new file mode 100644 index 0000000..a1c8ac4 Binary files /dev/null and b/_site/melody/cmpt-295/06/Lecture_06_Data_Representation_Fractional_Numbers.pdf differ diff --git a/_site/melody/cmpt-295/07/07.html b/_site/melody/cmpt-295/07/07.html new file mode 100644 index 0000000..981e1ac --- /dev/null +++ b/_site/melody/cmpt-295/07/07.html @@ -0,0 +1,733 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295 - Unit - Data Representation

    + +

    Lecture 7

    + +
      +
    • Representing fractional numbers in memory
    • +
    • IEEE floating point representation, their arithmetic operations and float in C
    • +
    + +

    Last Lecture

    + +
      +
    • IEEE floating point representation +
        +
      1. Normalized => exp ≠ 000…0 and exp ≠ 111…1 +
          +
        • Single precision: bias = 127, exp: [1..254], E: [-126..127] => [10-38 … 1038]
        • +
        • Called “normalized” because binary numbers are normalized as part of the conversion process
        • +
        +
          +
        • Effect: “We get the leading bit for free” => leading bit is always +assumed (never part of bit pattern) + * Conversion: IEEE floating point number as encoding scheme
        • +
        • + Fractional decimal numberIEEE 754 (bit pattern)\text{Fractional decimal number} \leftrightarrow \text{IEEE 754 (bit pattern)} +
            +
          • V=(–1)sM2EV = \text{(–1)}^{s} M 2^{E}; s is sign bit, M=1+fracM = 1 + \text{frac}, E=exp–biasE = \text{exp} – \text{bias}, bias=2k11\text{bias} = 2^{k-1} – 1 and k is width of exp
          • +
          +
        • +
        +
      2. +
      3. Denormalized
      4. +
      5. Special cases
      6. +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture +
          +
        • Bits and bytes in memory
        • +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    IEEE floating point representation (single precision)

    + +

    How would 47.28 be encoded as IEEE floating point number?

    + +
      +
    1. Convert 47.28 to binary (using the positional notation R2B(X)) => +
        +
      • 47 = 1011112\text{101111}_{2}
      • +
      • .28 = .010001111010111000012.\overline{01000111101011100001}_{2}
      • +
      • Also expressed as: .28 = .01000111101011100001010001111010111000010100011110101110000101...2\text{.01000111101011100001010001111010111000010100011110101110000101...}_{2}
      • +
      +
    2. +
    3. Normalize binary number: +
        +
      • 101111.01000111101011100001010001111010111000010100011110101110000101...2(×20)\text{101111.01000111101011100001010001111010111000010100011110101110000101...}_{2} (\times 2^{0}) becomes:
      • +
      • + 1.011110100011110101110000101000111101011100001010001111010111000...2×25\text{1.011110100011110101110000101000111101011100001010001111010111000...}_{2} \times 2^{5} +
      • +
      +
    4. +
    + +

    A diagram of the above number not fitting inside the frac portion of a signed number due to its width.

    + +

    Rounding

    + +

    This selection is done +by looking at the bit +pattern around the +rounding position.

    + +
      +
    • First, identify bit at rounding position
    • +
    • Then select which kind of rounding we must perform: +
        +
      1. Round up +
          +
        • When the value of the bits to the right of the bit at rounding position is > half the worth of the bit at rounding position
        • +
        • We “round” up by adding 1 to the bit at rounding position
        • +
        +
      2. +
      3. Round down +
          +
        • When the value of the bits to the right of the bit at rounding position is < half the worth of the bit at rounding position
        • +
        • We “round” down by discarding the bits to the right of the bit at rounding +position
        • +
        +
      4. +
      5. Round to “even number” +
          +
        • When the value of the bits to the right of the bit at rounding position is exactly half the worth of bit at rounding position, i.e., when these bits are 100…02
        • +
        • We “round” such that the bit at the rounding position becomes 0
        • +
        +
          +
        • If the bit at rounding position is 1 => then we “round to even number” by “rounding up” i.e., by adding 1
        • +
        • If the bit at rounding position is already 0 => then we “round to even number” by “rounding down” i.e., by discarding the bits to the right of the bit at rounding position
        • +
        +
      6. +
      +
    • +
    + +

    Rounding (and error)

    + +

    Example: rounding position -> round to nearest 1/4 (2 bits right of binary point)

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueBinaryRoundedActionRounded Value
    23322 \frac{3}{32}10.000112\text{10.00011}_{2}10.002\text{10.00}_{2}(<1/2—down)2
    23162 \frac{3}{16}10.001102\text{10.00110}_210.012\text{10.01}_{2}(>1/2—up)2142 \frac{1}{4}
    2782 \frac{7}{8}10.111002\text{10.11100}_{2}11.002\text{11.00}_{2}(1/2—up to even)3
    2582 \frac{5}{8}10.101002\text{10.10100}_{2}10.102\text{10.10}_{2}(1/2—down to even)2122 \frac{1}{2}
    + +
      +
    • Explain value of a bit versus worth of a bit
    • +
    • value of a bit: ____
    • +
    • worth of a bit: ____
    • +
    + +

    Back to IEEE floating point representation

    + +

    In the process of converting fractional decimal numbers to IEEE floating point numbers (i.e., bit patterns in fixed-size +memory), we apply these same rounding rules …

    + +

    Using the same numbers in our example:

    + +

    Imagine that the 4th bit in the binary column is our 23rd bit of the frac => rounding position. +And the 5th bit is the 24th bit.

    + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueBinary
    23322 \frac{3}{32}10.000112\text{10.00011}_{2}
    23162 \frac{3}{16}10.001102\text{10.00110}_{2}
    2782 \frac{7}{8}10.111002\text{10.11100}_{2}
    2582 \frac{5}{8}10.101002\text{10.10100}_{2}
    + +

    Homework – Let’s practice converting and rounding!

    + +

    How would 346.62 be encoded as IEEE floating point number (single precision) in memory?

    + +

    Also, can you compute the minimum value of the error introduced by the rounding process since 346.62 can only be approximated when encoded as an IEEE floating point representation

    + +

    Denormalized values

    + +

    Equations:

    + +
      +
    • + V=(–1)sM2EV = \text{(–1)}^{s} M 2^{E} +
    • +
    • + E=1–biasE = 1 – \text{bias} +
    • +
    • + bias=2k11\text{bias} = 2^{k-1} – 1 +
    • +
    • + M=fracM = \text{frac} +
    • +
    + +

    Example:

    + +
      +
    • Condition: exp=00000000\text{exp} = \text{00000000} (single precision)
    • +
    • Denormalized Values: V=(–1)sM2E=+/0.frac×2126V = \text{(–1)}^{s} M 2^{E} = +/− 0.\text{frac} \times 2^{−126} +
        +
      • Case 1: frac=0000\text{frac} = \text{000…0} -> +0 and –0
      • +
      • Case 2: frac0000\text{frac} \neq \text{000…0} -> numbers closest to 0.0 (equally spaced)
      • +
      +
    • +
    + +

    Smallest:

    + + + + + + + + + + + + + + + + +
    sexpfrac
    00000000000000000000000000000001
    + +V=(–1)sM2E=0.00000000000000000000001×21261.4×1045V = \text{(–1)}^{s} M 2^{E} = \text{0.00000000000000000000001} \times 2^{-126} \approxeq 1.4 \times \text{10}^{-45} + +

    Largest:

    + + + + + + + + + + + + + + + + +
    sexpfrac
    00000000011111111111111111111111
    + +V=(–1)sM2E=0.11111111111111111111111×21261.18×1038V = \text{(–1)}^{s} M 2^{E} = \text{0.11111111111111111111111} \times 2^{-126} \approxeq 1.18 \times 10^{-38} + +

    Special values

    + +

    Condition: exp = 111…1

    + +
      +
    • Case 1: frac = 000…0 +
        +
      • Represents value \infty(infinity)
      • +
      • Operation that overflows
      • +
      • Both positive and negative
      • +
      • E.g.: +
          +
        • + 1.00.0=1.00.0=+\frac{1.0}{0.0} = \frac{−1.0}{−0.0} = +\infty +
        • +
        • + 1.0/0.0=1.0/−0.0 = −\infty +
        • +
        +
      • +
      +
    • +
    • Case 2: frac ≠ 000…0 +
        +
      • Not-a-Number (NaN)
      • +
      • Represents case when no numeric value can be determined
      • +
      • e.g.: +
          +
        • + 1\sqrt{–1} +
        • +
        • + \infty - \infty +
        • +
        • + ×0\infty \times 0 +
        • +
        +
      • +
      • NaN propagates other NaN: e.g., NaN+x=NaN\text{NaN} + x = \text{NaN}
      • +
      +
    • +
    + +

    Axis of all floating point values

    + +
      +
    • s = 1 bit
    • +
    • exp = k bits +
        +
      1. if exp0\text{exp} \neq 0 and exp11...11\text{exp} \neq \text{11...11} => Normalized
      2. +
      3. if exp=00...00\text{exp} = \text{00...00} (all 0’s) => Denormalized
      4. +
      5. if exp=11...11\text{exp} = \text{11...11} (all 1’s) => Special Cases +n bits
      6. +
      +
    • +
    • frac = n bits
    • +
    + +

    Starting with the lowest to the highest, what values can be produced:

    + +
      +
    • NaN
    • +
    • + -\infty +
    • +
    • -Normalized
    • +
    • -Denormalized
    • +
    • -0
    • +
    • +0
    • +
    • +Denormalized
    • +
    • +Normalized
    • +
    • + ++\infty +
    • +
    • NaN
    • +
    + +

    What if floating point represented with 8 bits

    + +

    Equations:

    + +
      +
    • + V=(-1)sM2EV = \text{(-1)}^{s} M 2^{E} +
    • +
    • Denormalized: +
        +
      • + E=1biasE = 1 - \text{bias} +
      • +
      • + bias=2k11\text{bias} = 2^{k-1} - 1 +
      • +
      • + M=fracM = \text{frac} +
      • +
      +
    • +
    • Normalized: +
        +
      • + E=expbiasE = \text{exp} - \text{bias} +
      • +
      • + bias=2k11\text{bias} = 2^{k-1} - 1 +
      • +
      • + M=1+fracM = 1 + \text{frac} +
      • +
      +
    • +
    + +

    Annotation: To get a feel for all possible values expressible using IEEE like conversion, we use a small w. Here, instead of w = 32, we use w = 8. +This way, we can enumerate all values.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    sexpfracEValueTypeNotes
    00000000-60Denormalized 
    00000001-618×164=1512\frac{1}{8}\times\frac{1}{64} = \frac{1}{512}Denormalizedclosest to zero
    00000010-628×164=2512\frac{2}{8}\times\frac{1}{64} = \frac{2}{512}Denormalized 
    00000111-668×164=6512\frac{6}{8}\times\frac{1}{64} = \frac{6}{512}Denormalized 
    00000111-678×164=7512\frac{7}{8}\times\frac{1}{64} = \frac{7}{512}Denormalizedlargest denom.
    00001000-688×164=8512\frac{8}{8}\times\frac{1}{64} = \frac{8}{512}Normalizedsmallest nom.
    00001001-698×164=9512\frac{9}{8}\times\frac{1}{64} = \frac{9}{512}Normalized 
    00110110-1148×12=1416\frac{14}{8}\times\frac{1}{2} = \frac{14}{16}Normalized 
    00110111-1158×12=1516\frac{15}{8}\times\frac{1}{2} = \frac{15}{16}Normalizedclosest to one below
    00111000088×1=1\frac{8}{8}\times 1 = 1Normalized 
    00111001098×1=98\frac{9}{8}\times 1 = \frac{9}{8}Normalizedclosest to one above
    001110100108×1=108\frac{10}{8}\times 1 = \frac{10}{8}Normalized 
    011101107148×128=224\frac{14}{8}\times 128 = 224Normalized 
    011101117158×128=240\frac{15}{8}\times 128 = 240Normalizedlargest nom
    01111000n/a\inftyNaN 
    + +

    Conversion in C

    + +
      +
    • Casting between int, float, and double changes bit pattern
    • +
    • double/float to int +
        +
      • Truncates fractional part
      • +
      +
    • +
    • int to float +
        +
      • Exact conversion, as long as frac (obtained when the int is normalized) fits in 23 bits
      • +
      • Will round according to rounding rules
      • +
      +
    • +
    • int to double +
        +
      • Exact conversion, as long as frac (obtained when the int is normalized) fits in 52 bits
      • +
      • Will round according to rounding rules
      • +
      +
    • +
    + +

    Demo - C code

    + +
      +
    • Conversion – Observe the change in bit pattern +
        +
      • int to float
      • +
      • float to int
      • +
      +
    • +
    • Addition
    • +
    • Associativity – For floating point numbers f1, f2 and f3: +
        +
      • Is it always true that (f1 + f2) + f3 = f1 + (f2 + f3)?
      • +
      • Is it always true that (f1 * f2) * f3 = f1 * (f2 * f3)?
      • +
      +
    • +
    • Rounding – Effect of errors caused by rounding
    • +
    + +

    Floating point arithmetic

    + +
      +
    • x +f y = Round(x + y)
    • +
    • x *f y = Round(x * y)
    • +
    • Basic idea: +
        +
      • First compute true result
      • +
      • Make it fit into desired precision +
          +
        • Possibly overflow if exponent too large
        • +
        • Possibly round to fit into frac
        • +
        +
      • +
      +
    • +
    + +

    Summary

    + +
      +
    • Most fractional decimal numbers cannot be exactly encoded using IEEE floating point representation -> rounding
    • +
    • Denormalized values +
        +
      • Condition: exp = 0000…0
      • +
      • 0 <= denormalized values < 1, equidistant because all have same 2E2^{E}
      • +
      +
    • +
    • Special values +
        +
      • Condition: exp = 1111…1 +
          +
        • Case 1: frac=000...0=\text{frac} = \text{000...0} = \infty
        • +
        • Case 2: frac000...0=NaN\text{frac} \neq \text{000...0} = \text{NaN}
        • +
        +
      • +
      +
    • +
    • Impact on C +
        +
      • Conversion/casting, rounding
      • +
      • Arithmetic operators: +
          +
        • Behaviour not the same as for real arithmetic => violates associativity
        • +
        +
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point data & operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/07/07/index.html b/_site/melody/cmpt-295/07/07/index.html new file mode 100644 index 0000000..888f529 --- /dev/null +++ b/_site/melody/cmpt-295/07/07/index.html @@ -0,0 +1,735 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 - Unit - Data Representation

    + +

    Lecture 7

    + +
      +
    • Representing fractional numbers in memory
    • +
    • IEEE floating point representation, their arithmetic operations and float in C
    • +
    + +

    Last Lecture

    + +
      +
    • IEEE floating point representation +
        +
      1. Normalized => exp ≠ 000…0 and exp ≠ 111…1 +
          +
        • Single precision: bias = 127, exp: [1..254], E: [-126..127] => [10-38 … 1038]
        • +
        • Called “normalized” because binary numbers are normalized as part of the conversion process
        • +
        +
          +
        • Effect: “We get the leading bit for free” => leading bit is always +assumed (never part of bit pattern) + * Conversion: IEEE floating point number as encoding scheme
        • +
        • + Fractional decimal numberIEEE 754 (bit pattern)\text{Fractional decimal number} \leftrightarrow \text{IEEE 754 (bit pattern)} +
            +
          • V=(–1)sM2EV = \text{(–1)}^{s} M 2^{E}; s is sign bit, M=1+fracM = 1 + \text{frac}, E=exp–biasE = \text{exp} – \text{bias}, bias=2k11\text{bias} = 2^{k-1} – 1 and k is width of exp
          • +
          +
        • +
        +
      2. +
      3. Denormalized
      4. +
      5. Special cases
      6. +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture +
          +
        • Bits and bytes in memory
        • +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    IEEE floating point representation (single precision)

    + +

    How would 47.28 be encoded as IEEE floating point number?

    + +
      +
    1. Convert 47.28 to binary (using the positional notation R2B(X)) => +
        +
      • 47 = 1011112\text{101111}_{2}
      • +
      • .28 = .010001111010111000012.\overline{01000111101011100001}_{2}
      • +
      • Also expressed as: .28 = .01000111101011100001010001111010111000010100011110101110000101...2\text{.01000111101011100001010001111010111000010100011110101110000101...}_{2}
      • +
      +
    2. +
    3. Normalize binary number: +
        +
      • 101111.01000111101011100001010001111010111000010100011110101110000101...2(×20)\text{101111.01000111101011100001010001111010111000010100011110101110000101...}_{2} (\times 2^{0}) becomes:
      • +
      • + 1.011110100011110101110000101000111101011100001010001111010111000...2×25\text{1.011110100011110101110000101000111101011100001010001111010111000...}_{2} \times 2^{5} +
      • +
      +
    4. +
    + +

    A diagram of the above number not fitting inside the frac portion of a signed number due to its width.

    + +

    Rounding

    + +

    This selection is done +by looking at the bit +pattern around the +rounding position.

    + +
      +
    • First, identify bit at rounding position
    • +
    • Then select which kind of rounding we must perform: +
        +
      1. Round up +
          +
        • When the value of the bits to the right of the bit at rounding position is > half the worth of the bit at rounding position
        • +
        • We “round” up by adding 1 to the bit at rounding position
        • +
        +
      2. +
      3. Round down +
          +
        • When the value of the bits to the right of the bit at rounding position is < half the worth of the bit at rounding position
        • +
        • We “round” down by discarding the bits to the right of the bit at rounding +position
        • +
        +
      4. +
      5. Round to “even number” +
          +
        • When the value of the bits to the right of the bit at rounding position is exactly half the worth of bit at rounding position, i.e., when these bits are 100…02
        • +
        • We “round” such that the bit at the rounding position becomes 0
        • +
        +
          +
        • If the bit at rounding position is 1 => then we “round to even number” by “rounding up” i.e., by adding 1
        • +
        • If the bit at rounding position is already 0 => then we “round to even number” by “rounding down” i.e., by discarding the bits to the right of the bit at rounding position
        • +
        +
      6. +
      +
    • +
    + +

    Rounding (and error)

    + +

    Example: rounding position -> round to nearest 1/4 (2 bits right of binary point)

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueBinaryRoundedActionRounded Value
    23322 \frac{3}{32}10.000112\text{10.00011}_{2}10.002\text{10.00}_{2}(<1/2—down)2
    23162 \frac{3}{16}10.001102\text{10.00110}_210.012\text{10.01}_{2}(>1/2—up)2142 \frac{1}{4}
    2782 \frac{7}{8}10.111002\text{10.11100}_{2}11.002\text{11.00}_{2}(1/2—up to even)3
    2582 \frac{5}{8}10.101002\text{10.10100}_{2}10.102\text{10.10}_{2}(1/2—down to even)2122 \frac{1}{2}
    + +
      +
    • Explain value of a bit versus worth of a bit
    • +
    • value of a bit: ____
    • +
    • worth of a bit: ____
    • +
    + +

    Back to IEEE floating point representation

    + +

    In the process of converting fractional decimal numbers to IEEE floating point numbers (i.e., bit patterns in fixed-size +memory), we apply these same rounding rules …

    + +

    Using the same numbers in our example:

    + +

    Imagine that the 4th bit in the binary column is our 23rd bit of the frac => rounding position. +And the 5th bit is the 24th bit.

    + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueBinary
    23322 \frac{3}{32}10.000112\text{10.00011}_{2}
    23162 \frac{3}{16}10.001102\text{10.00110}_{2}
    2782 \frac{7}{8}10.111002\text{10.11100}_{2}
    2582 \frac{5}{8}10.101002\text{10.10100}_{2}
    + +

    Homework – Let’s practice converting and rounding!

    + +

    How would 346.62 be encoded as IEEE floating point number (single precision) in memory?

    + +

    Also, can you compute the minimum value of the error introduced by the rounding process since 346.62 can only be approximated when encoded as an IEEE floating point representation

    + +

    Denormalized values

    + +

    Equations:

    + +
      +
    • + V=(–1)sM2EV = \text{(–1)}^{s} M 2^{E} +
    • +
    • + E=1–biasE = 1 – \text{bias} +
    • +
    • + bias=2k11\text{bias} = 2^{k-1} – 1 +
    • +
    • + M=fracM = \text{frac} +
    • +
    + +

    Example:

    + +
      +
    • Condition: exp=00000000\text{exp} = \text{00000000} (single precision)
    • +
    • Denormalized Values: V=(–1)sM2E=+/0.frac×2126V = \text{(–1)}^{s} M 2^{E} = +/− 0.\text{frac} \times 2^{−126} +
        +
      • Case 1: frac=0000\text{frac} = \text{000…0} -> +0 and –0
      • +
      • Case 2: frac0000\text{frac} \neq \text{000…0} -> numbers closest to 0.0 (equally spaced)
      • +
      +
    • +
    + +

    Smallest:

    + + + + + + + + + + + + + + + + +
    sexpfrac
    00000000000000000000000000000001
    + +V=(–1)sM2E=0.00000000000000000000001×21261.4×1045V = \text{(–1)}^{s} M 2^{E} = \text{0.00000000000000000000001} \times 2^{-126} \approxeq 1.4 \times \text{10}^{-45} + +

    Largest:

    + + + + + + + + + + + + + + + + +
    sexpfrac
    00000000011111111111111111111111
    + +V=(–1)sM2E=0.11111111111111111111111×21261.18×1038V = \text{(–1)}^{s} M 2^{E} = \text{0.11111111111111111111111} \times 2^{-126} \approxeq 1.18 \times 10^{-38} + +

    Special values

    + +

    Condition: exp = 111…1

    + +
      +
    • Case 1: frac = 000…0 +
        +
      • Represents value \infty(infinity)
      • +
      • Operation that overflows
      • +
      • Both positive and negative
      • +
      • E.g.: +
          +
        • + 1.00.0=1.00.0=+\frac{1.0}{0.0} = \frac{−1.0}{−0.0} = +\infty +
        • +
        • + 1.0/0.0=1.0/−0.0 = −\infty +
        • +
        +
      • +
      +
    • +
    • Case 2: frac ≠ 000…0 +
        +
      • Not-a-Number (NaN)
      • +
      • Represents case when no numeric value can be determined
      • +
      • e.g.: +
          +
        • + 1\sqrt{–1} +
        • +
        • + \infty - \infty +
        • +
        • + ×0\infty \times 0 +
        • +
        +
      • +
      • NaN propagates other NaN: e.g., NaN+x=NaN\text{NaN} + x = \text{NaN}
      • +
      +
    • +
    + +

    Axis of all floating point values

    + +
      +
    • s = 1 bit
    • +
    • exp = k bits +
        +
      1. if exp0\text{exp} \neq 0 and exp11...11\text{exp} \neq \text{11...11} => Normalized
      2. +
      3. if exp=00...00\text{exp} = \text{00...00} (all 0’s) => Denormalized
      4. +
      5. if exp=11...11\text{exp} = \text{11...11} (all 1’s) => Special Cases +n bits
      6. +
      +
    • +
    • frac = n bits
    • +
    + +

    Starting with the lowest to the highest, what values can be produced:

    + +
      +
    • NaN
    • +
    • + -\infty +
    • +
    • -Normalized
    • +
    • -Denormalized
    • +
    • -0
    • +
    • +0
    • +
    • +Denormalized
    • +
    • +Normalized
    • +
    • + ++\infty +
    • +
    • NaN
    • +
    + +

    What if floating point represented with 8 bits

    + +

    Equations:

    + +
      +
    • + V=(-1)sM2EV = \text{(-1)}^{s} M 2^{E} +
    • +
    • Denormalized: +
        +
      • + E=1biasE = 1 - \text{bias} +
      • +
      • + bias=2k11\text{bias} = 2^{k-1} - 1 +
      • +
      • + M=fracM = \text{frac} +
      • +
      +
    • +
    • Normalized: +
        +
      • + E=expbiasE = \text{exp} - \text{bias} +
      • +
      • + bias=2k11\text{bias} = 2^{k-1} - 1 +
      • +
      • + M=1+fracM = 1 + \text{frac} +
      • +
      +
    • +
    + +

    Annotation: To get a feel for all possible values expressible using IEEE like conversion, we use a small w. Here, instead of w = 32, we use w = 8. +This way, we can enumerate all values.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    sexpfracEValueTypeNotes
    00000000-60Denormalized 
    00000001-618×164=1512\frac{1}{8}\times\frac{1}{64} = \frac{1}{512}Denormalizedclosest to zero
    00000010-628×164=2512\frac{2}{8}\times\frac{1}{64} = \frac{2}{512}Denormalized 
    00000111-668×164=6512\frac{6}{8}\times\frac{1}{64} = \frac{6}{512}Denormalized 
    00000111-678×164=7512\frac{7}{8}\times\frac{1}{64} = \frac{7}{512}Denormalizedlargest denom.
    00001000-688×164=8512\frac{8}{8}\times\frac{1}{64} = \frac{8}{512}Normalizedsmallest nom.
    00001001-698×164=9512\frac{9}{8}\times\frac{1}{64} = \frac{9}{512}Normalized 
    00110110-1148×12=1416\frac{14}{8}\times\frac{1}{2} = \frac{14}{16}Normalized 
    00110111-1158×12=1516\frac{15}{8}\times\frac{1}{2} = \frac{15}{16}Normalizedclosest to one below
    00111000088×1=1\frac{8}{8}\times 1 = 1Normalized 
    00111001098×1=98\frac{9}{8}\times 1 = \frac{9}{8}Normalizedclosest to one above
    001110100108×1=108\frac{10}{8}\times 1 = \frac{10}{8}Normalized 
    011101107148×128=224\frac{14}{8}\times 128 = 224Normalized 
    011101117158×128=240\frac{15}{8}\times 128 = 240Normalizedlargest nom
    01111000n/a\inftyNaN 
    + +

    Conversion in C

    + +
      +
    • Casting between int, float, and double changes bit pattern
    • +
    • double/float to int +
        +
      • Truncates fractional part
      • +
      +
    • +
    • int to float +
        +
      • Exact conversion, as long as frac (obtained when the int is normalized) fits in 23 bits
      • +
      • Will round according to rounding rules
      • +
      +
    • +
    • int to double +
        +
      • Exact conversion, as long as frac (obtained when the int is normalized) fits in 52 bits
      • +
      • Will round according to rounding rules
      • +
      +
    • +
    + +

    Demo - C code

    + +
      +
    • Conversion – Observe the change in bit pattern +
        +
      • int to float
      • +
      • float to int
      • +
      +
    • +
    • Addition
    • +
    • Associativity – For floating point numbers f1, f2 and f3: +
        +
      • Is it always true that (f1 + f2) + f3 = f1 + (f2 + f3)?
      • +
      • Is it always true that (f1 * f2) * f3 = f1 * (f2 * f3)?
      • +
      +
    • +
    • Rounding – Effect of errors caused by rounding
    • +
    + +

    Floating point arithmetic

    + +
      +
    • x +f y = Round(x + y)
    • +
    • x *f y = Round(x * y)
    • +
    • Basic idea: +
        +
      • First compute true result
      • +
      • Make it fit into desired precision +
          +
        • Possibly overflow if exponent too large
        • +
        • Possibly round to fit into frac
        • +
        +
      • +
      +
    • +
    + +

    Summary

    + +
      +
    • Most fractional decimal numbers cannot be exactly encoded using IEEE floating point representation -> rounding
    • +
    • Denormalized values +
        +
      • Condition: exp = 0000…0
      • +
      • 0 <= denormalized values < 1, equidistant because all have same 2E2^{E}
      • +
      +
    • +
    • Special values +
        +
      • Condition: exp = 1111…1 +
          +
        • Case 1: frac=000...0=\text{frac} = \text{000...0} = \infty
        • +
        • Case 2: frac000...0=NaN\text{frac} \neq \text{000...0} = \text{NaN}
        • +
        +
      • +
      +
    • +
    • Impact on C +
        +
      • Conversion/casting, rounding
      • +
      • Arithmetic operators: +
          +
        • Behaviour not the same as for real arithmetic => violates associativity
        • +
        +
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point data & operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/07/Lecture_07_Data_Representation_Fractional_Numbers_Rounding_Arithmetic_Operations.pdf b/_site/melody/cmpt-295/07/Lecture_07_Data_Representation_Fractional_Numbers_Rounding_Arithmetic_Operations.pdf new file mode 100644 index 0000000..c5ca9c9 Binary files /dev/null and b/_site/melody/cmpt-295/07/Lecture_07_Data_Representation_Fractional_Numbers_Rounding_Arithmetic_Operations.pdf differ diff --git a/_site/melody/cmpt-295/08/08.html b/_site/melody/cmpt-295/08/08.html new file mode 100644 index 0000000..1ca4615 --- /dev/null +++ b/_site/melody/cmpt-295/08/08.html @@ -0,0 +1,274 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295: Unit - Machine-Level Programming

    + +
      +
    • Lecture 8 – Introduction
    • +
    • Compilation process: C -> assembly code -> machine level code
    • +
    + +

    Last Lecture

    +
      +
    • Most fractional decimal numbers cannot be exactly encoded using IEEE floating point representation -> rounding
    • +
    • Denormalized values +
        +
      • Condition: exp = 0000…0
      • +
      • 0 <= denormalized values < 1, equidistant because all have same 2E
      • +
      +
    • +
    • Special values +
        +
      • Condition: exp = 1111…1 +
          +
        • Case 1: frac=000...0=\text{frac} = \text{000...0} = \infty
        • +
        • Case 2: frac000...0=NaN\text{frac} \neq \text{000...0} = \text{NaN}
        • +
        +
      • +
      +
    • +
    • Impact on C +
        +
      • Conversion/casting, rounding
      • +
      • Arithmetic operators: +
          +
        • Behaviour not the same as for real arithmetic => violates associativity
        • +
        +
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point data & operations
    • +
    + +

    What could these 32 bits represent?

    + +

    What kind of information could they encode?

    + +
    +00011000111011001000001101001000
    +
    + +

    Answer:

    + +
      +
    • Aside from characters, integers or floating point numbers, etc…
    • +
    • Review: We saw that all modern computers, designed based on the von Neumann architecture, store their programs in memory +
        +
      • Data and instructions of our C program are in main memory together (but in different locations)
      • +
      +
    • +
    • So, these bits could represent code, for example: +
        +
      • Assembly code: sub $0x18, %rsp
      • +
      • Machine code: 48 83 ec 18
      • +
      +
    • +
    + +

    C program in memory?

    + +

    We have just spent a few lectures looking at how our data +can be represented as a series of 0’s and I’s, now …

    + +
      +
    1. Question: How does our C program end up being represented as a series of 0’s and 1’s (i.e., as machine code)?
    2. +
    3. Question: Then, how does our C program (once it is represented as a series of 0’s and 1’s) end up being stored in +memory?
    4. +
    5. Question: Then, how does our C program (once it is represented as a series of 0’s and 1’s and it is stored in memory) end up being executed by the microprocessor (CPU)?
    6. +
    + +

    Demo – C program: sum_store.c

    + +
      +
    1. Question: How does our C program end up being represented as a series of 0’s and 1’s (i.e., as machine code)?
    2. +
    + +

    Let’s answer these questions with a demo

    + +

    Turning C into machine code - gcc

    + +

    The Big Picture (transcriber’s note: each step depends on the last; this is implied in the diagram):

    + +
      +
    • C program (.c) -> sum_store.c +
        +
      • C Preprocessor: gcc -E sum_store.c > sum_store.i
      • +
      +
    • +
    • Pre-processed Source (.i) -> sum_store.i +
        +
      • C Compiler +
          +
        • gcc -Og -S sum_store.i, or
        • +
        • gcc -Og -S sum_store.c
        • +
        +
      • +
      +
    • +
    • Assembly Program (.s) -> sum_store.s +
        +
      • Assumbler: gcc -g -c main.s sum_sotre.s
      • +
      +
    • +
    • Object (.o) -> sum_store.o +
        +
      • Linker: gcc -o ss main.o sum_store.o
      • +
      +
    • +
    • Executable -> ss +
        +
      • Loader: ./ss 5 6.
      • +
      • Disassemlber: objdumb -d ss (gdb/ddd debugger). This will write a disassembled object file which is the only one which is a mix between plain text and data.
      • +
      +
    • +
    • ISA - Instruction Set Architecture
    • +
    • Computer Executes It +
        +
      • CPU
      • +
      • Memory
      • +
      +
    • +
    + +

    Snapshot of compiled code

    + +
      +
    • C code: *dest = sum; +
        +
      • Store sum in memory designated by pointer dest
      • +
      +
    • +
    • Assembly code: movq %rax, (%rbx) +
        +
      • Move an 8-byte value to memory +
          +
        • Quad words in x86-64 parlance
        • +
        +
      • +
      • Operands: +
          +
        • sum: register %rax
        • +
        • dest: register %rbx
        • +
        • *dest: memory M[%rbx]
        • +
        +
      • +
      +
    • +
    • Machine code (1’s and 0’s): 0x40059e: 48 89 03 +
        +
      • 3-byte instruction
      • +
      • 48 stored at address 0x40059e
      • +
      • 89 stored at address 0x40059f
      • +
      • 03 stored at address ____
      • +
      +
    • +
    + +

    Fetch-Execute Cycle

    + +

    Terms:

    + +
    +
    PC - program counter
    +
    Defn: register containing address of instruction of ss that is currently executing
    +
    IR - instruction register
    +
    Defn: register containing copy of instruction of ss that is currently executing
    +
    + +

    Question: How does our C program (once it is represented as a series of 0’s and 1’s and it is stored in memory) end up being executed by the microprocessor (CPU)?

    + +

    Answer: The microprocessor executes the machine code version of our C program by executing the following simple loop:

    + +

    DO FOREVER:

    + +
      +
    • fetch next instruction from memory into CPU
    • +
    • update the program counter
    • +
    • decode the instruction
    • +
    • execute the instruction
    • +
    + +

    Summary

    + +
      +
    • Review: von Neumann architecture +
        +
      • Data and code are both stored in memory during program execution
      • +
      +
    • +
    + +
      +
    1. Question: How does our C program end up being represented as a series of 0’s and 1’s (i.e., as machine code)? +
        +
      • Compiler: C program -> assembly code -> machine level code
      • +
      • gcc: 1) C preprocessor, 2) C compiler, 3) assembler, 4) linker
      • +
      +
    2. +
    3. Question: How does our C program (once it is represented as a series of 0’s and 1’s) end up being stored in memory? +
        +
      • When C program is executed (e.g. from our demo: ./ss 5 6 )
      • +
      +
    4. +
    5. Question: How does our C program (once it is represented as a series of 0’s and 1’s and it is stored in memory) end up being executed by the microprocessor (CPU)? +
        +
      • CPU executes C program by looping through the fetch-execute cycle
      • +
      +
    6. +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/08/08/index.html b/_site/melody/cmpt-295/08/08/index.html new file mode 100644 index 0000000..075de60 --- /dev/null +++ b/_site/melody/cmpt-295/08/08/index.html @@ -0,0 +1,276 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295: Unit - Machine-Level Programming

    + +
      +
    • Lecture 8 – Introduction
    • +
    • Compilation process: C -> assembly code -> machine level code
    • +
    + +

    Last Lecture

    +
      +
    • Most fractional decimal numbers cannot be exactly encoded using IEEE floating point representation -> rounding
    • +
    • Denormalized values +
        +
      • Condition: exp = 0000…0
      • +
      • 0 <= denormalized values < 1, equidistant because all have same 2E
      • +
      +
    • +
    • Special values +
        +
      • Condition: exp = 1111…1 +
          +
        • Case 1: frac=000...0=\text{frac} = \text{000...0} = \infty
        • +
        • Case 2: frac000...0=NaN\text{frac} \neq \text{000...0} = \text{NaN}
        • +
        +
      • +
      +
    • +
    • Impact on C +
        +
      • Conversion/casting, rounding
      • +
      • Arithmetic operators: +
          +
        • Behaviour not the same as for real arithmetic => violates associativity
        • +
        +
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point data & operations
    • +
    + +

    What could these 32 bits represent?

    + +

    What kind of information could they encode?

    + +
    +00011000111011001000001101001000
    +
    + +

    Answer:

    + +
      +
    • Aside from characters, integers or floating point numbers, etc…
    • +
    • Review: We saw that all modern computers, designed based on the von Neumann architecture, store their programs in memory +
        +
      • Data and instructions of our C program are in main memory together (but in different locations)
      • +
      +
    • +
    • So, these bits could represent code, for example: +
        +
      • Assembly code: sub $0x18, %rsp
      • +
      • Machine code: 48 83 ec 18
      • +
      +
    • +
    + +

    C program in memory?

    + +

    We have just spent a few lectures looking at how our data +can be represented as a series of 0’s and I’s, now …

    + +
      +
    1. Question: How does our C program end up being represented as a series of 0’s and 1’s (i.e., as machine code)?
    2. +
    3. Question: Then, how does our C program (once it is represented as a series of 0’s and 1’s) end up being stored in +memory?
    4. +
    5. Question: Then, how does our C program (once it is represented as a series of 0’s and 1’s and it is stored in memory) end up being executed by the microprocessor (CPU)?
    6. +
    + +

    Demo – C program: sum_store.c

    + +
      +
    1. Question: How does our C program end up being represented as a series of 0’s and 1’s (i.e., as machine code)?
    2. +
    + +

    Let’s answer these questions with a demo

    + +

    Turning C into machine code - gcc

    + +

    The Big Picture (transcriber’s note: each step depends on the last; this is implied in the diagram):

    + +
      +
    • C program (.c) -> sum_store.c +
        +
      • C Preprocessor: gcc -E sum_store.c > sum_store.i
      • +
      +
    • +
    • Pre-processed Source (.i) -> sum_store.i +
        +
      • C Compiler +
          +
        • gcc -Og -S sum_store.i, or
        • +
        • gcc -Og -S sum_store.c
        • +
        +
      • +
      +
    • +
    • Assembly Program (.s) -> sum_store.s +
        +
      • Assumbler: gcc -g -c main.s sum_sotre.s
      • +
      +
    • +
    • Object (.o) -> sum_store.o +
        +
      • Linker: gcc -o ss main.o sum_store.o
      • +
      +
    • +
    • Executable -> ss +
        +
      • Loader: ./ss 5 6.
      • +
      • Disassemlber: objdumb -d ss (gdb/ddd debugger). This will write a disassembled object file which is the only one which is a mix between plain text and data.
      • +
      +
    • +
    • ISA - Instruction Set Architecture
    • +
    • Computer Executes It +
        +
      • CPU
      • +
      • Memory
      • +
      +
    • +
    + +

    Snapshot of compiled code

    + +
      +
    • C code: *dest = sum; +
        +
      • Store sum in memory designated by pointer dest
      • +
      +
    • +
    • Assembly code: movq %rax, (%rbx) +
        +
      • Move an 8-byte value to memory +
          +
        • Quad words in x86-64 parlance
        • +
        +
      • +
      • Operands: +
          +
        • sum: register %rax
        • +
        • dest: register %rbx
        • +
        • *dest: memory M[%rbx]
        • +
        +
      • +
      +
    • +
    • Machine code (1’s and 0’s): 0x40059e: 48 89 03 +
        +
      • 3-byte instruction
      • +
      • 48 stored at address 0x40059e
      • +
      • 89 stored at address 0x40059f
      • +
      • 03 stored at address 0x4005a0
      • +
      +
    • +
    + +

    Fetch-Execute Cycle

    + +

    Terms:

    + +
    +
    PC - program counter
    +
    Defn: register containing address of instruction of ss that is currently executing
    +
    IR - instruction register
    +
    Defn: register containing copy of instruction of ss that is currently executing
    +
    + +

    Question: How does our C program (once it is represented as a series of 0’s and 1’s and it is stored in memory) end up being executed by the microprocessor (CPU)?

    + +

    Answer: The microprocessor executes the machine code version of our C program by executing the following simple loop:

    + +

    DO FOREVER:

    + +
      +
    • fetch next instruction from memory into CPU
    • +
    • update the program counter
    • +
    • decode the instruction
    • +
    • execute the instruction
    • +
    + +

    Summary

    + +
      +
    • Review: von Neumann architecture +
        +
      • Data and code are both stored in memory during program execution
      • +
      +
    • +
    + +
      +
    1. Question: How does our C program end up being represented as a series of 0’s and 1’s (i.e., as machine code)? +
        +
      • Compiler: C program -> assembly code -> machine level code
      • +
      • gcc: 1) C preprocessor, 2) C compiler, 3) assembler, 4) linker
      • +
      +
    2. +
    3. Question: How does our C program (once it is represented as a series of 0’s and 1’s) end up being stored in memory? +
        +
      • When C program is executed (e.g. from our demo: ./ss 5 6 )
      • +
      +
    4. +
    5. Question: How does our C program (once it is represented as a series of 0’s and 1’s and it is stored in memory) end up being executed by the microprocessor (CPU)? +
        +
      • CPU executes C program by looping through the fetch-execute cycle
      • +
      +
    6. +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/08/Lecture_08_Machine_Level_Prog_Intro.pdf b/_site/melody/cmpt-295/08/Lecture_08_Machine_Level_Prog_Intro.pdf new file mode 100644 index 0000000..22f7182 Binary files /dev/null and b/_site/melody/cmpt-295/08/Lecture_08_Machine_Level_Prog_Intro.pdf differ diff --git a/_site/melody/cmpt-295/09/09.html b/_site/melody/cmpt-295/09/09.html new file mode 100644 index 0000000..e2003a1 --- /dev/null +++ b/_site/melody/cmpt-295/09/09.html @@ -0,0 +1,783 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295: Unit - Machine-Level Programming

    + +

    Lecture 9 – Assembly language basics: Data, move operation

    + +

    Last Lecture

    + +
      +
    • Review: von Neumann architecture
    • +
    • Data and code are both stored in memory during program execution
    • +
    + +
      +
    1. Question: How does our C program end up being represented as a +series of 0’s and 1’s (i.e., as machine code)? +
        +
      • Compiler: C program -> assembly code -> machine level code
      • +
      • gcc: 1) C preprocessor, 2) C compiler, 3) assembler, 4) linker
      • +
      +
    2. +
    3. Question: How does our C program (once it is represented as a series of +0’s and 1’s) end up being stored in memory? Annotation: Loader +
        +
      • When C program is executed (e.g. from our demo: ./ss 5 6 )
      • +
      +
    4. +
    5. Question: How does our C program (once it is represented as a series of +0’s and 1’s and it is stored in memory) end up being executed by the +microprocessor (CPU)? +
        +
      • CPU executes C program by looping through the fetch-execute cycle
      • +
      +
    6. +
    + +

    Summary - Turning C into machine level code - gcc

    + +

    The big picture:

    + +

    Annotation: Lab 1

    + +
      +
    • C program (.c) -> sum_store.c +
        +
      • C Preprocessor: gcc -g sum_store.c > sum_store.i (Annotation: sum_store.c, sum_store.i are text) +
          +
        • Expands the header(s) found at the top of the C program by including their content into the C program.
        • +
        +
      • +
      +
    • +
    • Preprocessed source (.i) -> sum_store.i +
        +
      • C Compiler: gcc -Og -S sum_store.i OR gcc -Og -S sum_store.c +
          +
        • Compiled the C program into an assembly lanaugage program.
        • +
        +
      • +
      +
    • +
    • Assembly program (.s) -> sum_store.s +
        +
      • Assembler: gcc -g -c main.s sum_stoore.s (Annotation: main.s, sum_store.s are text) +
          +
        • Assembles the assembly language program into an object file (series of 0’s and 1’s) which is a combination of machine language instructions, data and info needed to place all of this properly into memory.
        • +
        +
      • +
      +
    • +
    • Object (.o) -> sum_store.o +
        +
      • Linker: gcc -o ss main.o sum_store.o (Annotation: main.o, sum_store.o are binary) +
          +
        • Combines independently assembled machine language programs with library routines into an executable file.
        • +
        +
      • +
      +
    • +
    • Executable: ss +
        +
      • Loader: ./ss 5 6 +
          +
        • Loads machine code (from files) into proper memory locations for execution by CPU.
        • +
        +
      • +
      • (optional) Disassembler, gdb/ddd debugger: objdump -d ss +
          +
        • disassembled object file is text and binary
        • +
        +
      • +
      +
    • +
    • Instruction Set Architecture (ISA)
    • +
    • Computer executes it. +
        +
      • CPU
      • +
      • Memory
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    Programming in C versus in x86-64 assembly language

    + +

    When programming in C, we can …

    + +
      +
    • Store/retrieve data into/from memory, i.e. variables
    • +
    • Perform calculations on data +
        +
      • e.g., arithmetic, logic, shift
      • +
      +
    • +
    • Transfer control: decide what part of the program to execute +next based on some condition +
        +
      • e.g., if-else, loop, function call
      • +
      +
    • +
    + +

    When programming in assembly language, we can do the +same things, however …

    + +

    Programming in x86-64 assembly

    + +
      +
    • … with assembly language (and machine code), parts of +the microprocessor state are visible to assembly +programmers that normally are hidden from C programmers
    • +
    • As assembly programmers, we now have access to …
    • +
    + +

    Testing a diagram idea

    + +

    Done with MathML:

    + +

    CPU=[Registers,Condition Codes,PC]CPUDataMemoryCPUAddressesMemoryCPUinstructionsMemory +\text{CPU} = [\text{Registers}, \text{Condition Codes}, \text{PC}] \\ +\text{CPU} \xleftrightarrow{Data}{} \text{Memory} \\ +\text{CPU} \xrightarrow{Addresses}{} \text{Memory} \\ +\text{CPU} \xleftarrow{instructions}{} \text{Memory} +

    + +

    Description: CPU contains registers, condition codes and PC. +CPU has a right arrow pointing to memory labled “addresses”. +Memory has a left arrow pointing to CPU labled “instructions”. +CPU and Memory have a two sides arrow pointing to eachother with the label “data”.

    + +

    Hum … Why are we learning assembly language?

    + +

    (empty slide outside of title)

    + +

    x86-64 Assembly Language - Data

    + +
      +
    • Integral numbers not stored in variables but in registers +
        +
      • Distinction between different integer size: 1, 2, 4 and 8 bytes
      • +
      +
    • +
    • Addresses not stored in pointer variables but in registers +
        +
      • Size: 8 bytes
      • +
      • Treated as integral numbers
      • +
      +
    • +
    • Floating point numbers stored in different registers than integral values +
        +
      • Distinction between different floating point numbers: 4 and 8 bytes
      • +
      +
    • +
    • No aggregate types such as arrays or structures
    • +
    + +

    x86-64 Assembly Language – Data

    + +

    Integer Registers

    + +
      +
    • Storage locations in CPU -> fastest storage
    • +
    • 16 registers are used explicitly – must name them in assembly code (annotation: %eax)
    • +
    • Some registers are used implicitly +
        +
      • e.g., PC, FLAGS
      • +
      +
    • +
    • Each register is 64 bits in size, but we can refer to its: +
        +
      • first byte LSB (8 bits),
      • +
      • first 2 bytes (16 bits),
      • +
      • first 4 bytes (32 bits),
      • +
      • or to all of its 8 bytes (64 bits)
      • +
      +
    • +
    + +

    Integer Registers table (note, the blank first column of the “8-bit” column is intentional):

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    64-bit (quad)32-bit (double)16-bit (word)8-bit (byte)8-bit (byte)Transcriber’s Note
    63..031..015..0 7..0 
    raxeaxax al 
    rbxebxbx bl 
    rcxecxcx cl 
    rdxedxdx dl 
    rsiesisi sil 
    rdiedidi dil 
    rbpebpbp bpl 
    rspespsp spl 
         All cells above are pink and all cells below are yellow. I don’t know why.
    r8r8dr8w r8b 
    r9r9dr9w r9b 
    r10r10dr10w r10b 
    r11r11dr11w r11b 
    r12r12dr12w r12b 
    r13r13dr13w r13b 
    r14r14dr14w r14b 
    r15r15dr15w r15b 
    + +

    About these integer registers!

    + +

    Transcriber’s note: 6 slides are compressed into this transcription.

    + +
      +
    • MSb 63..LSb 0 (%al, %dil, %rl2b)
    • +
    • 31..LSb 0 (%ax, %di, %rl2w)
    • +
    • 15..LSb 0 (%eax, %edi, %rl2d)
    • +
    • 7..LSb 0 (%rax, %rdi, %rl2)
    • +
    + +

    If I want 8 bits worth of data, then I can use register names such as %al or %dil or %r12b

    + +

    If I want 16 bits worth of data, then I can use register names such as %ax or %di or %r12w

    + +

    If I want 32 bits worth of data, then I can use register names such as %eax or %edi or %r12d

    + +

    If I want 64 bits worth of data, then I can use register names such as %rax or %rdi or %r12

    + +

    Remember that for all 16 registers …

    + +

    Let’s use the +register associated +with the names +%rax, %eax, %ax +and %al +as an example:

    + +
      +
    • %rax, %eax, %ax and %al all refer to the same register
    • +
    • However… +
        +
      • Each refer to a different section of this register
      • +
      • %rax refers to all 64 bits of this register
      • +
      • %eax refers to only 32 bits of this register +
          +
        • the LS 32 bits of it -> bit 0 to bit 31
        • +
        +
      • +
      • %ax refers to only 16 bits of this register +
          +
        • the LS 16 bits of it -> bit 0 to bit 15
        • +
        +
      • +
      • %al refers to only 8 bits of this register +
          +
        • the LS 8 bits of it -> bit 0 to bit 7
        • +
        +
      • +
      +
    • +
    + +

    x86-64 Assembly Language - Instructions

    + +
      +
    • “2 operand” assembly language
    • +
    • x86-64 functionally complete -> i.e., it is “Turing complete”. There are 3 classes of instructions. +
        +
      1. Memory reference => Data transfer instructions—Transfer data between memory and registers +
          +
        • Load data from memory into register
        • +
        • Store register data into memory
        • +
        • Move data from one register to another
        • +
        +
      2. +
      3. Arithmetic and logical => Data manipulation instructions—Perform calculations on register data +
          +
        • e.g., arithmetic, logic, shift
        • +
        +
      4. +
      5. Branch and jump => Program control instructions—Transfer control +
          +
        • Unconditional jumps to/from functions
        • +
        • Unconditional/conditional branches
        • +
        +
      6. +
      +
    • +
    + +

    Move data – mov*

    + +

    Size designators:

    + +
      +
    • ...q for long/64-bit
    • +
    • ...l for int/32-bit
    • +
    • ...w for short/16-bit
    • +
    • ...b for char/8-bit
    • +
    + +

    Annotation: Homework: movl $0xFF4150AC, %eax (both $0x... and %eax are 32 bits, notice the size designation: l)

    + +
      +
    1. Memory reference => Data transfer instructions
    2. +
    + +
      +
    • Transfer data between memory and registers
    • +
    • Syntax: mov* Source, Destination +
        +
      • Diagram: movq SourceDestination\text{movq}\space\text{Source}\rightarrow\text{Destination}
      • +
      +
    • +
    • Example: movq %rdi, %rax +
        +
      • Diagram: movq %rdi%rax\text{movq}\space\text{\%rdi}\rightarrow\text{\%rax}
      • +
      +
    • +
    • Allowed moves: +
        +
      • From register to register (Move)
      • +
      • From memory to register (Load)
      • +
      • From register to memory (Store)
      • +
      +
    • +
    • Conditional move (cmov*) +
        +
      • Same as above, but based on result of comparison
      • +
      +
    • +
    + +

    Demo – Swap Function

    + +
      +
    • Problem: Let’s swap the contents of two variables
    • +
    • For now, we need to know that +
        +
      • Argument 1 of function swap(…) -> saved in %rdi
      • +
      • Argument 2 of function swap(…) -> saved in %rsi
      • +
      +
    • +
    + +

    Demo – Swap Function

    + +

    C code:

    + +
    void swap(long *xp, long *yp)
    +{
    +  long L1 = *xp;
    +  long L2 = *yp;
    +  *xp = L2;
    +  *yp = L1;
    +  return;
    +}
    +
    + +

    Annotation: Because xp and yp contain an address of 64-bits, %rdi & %rsi are used to hold the value of xp and yp, respectively.

    + +

    Assembly code:

    + +
    wrap: # xp -> %rdi, yp -> %rsi
    +  movq    (%rdi), %rax # L1 = *xp; parenthasies indicate "indirect"
    +  movq    (%rsi), %rdx # L2 = *yp
    +  movq    %rdx, (%rdi) # *xp = L2
    +  movq    %rax, (%rsa) # *yp = L1
    +  ret
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterAddress
    %rdi0x0020
    %rsi0x0000
    %rax123
    %rdx456
    + +

    Remember this is a compressed view of memory.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressValue
    0x0020(deleted)123 (annotation, inserted)456
    0x0018 
    0x0010 
    0x0008 
    0x0000(deleted)456 (annotation, inserted)123
    + +

    Operand Combinations for mov*

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SourceDestAssembly (instruction (space) source, dest)in C
    ImmediateRegistermovq $0x4,%raxresult=0x4;
    ImmediateMemorymovq $-147,(%rax)*result=-147;
    RegisterRegistermovq %rax,%rdxvar=result;
    RegisterMemorymovq %rax,(%rdx)*var1=result;
    MemoryRegistermovq (%rax),%rdxvar1=*result;
    + +

    Cannot do memory-memory transfer with a single mov* instruction

    + +

    Homework 2

    + +
      +
    • Since we cannot do memory-memory transfer with a single mov* instruction … +
        +
      • Can you write a little x86-64 assembly program that transfers +data stored at address 0x0000 to address 0x0018?
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegistersValue
    %rdi 
    %rsi 
    %rax 
    %rdx 
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressValue
    0x0020 
    0x0018 
    0x0010 
    0x0008 
    0x00006
    + +

    Summary

    + +
      +
    • As x86-64 assembly s/w dev., we now get to see more of the microprocessor +(CPU) state: PC, registers, condition codes
    • +
    • x86-64 assembly language – Data +
        +
      • 16 integer registers of 1, 2, 4 or 8 bytes + memory address of 8 bytes
      • +
      • Floating point registers of 4 or 8 bytes
      • +
      • No aggregate types such as arrays or structures
      • +
      +
    • +
    • x86-64 assembly language – Instructions +
        +
      • mov* instruction family +
          +
        • From register to register
        • +
        • From memory to register
        • +
        • From register to memory
        • +
        +
      • +
      • Memory addressing modes
      • +
      • Cannot do memory-memory transfer with a single mov* instruction
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • (selected) Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/09/09/index.html b/_site/melody/cmpt-295/09/09/index.html new file mode 100644 index 0000000..f526d10 --- /dev/null +++ b/_site/melody/cmpt-295/09/09/index.html @@ -0,0 +1,785 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295: Unit - Machine-Level Programming

    + +

    Lecture 9 – Assembly language basics: Data, move operation

    + +

    Last Lecture

    + +
      +
    • Review: von Neumann architecture
    • +
    • Data and code are both stored in memory during program execution
    • +
    + +
      +
    1. Question: How does our C program end up being represented as a +series of 0’s and 1’s (i.e., as machine code)? +
        +
      • Compiler: C program -> assembly code -> machine level code
      • +
      • gcc: 1) C preprocessor, 2) C compiler, 3) assembler, 4) linker
      • +
      +
    2. +
    3. Question: How does our C program (once it is represented as a series of +0’s and 1’s) end up being stored in memory? Annotation: Loader +
        +
      • When C program is executed (e.g. from our demo: ./ss 5 6 )
      • +
      +
    4. +
    5. Question: How does our C program (once it is represented as a series of +0’s and 1’s and it is stored in memory) end up being executed by the +microprocessor (CPU)? +
        +
      • CPU executes C program by looping through the fetch-execute cycle
      • +
      +
    6. +
    + +

    Summary - Turning C into machine level code - gcc

    + +

    The big picture:

    + +

    Annotation: Lab 1

    + +
      +
    • C program (.c) -> sum_store.c +
        +
      • C Preprocessor: gcc -g sum_store.c > sum_store.i (Annotation: sum_store.c, sum_store.i are text) +
          +
        • Expands the header(s) found at the top of the C program by including their content into the C program.
        • +
        +
      • +
      +
    • +
    • Preprocessed source (.i) -> sum_store.i +
        +
      • C Compiler: gcc -Og -S sum_store.i OR gcc -Og -S sum_store.c +
          +
        • Compiled the C program into an assembly lanaugage program.
        • +
        +
      • +
      +
    • +
    • Assembly program (.s) -> sum_store.s +
        +
      • Assembler: gcc -g -c main.s sum_stoore.s (Annotation: main.s, sum_store.s are text) +
          +
        • Assembles the assembly language program into an object file (series of 0’s and 1’s) which is a combination of machine language instructions, data and info needed to place all of this properly into memory.
        • +
        +
      • +
      +
    • +
    • Object (.o) -> sum_store.o +
        +
      • Linker: gcc -o ss main.o sum_store.o (Annotation: main.o, sum_store.o are binary) +
          +
        • Combines independently assembled machine language programs with library routines into an executable file.
        • +
        +
      • +
      +
    • +
    • Executable: ss +
        +
      • Loader: ./ss 5 6 +
          +
        • Loads machine code (from files) into proper memory locations for execution by CPU.
        • +
        +
      • +
      • (optional) Disassembler, gdb/ddd debugger: objdump -d ss +
          +
        • disassembled object file is text and binary
        • +
        +
      • +
      +
    • +
    • Instruction Set Architecture (ISA)
    • +
    • Computer executes it. +
        +
      • CPU
      • +
      • Memory
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    Programming in C versus in x86-64 assembly language

    + +

    When programming in C, we can …

    + +
      +
    • Store/retrieve data into/from memory, i.e. variables
    • +
    • Perform calculations on data +
        +
      • e.g., arithmetic, logic, shift
      • +
      +
    • +
    • Transfer control: decide what part of the program to execute +next based on some condition +
        +
      • e.g., if-else, loop, function call
      • +
      +
    • +
    + +

    When programming in assembly language, we can do the +same things, however …

    + +

    Programming in x86-64 assembly

    + +
      +
    • … with assembly language (and machine code), parts of +the microprocessor state are visible to assembly +programmers that normally are hidden from C programmers
    • +
    • As assembly programmers, we now have access to …
    • +
    + +

    Testing a diagram idea

    + +

    Done with MathML:

    + +

    CPU=[Registers,Condition Codes,PC]CPUDataMemoryCPUAddressesMemoryCPUinstructionsMemory +\text{CPU} = [\text{Registers}, \text{Condition Codes}, \text{PC}] \\ +\text{CPU} \xleftrightarrow{Data}{} \text{Memory} \\ +\text{CPU} \xrightarrow{Addresses}{} \text{Memory} \\ +\text{CPU} \xleftarrow{instructions}{} \text{Memory} +

    + +

    Description: CPU contains registers, condition codes and PC. +CPU has a right arrow pointing to memory labled “addresses”. +Memory has a left arrow pointing to CPU labled “instructions”. +CPU and Memory have a two sides arrow pointing to eachother with the label “data”.

    + +

    Hum … Why are we learning assembly language?

    + +

    (empty slide outside of title)

    + +

    x86-64 Assembly Language - Data

    + +
      +
    • Integral numbers not stored in variables but in registers +
        +
      • Distinction between different integer size: 1, 2, 4 and 8 bytes
      • +
      +
    • +
    • Addresses not stored in pointer variables but in registers +
        +
      • Size: 8 bytes
      • +
      • Treated as integral numbers
      • +
      +
    • +
    • Floating point numbers stored in different registers than integral values +
        +
      • Distinction between different floating point numbers: 4 and 8 bytes
      • +
      +
    • +
    • No aggregate types such as arrays or structures
    • +
    + +

    x86-64 Assembly Language – Data

    + +

    Integer Registers

    + +
      +
    • Storage locations in CPU -> fastest storage
    • +
    • 16 registers are used explicitly – must name them in assembly code (annotation: %eax)
    • +
    • Some registers are used implicitly +
        +
      • e.g., PC, FLAGS
      • +
      +
    • +
    • Each register is 64 bits in size, but we can refer to its: +
        +
      • first byte LSB (8 bits),
      • +
      • first 2 bytes (16 bits),
      • +
      • first 4 bytes (32 bits),
      • +
      • or to all of its 8 bytes (64 bits)
      • +
      +
    • +
    + +

    Integer Registers table (note, the blank first column of the “8-bit” column is intentional):

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    64-bit (quad)32-bit (double)16-bit (word)8-bit (byte)8-bit (byte)Transcriber’s Note
    63..031..015..0 7..0 
    raxeaxax al 
    rbxebxbx bl 
    rcxecxcx cl 
    rdxedxdx dl 
    rsiesisi sil 
    rdiedidi dil 
    rbpebpbp bpl 
    rspespsp spl 
         All cells above are pink and all cells below are yellow. I don’t know why.
    r8r8dr8w r8b 
    r9r9dr9w r9b 
    r10r10dr10w r10b 
    r11r11dr11w r11b 
    r12r12dr12w r12b 
    r13r13dr13w r13b 
    r14r14dr14w r14b 
    r15r15dr15w r15b 
    + +

    About these integer registers!

    + +

    Transcriber’s note: 6 slides are compressed into this transcription.

    + +
      +
    • MSb 63..LSb 0 (%al, %dil, %rl2b)
    • +
    • 31..LSb 0 (%ax, %di, %rl2w)
    • +
    • 15..LSb 0 (%eax, %edi, %rl2d)
    • +
    • 7..LSb 0 (%rax, %rdi, %rl2)
    • +
    + +

    If I want 8 bits worth of data, then I can use register names such as %al or %dil or %r12b

    + +

    If I want 16 bits worth of data, then I can use register names such as %ax or %di or %r12w

    + +

    If I want 32 bits worth of data, then I can use register names such as %eax or %edi or %r12d

    + +

    If I want 64 bits worth of data, then I can use register names such as %rax or %rdi or %r12

    + +

    Remember that for all 16 registers …

    + +

    Let’s use the +register associated +with the names +%rax, %eax, %ax +and %al +as an example:

    + +
      +
    • %rax, %eax, %ax and %al all refer to the same register
    • +
    • However… +
        +
      • Each refer to a different section of this register
      • +
      • %rax refers to all 64 bits of this register
      • +
      • %eax refers to only 32 bits of this register +
          +
        • the LS 32 bits of it -> bit 0 to bit 31
        • +
        +
      • +
      • %ax refers to only 16 bits of this register +
          +
        • the LS 16 bits of it -> bit 0 to bit 15
        • +
        +
      • +
      • %al refers to only 8 bits of this register +
          +
        • the LS 8 bits of it -> bit 0 to bit 7
        • +
        +
      • +
      +
    • +
    + +

    x86-64 Assembly Language - Instructions

    + +
      +
    • “2 operand” assembly language
    • +
    • x86-64 functionally complete -> i.e., it is “Turing complete”. There are 3 classes of instructions. +
        +
      1. Memory reference => Data transfer instructions—Transfer data between memory and registers +
          +
        • Load data from memory into register
        • +
        • Store register data into memory
        • +
        • Move data from one register to another
        • +
        +
      2. +
      3. Arithmetic and logical => Data manipulation instructions—Perform calculations on register data +
          +
        • e.g., arithmetic, logic, shift
        • +
        +
      4. +
      5. Branch and jump => Program control instructions—Transfer control +
          +
        • Unconditional jumps to/from functions
        • +
        • Unconditional/conditional branches
        • +
        +
      6. +
      +
    • +
    + +

    Move data – mov*

    + +

    Size designators:

    + +
      +
    • ...q for long/64-bit
    • +
    • ...l for int/32-bit
    • +
    • ...w for short/16-bit
    • +
    • ...b for char/8-bit
    • +
    + +

    Annotation: Homework: movl $0xFF4150AC, %eax (both $0x... and %eax are 32 bits, notice the size designation: l)

    + +
      +
    1. Memory reference => Data transfer instructions
    2. +
    + +
      +
    • Transfer data between memory and registers
    • +
    • Syntax: mov* Source, Destination +
        +
      • Diagram: movq SourceDestination\text{movq}\space\text{Source}\rightarrow\text{Destination}
      • +
      +
    • +
    • Example: movq %rdi, %rax +
        +
      • Diagram: movq %rdi%rax\text{movq}\space\text{\%rdi}\rightarrow\text{\%rax}
      • +
      +
    • +
    • Allowed moves: +
        +
      • From register to register (Move)
      • +
      • From memory to register (Load)
      • +
      • From register to memory (Store)
      • +
      +
    • +
    • Conditional move (cmov*) +
        +
      • Same as above, but based on result of comparison
      • +
      +
    • +
    + +

    Demo – Swap Function

    + +
      +
    • Problem: Let’s swap the contents of two variables
    • +
    • For now, we need to know that +
        +
      • Argument 1 of function swap(…) -> saved in %rdi
      • +
      • Argument 2 of function swap(…) -> saved in %rsi
      • +
      +
    • +
    + +

    Demo – Swap Function

    + +

    C code:

    + +
    void swap(long *xp, long *yp)
    +{
    +  long L1 = *xp;
    +  long L2 = *yp;
    +  *xp = L2;
    +  *yp = L1;
    +  return;
    +}
    +
    + +

    Annotation: Because xp and yp contain an address of 64-bits, %rdi & %rsi are used to hold the value of xp and yp, respectively.

    + +

    Assembly code:

    + +
    wrap: # xp -> %rdi, yp -> %rsi
    +  movq    (%rdi), %rax # L1 = *xp; parenthasies indicate "indirect"
    +  movq    (%rsi), %rdx # L2 = *yp
    +  movq    %rdx, (%rdi) # *xp = L2
    +  movq    %rax, (%rsa) # *yp = L1
    +  ret
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterAddress
    %rdi0x0020
    %rsi0x0000
    %rax123
    %rdx456
    + +

    Remember this is a compressed view of memory.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressValue
    0x0020(deleted)123 (annotation, inserted)456
    0x0018 
    0x0010 
    0x0008 
    0x0000(deleted)456 (annotation, inserted)123
    + +

    Operand Combinations for mov*

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SourceDestAssembly (instruction (space) source, dest)in C
    ImmediateRegistermovq $0x4,%raxresult=0x4;
    ImmediateMemorymovq $-147,(%rax)*result=-147;
    RegisterRegistermovq %rax,%rdxvar=result;
    RegisterMemorymovq %rax,(%rdx)*var1=result;
    MemoryRegistermovq (%rax),%rdxvar1=*result;
    + +

    Cannot do memory-memory transfer with a single mov* instruction

    + +

    Homework 2

    + +
      +
    • Since we cannot do memory-memory transfer with a single mov* instruction … +
        +
      • Can you write a little x86-64 assembly program that transfers +data stored at address 0x0000 to address 0x0018?
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegistersValue
    %rdi 
    %rsi 
    %rax 
    %rdx 
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressValue
    0x0020 
    0x0018 
    0x0010 
    0x0008 
    0x00006
    + +

    Summary

    + +
      +
    • As x86-64 assembly s/w dev., we now get to see more of the microprocessor +(CPU) state: PC, registers, condition codes
    • +
    • x86-64 assembly language – Data +
        +
      • 16 integer registers of 1, 2, 4 or 8 bytes + memory address of 8 bytes
      • +
      • Floating point registers of 4 or 8 bytes
      • +
      • No aggregate types such as arrays or structures
      • +
      +
    • +
    • x86-64 assembly language – Instructions +
        +
      • mov* instruction family +
          +
        • From register to register
        • +
        • From memory to register
        • +
        • From register to memory
        • +
        +
      • +
      • Memory addressing modes
      • +
      • Cannot do memory-memory transfer with a single mov* instruction
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • (selected) Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/09/Lecture_09_Machine_Level_Prog_Data_Move_Annotated.pdf b/_site/melody/cmpt-295/09/Lecture_09_Machine_Level_Prog_Data_Move_Annotated.pdf new file mode 100644 index 0000000..1b60809 Binary files /dev/null and b/_site/melody/cmpt-295/09/Lecture_09_Machine_Level_Prog_Data_Move_Annotated.pdf differ diff --git a/_site/melody/cmpt-295/10/10.html b/_site/melody/cmpt-295/10/10.html new file mode 100644 index 0000000..3559fd0 --- /dev/null +++ b/_site/melody/cmpt-295/10/10.html @@ -0,0 +1,655 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295 Unit - Machine-Level Programming

    + +

    Lecture 10 – Assembly language basics: leaq instruction, +memory addressing modes and +arithmetic & logical operations

    + +

    Last Lecture

    + +
      +
    • As x86-64 assembly s/w dev., we now get to see more of the microprocessor +(CPU) state: PC, registers, condition codes
    • +
    • x86-64 assembly language – Data +
        +
      • 16 integer registers of 1, 2, 4 or 8 bytes + memory address of 8 bytes
      • +
      • Floating point registers of 4 or 8 bytes
      • +
      • No aggregate types such as arrays or structures
      • +
      +
    • +
    • x86-64 assembly language – Instructions +
        +
      • mov* instruction family +
          +
        • From register to register
        • +
        • From memory to register
        • +
        • From register to memory
        • +
        +
      • +
      • Memory addressing modes
      • +
      • Cannot do memory-memory transfer with a single mov* instruction
      • +
      +
    • +
    + +

    Why cannot do memory-memory transfer with a single mov* instruction?

    + +
      +
    • No x86-64 assembly instructions that take 2 memory addresses as +operands
    • +
    • Such instruction would +
        +
      • Makes for very long machine instructions
      • +
      • Require more complex decoder unit (on microprocessor) +in other words, require more complex microprocessor datapath
      • +
      • Memory only has one data bus and one address bus
      • +
      • No appetite for instruction set architects to create such instructions
      • +
      • Registers very fast and can easily be used for such transfer
      • +
      +
    • +
    • More info here: https://stackoverflow.com/questions/33794169/why-isnt-movl-frommemory-to-memory-allowed
    • +
    + +

    Last Lecture

    + +

    Requirement: When reading/writing assembly code…

    + +
      +
    • add a comment at the top of your function in your assembly code describing the parameter-toregister mapping
    • +
    • Comment each of your assembly language instruction by explaining what it does using corresponding C statement or pseudocode
    • +
    + +
    swap:
    +  # xp -> %rdi, yp -> %rsi
    +  movq (%rdi), %rax # L1 = *xp
    +  movq (%rsi), %rdx # L2 = *yp
    +  movq %rdx, (%rdi) # *xp = L2
    +  movq %rax, (%rsi) # *yp = L1
    +  ret
    +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • (highlighted) Memory addressing modes
      • +
      +
    • +
    • (highlighted) Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    Various types of operands to x86-64 instructions

    + +
      +
    1. Integer value as operand directly in an instruction +
        +
      • This operand is called immediate
      • +
      • Operand syntax: Imm
      • +
      • Examples: movq $0x4,%rax and movb $-17,%al (NOTE: These instructions copy immediate value to register)
      • +
      +
    2. +
    3. Registers as operands in an instruction +(Note: So far, this is the type of operands what we have seen!) +
        +
      • Operand value: R[ra]R[r_{a}]
      • +
      • Operand syntax: %ra\%r_{a} <- name of particular register
      • +
      • Example: movq %rax,%rdx (NOTE: This instruction copies the value of one register into another register)
      • +
      +
    4. +
    5. Memory address – using various memory addressing +modes as operands in an instruction
    6. +
    + +

    Memory addressing modes

    + +

    We access memory in an x86-64 instruction by expressing a memory address through various memory addressing modes

    + +
      +
    1. Absolute memory addressing mode +
        +
      • Use memory address as operand directly in instruction
      • +
      +
        +
      • 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))
      • +
      +
    2. +
    3. Indirect memory addressing mode
    4. +
    + +

    2. Indirect memory addressing mode

    + +
      +
    • When a register contains an address +
        +
      • Similar to a pointer in C
      • +
      +
    • +
    • To access the data at the address contained in the +register, we use parentheses (…)
    • +
    • General Syntax: (rbr_b)
    • +
    • Effect: M[R[rbr_b]]
    • +
    + +

    2. Indirect memory addressing mode

    + +

    Example: register to register

    + +

    Assembly code: movq %rdx, %rax

    + +

    Meaning, or affect: raxrdx\text{rax} \leftarrow \text{rdx}, or R[rax]R[rdx]\text{R[rax]} \leftarrow \text{R[rdx]}

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterBeforeAfter
    %rax156
    %rdx66
    M[6]1111
    + +

    vs. memory to register

    + +

    Assembly code: movq (%rdx),%rax

    + +

    Meaning or affect: raxM[rdx]\text{rax} \leftarrow \text{M[rdx]} or R[rax]M[R[rdx]]\text{R[rax]} \leftarrow \text{M[R[rdx]]}

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterBeforeAfter
    %rax1511
    %rdx66
    M[6]1111
    + +

    Other examples:

    + +
      +
    • register to memory: movq %rax,(%rdx)
    • +
    • immediate to memory: movq $-147,(%rax)
    • +
    + +

    leaq - Load effective address instruction

    + +

    leaq has the form of an instruction that reads from memory to a register (because of the parentheses), however it does not reference memory at all!

    + +
      +
    • Often used for address computations and general arithmetic computations
    • +
    • Syntax: leaq Source, Destination
    • +
    • Example: +
        +
      1. Computing addresses +
          +
        • if %rax <- 0x0000000000000008 and %rcx <- 16
        • +
        • leaq (%rax, %rcx), %rdx
        • +
        • Once executed, rdx will contain 0x18
        • +
        +
      2. +
      3. Computing arithmetic expressions of the form x+k×yx + k \times y where k[1,2,4,8]k \in [1,2,4,8] +
          +
        • if %rdi <- variable a
        • +
        • leaq (%rdi, %rdi, 2), %rax (%rdi is x, 2nd %rdi is y and 2 is k)
        • +
        • Once executed, rax will contain 3a
        • +
        • C code: return a*3;
        • +
        +
      4. +
      +
    • +
    • Operand Destination is a register
    • +
    • Operand Source is a memory addressing mode expression
    • +
    + +

    3. “Base + displacement” memory addressing mode

    + +
      +
    • General Syntax: Imm(rbr_b)
    • +
    • Effect: M[Imm + R[rbr_b]]
    • +
    • Examples: +
        +
      • movq %rax, -8(%rsp)
      • +
      • leaq 7(%rdi), %rax
      • +
      +
    • +
    • Careful here! +
        +
      • When dealing with leaq, the effect is Imm + R[rbr_b] not M[Imm + R[rbr_b]]
      • +
      +
    • +
    + +

    4. Indexed memory addressing mode

    + +
      +
    1. General Syntax: (rbr_b,rir_i) +
        +
      • Effect: M[R[rbr_b] + R[rir_i]]
      • +
      • Example: movb (%rdi, %rcx), %al
      • +
      +
    2. +
    3. General Syntax: Imm(rb,ri) +
        +
      • Effect: M[Imm + R[rbr_b] + R[rir_i]]
      • +
      • Example: movw 0xA(%rdi, %rcx), %r11w
      • +
      • Careful here! When dealing with leaq, the effect is
      • +
      +
    4. +
    5. R[rbr_b] + R[rir_i] not M[R[rbr_b] + R[rir_i]]
    6. +
    7. Imm + R[rbr_b] + R[rir_i] not M[Imm + R[rbr_b] + R[rir_i]]
    8. +
    + +

    5. Scaled indexed memory addressing mode

    +
      +
    1. General Syntax: (,rir_i,s) +
        +
      • Effect: M[R[ri]×s]M[R[r_i] \times s]
      • +
      • Example: (, %rdi, 2)
      • +
      +
    2. +
    3. General Syntax: Imm(,rir_i,s) +
        +
      • Effect: M[Imm+R[ri]×s]M[\text{Imm} + R[r_i] \times s]
      • +
      • Example: 3(, %rcx, 8)
      • +
      +
    4. +
    5. General Syntax: (rb,ri,s)(r_{b},r_{i},s) +
        +
      • Effect: M[R[rb]+R[ri]×s]M[R[r_b] + R[r_i] \times s]
      • +
      • Example: (%rdi, %rsi, 4)
      • +
      +
    6. +
    7. General Syntax: Imm(rb,ri,s)\text{Imm}(r_{b},r_{i},s) +
        +
      • Effect: M[Imm+R[rb]+R[ri]×s]M[\text{Imm} + R[r_b] + R[r_i] \times s]
      • +
      • Example: 8(%rdi, %rsi, 4) +13
      • +
      +
    8. +
    + +

    Again, careful here! When dealing with leaq, the effect is not to reference memory at all!

    + +

    Summary - Memory addressing modes

    + +

    We access memory in an x86-64 instruction by expressing a +memory address through various memory addressing modes

    + +
      +
    1. Absolute
    2. +
    3. Indirect
    4. +
    5. “Base + displacement”
    6. +
    7. 2 indexed
    8. +
    9. 4 scaled indexed
    10. +
    + +

    See Table of x86-64 Addressing Modes on Resources web page of our course web site

    + +

    Box on left of slide:

    + +
      +
    • General Syntax: Imm(rb,ri,s)\text{Imm}(r_{b}, r_{i}, s)
    • +
    • Effect: M[Imm+R[rb]+R[ri]×s]M[\text{Imm} + R[r_b]+ R[r_i] \times s]
    • +
    + +

    Let’s try it!

    + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rdx0xf000
    %rcx0x0100
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ExpressionAddress ComputationAddress
    8(%rdx)0xF000+(810=0x8)(8_{10} = \text{0x8})0xF008
    (%rdx,%rcx)0xF000=0x0100\text{0xF000} = \text{0x0100}0xF100
    (%rdx,%rcx,4)0xF000+0x0100×4\text{0xF000} + \text{0x0100} \times 40xF400
    0x80(,%rdx,2)0x80+0xF000×2\text{0x80} + \text{0xF000} \times 20xE080
    0x80(%rdx, 2)invalid -> missing a “,” (comma) before %rdx 
    0x80(,%rdx, 3)invalid scalar value 
    + +

    Two-Operand Arithmetic Instructions

    + +

    * -> Size designator

    +
      +
    • q -> long/64
    • +
    • l -> int/32
    • +
    • w -> short/16
    • +
    • b -> char/8
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SyntaxMeaningExamplesin C
    add* Src, DestDest=Dest+Src\text{Dest} = \text{Dest} + \text{Src}addq %rax,%rcxx+=y
    sub* Src, DestDest=DestSrc\text{Dest} = \text{Dest} - \text{Src}subq %rax,%rcxx-=y
    imul* Src, DestDest=Dest×Src\text{Dest} = \text{Dest} \times \text{Src}imulq $16,(%rax,%rdx,8)x*=y
    + +
      +
    • “destination” and “first operand” are the same +
        +
      • “2 operand” assembly language (machine)
      • +
      +
    • +
    • mem = mem OP mem usually not supported
    • +
    • 2 assembly code formats: ATT and Intel format (see Aside in Section 3.2 P. 177) +
        +
      • We are using the ATT format
      • +
      • Both order the operands of their instructions differently - Watch out!
      • +
      +
    • +
    + +

    Two-Operand Logical Instructions

    + +
      +
    • * -> Size designator
    • +
    • q -> long/64
    • +
    • l -> int/32
    • +
    • w -> short/16
    • +
    • b -> char/8
    • +
    + +

    Transcriber’s 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: &, | and ^ respectively.

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SyntaxMeaningExample
    and* Src, DestDest=DestSrc\text{Dest} = \text{Dest} \land \text{Src}andl $252645135, %edi
    or* Src, DestDest=DestSrc\text{Dest} = \text{Dest} \lor \text{Src}org %rsi,%rdi
    xor* Src, DestDest=DestSrc\text{Dest} = \text{Dest} \oplus \text{Src}xorq %rsi,%rdi
    + +
      +
    • xorq special purpose: +
        +
      • xorq %rax, %rax: zeroes register %rax
      • +
      • movq $0, %rax: also zeroes register %rax
      • +
      +
    • +
    • x86-64 convention: +
        +
      • Any instruction updating the lower 4 bytes will cause the higher-order bytes to be set to 0
      • +
      • xorl %eax, %eax and movl $0, %eax <- also zeroes register %rax
      • +
      +
    • +
    + +

    Two-Operand Shift Instructions

    + +
      +
    • * -> Size designator
    • +
    • q -> long/64
    • +
    • l -> int/32
    • +
    • w -> short/16
    • +
    • b -> char/8
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SyntaxMeaningExampleNote
    sal* Src, DestDest <- Dest « Srcsalq $4,%raxLeft shift–also called shlq: filling Dest with 0, from the right
    sar* Src, DestDest <- » Srcsarl %cl,%raxRight arithmatic Shift: filling Dest with sign bit, form the left.
    shr* Src, DestDest <- Dest » Srcshrq $2,%r8Right logical Shift: filling Rest with 0, from the left
    + +

    One-Operand Arithmetic Instructions

    + +
      +
    • * -> Size designator
    • +
    • q -> long/64
    • +
    • l -> int/32
    • +
    • w -> short/16
    • +
    • b -> char/8
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SyntaxMeaningExamples
    inc* DestDest=Dest+1\text{Dest} = \text{Dest}+1incq (%rsp)
    dec* DestDest=Dest1\text{Dest} = \text{Dest}-1decq %rsi
    neg* DestDest=Dest\text{Dest} = -\text{Dest}negl %eax
    not* DestDest = ~Destnotq %rdi
    + +

    Summary

    + +
      +
    • leaq - load effective address instruction
    • +
    • Various types of operands to x86-64 instructions +
        +
      • Immediate (constant integral value)
      • +
      • Register (16 registers)
      • +
      • Memory address (various memory addressing modes) +
          +
        • General Syntax: Imm(rb,ri,s)\text{Imm}(r_{b}, r_{i}, s)
        • +
        +
      • +
      +
    • +
    • Arithmetic & logical operations +
        +
      • Arithmetic instructions: add*, sub*, imul*, inc*, dec*, neg*, not*
      • +
      • Logical instructions: and*, or*, xor*
      • +
      • Shift instructions: sal*, sar*, shr*
      • +
      +
    • +
    + +

    Next lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/10/10/index.html b/_site/melody/cmpt-295/10/10/index.html new file mode 100644 index 0000000..9070057 --- /dev/null +++ b/_site/melody/cmpt-295/10/10/index.html @@ -0,0 +1,657 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 Unit - Machine-Level Programming

    + +

    Lecture 10 – Assembly language basics: leaq instruction, +memory addressing modes and +arithmetic & logical operations

    + +

    Last Lecture

    + +
      +
    • As x86-64 assembly s/w dev., we now get to see more of the microprocessor +(CPU) state: PC, registers, condition codes
    • +
    • x86-64 assembly language – Data +
        +
      • 16 integer registers of 1, 2, 4 or 8 bytes + memory address of 8 bytes
      • +
      • Floating point registers of 4 or 8 bytes
      • +
      • No aggregate types such as arrays or structures
      • +
      +
    • +
    • x86-64 assembly language – Instructions +
        +
      • mov* instruction family +
          +
        • From register to register
        • +
        • From memory to register
        • +
        • From register to memory
        • +
        +
      • +
      • Memory addressing modes
      • +
      • Cannot do memory-memory transfer with a single mov* instruction
      • +
      +
    • +
    + +

    Why cannot do memory-memory transfer with a single mov* instruction?

    + +
      +
    • No x86-64 assembly instructions that take 2 memory addresses as +operands
    • +
    • Such instruction would +
        +
      • Makes for very long machine instructions
      • +
      • Require more complex decoder unit (on microprocessor) +in other words, require more complex microprocessor datapath
      • +
      • Memory only has one data bus and one address bus
      • +
      • No appetite for instruction set architects to create such instructions
      • +
      • Registers very fast and can easily be used for such transfer
      • +
      +
    • +
    • More info here: https://stackoverflow.com/questions/33794169/why-isnt-movl-frommemory-to-memory-allowed
    • +
    + +

    Last Lecture

    + +

    Requirement: When reading/writing assembly code…

    + +
      +
    • add a comment at the top of your function in your assembly code describing the parameter-toregister mapping
    • +
    • Comment each of your assembly language instruction by explaining what it does using corresponding C statement or pseudocode
    • +
    + +
    swap:
    +  # xp -> %rdi, yp -> %rsi
    +  movq (%rdi), %rax # L1 = *xp
    +  movq (%rsi), %rdx # L2 = *yp
    +  movq %rdx, (%rdi) # *xp = L2
    +  movq %rax, (%rsi) # *yp = L1
    +  ret
    +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • (highlighted) Memory addressing modes
      • +
      +
    • +
    • (highlighted) Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    Various types of operands to x86-64 instructions

    + +
      +
    1. Integer value as operand directly in an instruction +
        +
      • This operand is called immediate
      • +
      • Operand syntax: Imm
      • +
      • Examples: movq $0x4,%rax and movb $-17,%al (NOTE: These instructions copy immediate value to register)
      • +
      +
    2. +
    3. Registers as operands in an instruction +(Note: So far, this is the type of operands what we have seen!) +
        +
      • Operand value: R[ra]R[r_{a}]
      • +
      • Operand syntax: %ra\%r_{a} <- name of particular register
      • +
      • Example: movq %rax,%rdx (NOTE: This instruction copies the value of one register into another register)
      • +
      +
    4. +
    5. Memory address – using various memory addressing +modes as operands in an instruction
    6. +
    + +

    Memory addressing modes

    + +

    We access memory in an x86-64 instruction by expressing a memory address through various memory addressing modes

    + +
      +
    1. Absolute memory addressing mode +
        +
      • Use memory address as operand directly in instruction
      • +
      +
        +
      • 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))
      • +
      +
    2. +
    3. Indirect memory addressing mode
    4. +
    + +

    2. Indirect memory addressing mode

    + +
      +
    • When a register contains an address +
        +
      • Similar to a pointer in C
      • +
      +
    • +
    • To access the data at the address contained in the +register, we use parentheses (…)
    • +
    • General Syntax: (rbr_b)
    • +
    • Effect: M[R[rbr_b]]
    • +
    + +

    2. Indirect memory addressing mode

    + +

    Example: register to register

    + +

    Assembly code: movq %rdx, %rax

    + +

    Meaning, or affect: raxrdx\text{rax} \leftarrow \text{rdx}, or R[rax]R[rdx]\text{R[rax]} \leftarrow \text{R[rdx]}

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterBeforeAfter
    %rax156
    %rdx66
    M[6]1111
    + +

    vs. memory to register

    + +

    Assembly code: movq (%rdx),%rax

    + +

    Meaning or affect: raxM[rdx]\text{rax} \leftarrow \text{M[rdx]} or R[rax]M[R[rdx]]\text{R[rax]} \leftarrow \text{M[R[rdx]]}

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterBeforeAfter
    %rax1511
    %rdx66
    M[6]1111
    + +

    Other examples:

    + +
      +
    • register to memory: movq %rax,(%rdx)
    • +
    • immediate to memory: movq $-147,(%rax)
    • +
    + +

    leaq - Load effective address instruction

    + +

    leaq has the form of an instruction that reads from memory to a register (because of the parentheses), however it does not reference memory at all!

    + +
      +
    • Often used for address computations and general arithmetic computations
    • +
    • Syntax: leaq Source, Destination
    • +
    • Example: +
        +
      1. Computing addresses +
          +
        • if %rax <- 0x0000000000000008 and %rcx <- 16
        • +
        • leaq (%rax, %rcx), %rdx
        • +
        • Once executed, rdx will contain 0x18
        • +
        +
      2. +
      3. Computing arithmetic expressions of the form x+k×yx + k \times y where k[1,2,4,8]k \in [1,2,4,8] +
          +
        • if %rdi <- variable a
        • +
        • leaq (%rdi, %rdi, 2), %rax (%rdi is x, 2nd %rdi is y and 2 is k)
        • +
        • Once executed, rax will contain 3a
        • +
        • C code: return a*3;
        • +
        +
      4. +
      +
    • +
    • Operand Destination is a register
    • +
    • Operand Source is a memory addressing mode expression
    • +
    + +

    3. “Base + displacement” memory addressing mode

    + +
      +
    • General Syntax: Imm(rbr_b)
    • +
    • Effect: M[Imm + R[rbr_b]]
    • +
    • Examples: +
        +
      • movq %rax, -8(%rsp)
      • +
      • leaq 7(%rdi), %rax
      • +
      +
    • +
    • Careful here! +
        +
      • When dealing with leaq, the effect is Imm + R[rbr_b] not M[Imm + R[rbr_b]]
      • +
      +
    • +
    + +

    4. Indexed memory addressing mode

    + +
      +
    1. General Syntax: (rbr_b,rir_i) +
        +
      • Effect: M[R[rbr_b] + R[rir_i]]
      • +
      • Example: movb (%rdi, %rcx), %al
      • +
      +
    2. +
    3. General Syntax: Imm(rb,ri) +
        +
      • Effect: M[Imm + R[rbr_b] + R[rir_i]]
      • +
      • Example: movw 0xA(%rdi, %rcx), %r11w
      • +
      • Careful here! When dealing with leaq, the effect is
      • +
      +
    4. +
    5. R[rbr_b] + R[rir_i] not M[R[rbr_b] + R[rir_i]]
    6. +
    7. Imm + R[rbr_b] + R[rir_i] not M[Imm + R[rbr_b] + R[rir_i]]
    8. +
    + +

    5. Scaled indexed memory addressing mode

    +
      +
    1. General Syntax: (,rir_i,s) +
        +
      • Effect: M[R[ri]×s]M[R[r_i] \times s]
      • +
      • Example: (, %rdi, 2)
      • +
      +
    2. +
    3. General Syntax: Imm(,rir_i,s) +
        +
      • Effect: M[Imm+R[ri]×s]M[\text{Imm} + R[r_i] \times s]
      • +
      • Example: 3(, %rcx, 8)
      • +
      +
    4. +
    5. General Syntax: (rb,ri,s)(r_{b},r_{i},s) +
        +
      • Effect: M[R[rb]+R[ri]×s]M[R[r_b] + R[r_i] \times s]
      • +
      • Example: (%rdi, %rsi, 4)
      • +
      +
    6. +
    7. General Syntax: Imm(rb,ri,s)\text{Imm}(r_{b},r_{i},s) +
        +
      • Effect: M[Imm+R[rb]+R[ri]×s]M[\text{Imm} + R[r_b] + R[r_i] \times s]
      • +
      • Example: 8(%rdi, %rsi, 4) +13
      • +
      +
    8. +
    + +

    Again, careful here! When dealing with leaq, the effect is not to reference memory at all!

    + +

    Summary - Memory addressing modes

    + +

    We access memory in an x86-64 instruction by expressing a +memory address through various memory addressing modes

    + +
      +
    1. Absolute
    2. +
    3. Indirect
    4. +
    5. “Base + displacement”
    6. +
    7. 2 indexed
    8. +
    9. 4 scaled indexed
    10. +
    + +

    See Table of x86-64 Addressing Modes on Resources web page of our course web site

    + +

    Box on left of slide:

    + +
      +
    • General Syntax: Imm(rb,ri,s)\text{Imm}(r_{b}, r_{i}, s)
    • +
    • Effect: M[Imm+R[rb]+R[ri]×s]M[\text{Imm} + R[r_b]+ R[r_i] \times s]
    • +
    + +

    Let’s try it!

    + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rdx0xf000
    %rcx0x0100
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ExpressionAddress ComputationAddress
    8(%rdx)0xF000+(810=0x8)(8_{10} = \text{0x8})0xF008
    (%rdx,%rcx)0xF000=0x0100\text{0xF000} = \text{0x0100}0xF100
    (%rdx,%rcx,4)0xF000+0x0100×4\text{0xF000} + \text{0x0100} \times 40xF400
    0x80(,%rdx,2)0x80+0xF000×2\text{0x80} + \text{0xF000} \times 20xE080
    0x80(%rdx, 2)invalid -> missing a “,” (comma) before %rdx 
    0x80(,%rdx, 3)invalid scalar value 
    + +

    Two-Operand Arithmetic Instructions

    + +

    * -> Size designator

    +
      +
    • q -> long/64
    • +
    • l -> int/32
    • +
    • w -> short/16
    • +
    • b -> char/8
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SyntaxMeaningExamplesin C
    add* Src, DestDest=Dest+Src\text{Dest} = \text{Dest} + \text{Src}addq %rax,%rcxx+=y
    sub* Src, DestDest=DestSrc\text{Dest} = \text{Dest} - \text{Src}subq %rax,%rcxx-=y
    imul* Src, DestDest=Dest×Src\text{Dest} = \text{Dest} \times \text{Src}imulq $16,(%rax,%rdx,8)x*=y
    + +
      +
    • “destination” and “first operand” are the same +
        +
      • “2 operand” assembly language (machine)
      • +
      +
    • +
    • mem = mem OP mem usually not supported
    • +
    • 2 assembly code formats: ATT and Intel format (see Aside in Section 3.2 P. 177) +
        +
      • We are using the ATT format
      • +
      • Both order the operands of their instructions differently - Watch out!
      • +
      +
    • +
    + +

    Two-Operand Logical Instructions

    + +
      +
    • * -> Size designator
    • +
    • q -> long/64
    • +
    • l -> int/32
    • +
    • w -> short/16
    • +
    • b -> char/8
    • +
    + +

    Transcriber’s 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: &, | and ^ respectively.

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SyntaxMeaningExample
    and* Src, DestDest=DestSrc\text{Dest} = \text{Dest} \land \text{Src}andl $252645135, %edi
    or* Src, DestDest=DestSrc\text{Dest} = \text{Dest} \lor \text{Src}org %rsi,%rdi
    xor* Src, DestDest=DestSrc\text{Dest} = \text{Dest} \oplus \text{Src}xorq %rsi,%rdi
    + +
      +
    • xorq special purpose: +
        +
      • xorq %rax, %rax: zeroes register %rax
      • +
      • movq $0, %rax: also zeroes register %rax
      • +
      +
    • +
    • x86-64 convention: +
        +
      • Any instruction updating the lower 4 bytes will cause the higher-order bytes to be set to 0
      • +
      • xorl %eax, %eax and movl $0, %eax <- also zeroes register %rax
      • +
      +
    • +
    + +

    Two-Operand Shift Instructions

    + +
      +
    • * -> Size designator
    • +
    • q -> long/64
    • +
    • l -> int/32
    • +
    • w -> short/16
    • +
    • b -> char/8
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SyntaxMeaningExampleNote
    sal* Src, DestDest <- Dest « Srcsalq $4,%raxLeft shift–also called shlq: filling Dest with 0, from the right
    sar* Src, DestDest <- » Srcsarl %cl,%raxRight arithmatic Shift: filling Dest with sign bit, form the left.
    shr* Src, DestDest <- Dest » Srcshrq $2,%r8Right logical Shift: filling Rest with 0, from the left
    + +

    One-Operand Arithmetic Instructions

    + +
      +
    • * -> Size designator
    • +
    • q -> long/64
    • +
    • l -> int/32
    • +
    • w -> short/16
    • +
    • b -> char/8
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SyntaxMeaningExamples
    inc* DestDest=Dest+1\text{Dest} = \text{Dest}+1incq (%rsp)
    dec* DestDest=Dest1\text{Dest} = \text{Dest}-1decq %rsi
    neg* DestDest=Dest\text{Dest} = -\text{Dest}negl %eax
    not* DestDest = ~Destnotq %rdi
    + +

    Summary

    + +
      +
    • leaq - load effective address instruction
    • +
    • Various types of operands to x86-64 instructions +
        +
      • Immediate (constant integral value)
      • +
      • Register (16 registers)
      • +
      • Memory address (various memory addressing modes) +
          +
        • General Syntax: Imm(rb,ri,s)\text{Imm}(r_{b}, r_{i}, s)
        • +
        +
      • +
      +
    • +
    • Arithmetic & logical operations +
        +
      • Arithmetic instructions: add*, sub*, imul*, inc*, dec*, neg*, not*
      • +
      • Logical instructions: and*, or*, xor*
      • +
      • Shift instructions: sal*, sar*, shr*
      • +
      +
    • +
    + +

    Next lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/10/Lecture_10_Machine_Level_Prog_Arithmetic_leaq_Instructions.pdf b/_site/melody/cmpt-295/10/Lecture_10_Machine_Level_Prog_Arithmetic_leaq_Instructions.pdf new file mode 100644 index 0000000..c446787 Binary files /dev/null and b/_site/melody/cmpt-295/10/Lecture_10_Machine_Level_Prog_Arithmetic_leaq_Instructions.pdf differ diff --git a/_site/melody/cmpt-295/11/11.html b/_site/melody/cmpt-295/11/11.html new file mode 100644 index 0000000..4fc870b --- /dev/null +++ b/_site/melody/cmpt-295/11/11.html @@ -0,0 +1,124 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Machine-Level Programming
    • +
    • Lecture 11 – Assembly language basics: Practice and DEMO +-> leaq and arithmetic & logical instructions and +memory addressing modes
    • +
    + +

    Why did the programmer quit their job?

    + +

    A never got arrays!

    + +

    Summary

    + +
      +
    • leaq - load effective address instruction
    • +
    • Using data as operand to an instruction: +
        +
      • Immediate (constant integral value)
      • +
      • Register (16 registers)
      • +
      • Memory (various memory addressing modes) +
          +
        • General Syntax: Imm(rb,ri,s)\text{Imm}(r_{b}, r_{i}, s)
        • +
        +
      • +
      +
    • +
    • Arithmetic & logical operations +
        +
      • Arithmetic instructions: add*, sub*, imul* inc*, dec*, neg*, not*
      • +
      • Logical instructions: and*, or*, xor*
      • +
      • Shift instructions: sal*, sar*, shr*
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • (selected) Memory addressing modes
      • +
      +
    • +
    • (selected) Operation leaq and Arithmetic & logical operations (Annotation: practice and DEMO!)
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    Demo

    + +
      +
    1. gcc uses leaq for addition -> sum_store.c
    2. +
    3. Writing our own assembly code (arith.s) using arithmetic +instructions of x86-64 assembly language
    4. +
    5. makefile +
        +
      • when we compile our own *.s files with *.c files
      • +
      • when we compile only *.c files
      • +
      +
    6. +
    7. How would gcc compile our arith.c into arith.s?
    8. +
    + +

    Summary

    + +
      +
    • Demo +
        +
      • Observation: C compiler will figure out different instruction combinations to carry out the computations in our C code
      • +
      +
    • +
    + +

    Next lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/11/11/index.html b/_site/melody/cmpt-295/11/11/index.html new file mode 100644 index 0000000..ff508ba --- /dev/null +++ b/_site/melody/cmpt-295/11/11/index.html @@ -0,0 +1,126 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Machine-Level Programming
    • +
    • Lecture 11 – Assembly language basics: Practice and DEMO +-> leaq and arithmetic & logical instructions and +memory addressing modes
    • +
    + +

    Why did the programmer quit their job?

    + +

    A never got arrays!

    + +

    Summary

    + +
      +
    • leaq - load effective address instruction
    • +
    • Using data as operand to an instruction: +
        +
      • Immediate (constant integral value)
      • +
      • Register (16 registers)
      • +
      • Memory (various memory addressing modes) +
          +
        • General Syntax: Imm(rb,ri,s)\text{Imm}(r_{b}, r_{i}, s)
        • +
        +
      • +
      +
    • +
    • Arithmetic & logical operations +
        +
      • Arithmetic instructions: add*, sub*, imul* inc*, dec*, neg*, not*
      • +
      • Logical instructions: and*, or*, xor*
      • +
      • Shift instructions: sal*, sar*, shr*
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • (selected) Memory addressing modes
      • +
      +
    • +
    • (selected) Operation leaq and Arithmetic & logical operations (Annotation: practice and DEMO!)
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    Demo

    + +
      +
    1. gcc uses leaq for addition -> sum_store.c
    2. +
    3. Writing our own assembly code (arith.s) using arithmetic +instructions of x86-64 assembly language
    4. +
    5. makefile +
        +
      • when we compile our own *.s files with *.c files
      • +
      • when we compile only *.c files
      • +
      +
    6. +
    7. How would gcc compile our arith.c into arith.s?
    8. +
    + +

    Summary

    + +
      +
    • Demo +
        +
      • Observation: C compiler will figure out different instruction combinations to carry out the computations in our C code
      • +
      +
    • +
    + +

    Next lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmov*
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/11/Lecture_11_Machine_Level_Prog_Instructions_Practice_Demo.pdf b/_site/melody/cmpt-295/11/Lecture_11_Machine_Level_Prog_Instructions_Practice_Demo.pdf new file mode 100644 index 0000000..b02b223 Binary files /dev/null and b/_site/melody/cmpt-295/11/Lecture_11_Machine_Level_Prog_Instructions_Practice_Demo.pdf differ diff --git a/_site/melody/cmpt-295/12/12.html b/_site/melody/cmpt-295/12/12.html new file mode 100644 index 0000000..fb43316 --- /dev/null +++ b/_site/melody/cmpt-295/12/12.html @@ -0,0 +1,326 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Machine-Level Programming
    • +
    • Lecture 12 – Assembly language – Program Control – Conditional Statements
    • +
    + +

    Last Lecture

    + +
      +
    • Demo +
        +
      • Observation: C compiler will figure out different instruction combinations to carry out the computations in our C code
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX (selected)
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    Completing our Demo

    + +
      +
    1. (checked) gcc uses leaq for addition -> sum_store.c our own assembly code (arith.s) using arithmetic
    2. +
    3. (checked) Writing instructions of x86-64 assembly language
    4. +
    5. makefile +
        +
      • when we compile our own *.s files with *.c files
      • +
      • when we compile only *.c files
      • +
      +
    6. +
    7. How would gcc compile our arith.c into arith.s?
    8. +
    + +

    Program Control Overview

    + +
      +
    • We can change the execution flow of a program +
        +
      1. Based on a condition
      2. +
      3. Unconditionally
      4. +
      +
    • +
    • Control statements (in C) +
        +
      • if/else
      • +
      • switch
      • +
      • cmp* instruction (compare)
      • +
      • for loop
      • +
      +
    • +
    • Control statements (in x86-64 assemvly) +
        +
      • cmp* instruction (compare)
      • +
      • jX instructions (jump)
      • +
      +
    • +
    + +

    Function calls -> call and ret

    + +

    Conditional statement: if/else

    + +

    in C:

    + +
    void func(long x,long y){
    +  if ( x < y ) {
    +    // stmts true
    +  } else {
    +    // stmts false
    +  }
    +  return;
    +}
    +
    + +

    A label is a memory address.

    + +

    in assembly:

    +
    # 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     #
    +
    + +

    We branch (jump) when the condition is false -> This technique is called “coding the false condition first”

    + +

    comparison instructions

    + +
      +
    • * -> Size designator
    • +
    • q -> long/64-bits
    • +
    • l -> int/32-bits
    • +
    • w -> short/16-bits
    • +
    • b -> char/8-bits
    • +
    + +

    Remember in Lecture 9, we saw… (See header “Programming in x86-64 assembly” from lecture 9)

    + + + + + + + + + + + + + + + + + + + + + + + + +
    SyntaxMeaning/EffectExampleNotes
    cmp* Src2, Src1Src1 – Src2 ->
    > 0? -> Src1 > Src2
    = 0? -> Src1 == Src2
    < 0? -> Src1 < Src2
    cmpq %rsi,%rdiwithout saving the result in the destination operand (no Dest). Sets condition codes based on value of Src1Src2\text{Src1} - \text{Src2}
    test* Src2, Src1Src1 & Src2 ->testq %rax,%raxwithout saving the result in the destination operand (no Dest); sets condition code based on value of Src1Src2\text{Src1} \land \text{Src2}; useful when one of the operands is a bit mask
    + +

    jX jump family instructions (branching)

    + +
      +
    • Jump to different part of the program depending on result of previous instructions (i.e., condition codes)
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    jXDescription
    jmpunconditional
    jeEqual / Zero
    jneNot Equal / Not Zero
    jsNegative
    jnsNonnegative
    jgGreater (Signed)
    jgeGreater or Equal (Signed)
    jlLess (Signed)
    jleLess or Equal (Signed
    jaAbove (unsigned)
    jbBelow (unsigned)
    + +

    Example – int abs(int x)

    + +

    in C:

    +
    int abs(int x){
    +  if(x<0)
    +    x=-x;
    +  return x;
    +}
    +
    + +

    in assembly:

    +
    # x in edi, result in eax
    +
    +abs:
    +  movl %edi,%eax # eax <- x
    +  ____ #
    +  ____ # ret if x >= 0
    +  ____ # x = -x
    +
    +endif:
    +  ret
    +
    + +

    int max(int x, int y )- Homework

    + +

    In C:

    + +
    int max(int x, int y){
    +  int result=x;
    +  if(y>x)
    +    result=y;
    +  return result;
    +}
    +
    + +

    In assembly:

    + +
    # x in edi, y in esi, result in eax
    +max:
    +  movl %edi,%eax # result = x
    +  ____
    +  ____
    +  ____
    +  ____
    +  ____
    +endif:
    +  ret 
    +
    + +

    Summary

    + +
      +
    • In C, we can change the execution flow of a program +
        +
      1. Conditionaly +
          +
        • Conditional statements: if/else, switch
        • +
        • Iterative statements: loops
        • +
        +
      2. +
      3. Unconditionally +
          +
        • Functions calls
        • +
        +
      4. +
      +
    • +
    • In x86-64 assembly, we can also change the execution flow of a program +
        +
      • cmp* instruction (compare)
      • +
      • jX instructions (jump)
      • +
      • call and ret instructions
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • (selected) Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/12/12/index.html b/_site/melody/cmpt-295/12/12/index.html new file mode 100644 index 0000000..bf589d1 --- /dev/null +++ b/_site/melody/cmpt-295/12/12/index.html @@ -0,0 +1,328 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Machine-Level Programming
    • +
    • Lecture 12 – Assembly language – Program Control – Conditional Statements
    • +
    + +

    Last Lecture

    + +
      +
    • Demo +
        +
      • Observation: C compiler will figure out different instruction combinations to carry out the computations in our C code
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX (selected)
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    Completing our Demo

    + +
      +
    1. (checked) gcc uses leaq for addition -> sum_store.c our own assembly code (arith.s) using arithmetic
    2. +
    3. (checked) Writing instructions of x86-64 assembly language
    4. +
    5. makefile +
        +
      • when we compile our own *.s files with *.c files
      • +
      • when we compile only *.c files
      • +
      +
    6. +
    7. How would gcc compile our arith.c into arith.s?
    8. +
    + +

    Program Control Overview

    + +
      +
    • We can change the execution flow of a program +
        +
      1. Based on a condition
      2. +
      3. Unconditionally
      4. +
      +
    • +
    • Control statements (in C) +
        +
      • if/else
      • +
      • switch
      • +
      • cmp* instruction (compare)
      • +
      • for loop
      • +
      +
    • +
    • Control statements (in x86-64 assemvly) +
        +
      • cmp* instruction (compare)
      • +
      • jX instructions (jump)
      • +
      +
    • +
    + +

    Function calls -> call and ret

    + +

    Conditional statement: if/else

    + +

    in C:

    + +
    void func(long x,long y){
    +  if ( x < y ) {
    +    // stmts true
    +  } else {
    +    // stmts false
    +  }
    +  return;
    +}
    +
    + +

    A label is a memory address.

    + +

    in assembly:

    +
    # 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     #
    +
    + +

    We branch (jump) when the condition is false -> This technique is called “coding the false condition first”

    + +

    comparison instructions

    + +
      +
    • * -> Size designator
    • +
    • q -> long/64-bits
    • +
    • l -> int/32-bits
    • +
    • w -> short/16-bits
    • +
    • b -> char/8-bits
    • +
    + +

    Remember in Lecture 9, we saw… (See header “Programming in x86-64 assembly” from lecture 9)

    + + + + + + + + + + + + + + + + + + + + + + + + +
    SyntaxMeaning/EffectExampleNotes
    cmp* Src2, Src1Src1 – Src2 ->
    > 0? -> Src1 > Src2
    = 0? -> Src1 == Src2
    < 0? -> Src1 < Src2
    cmpq %rsi,%rdiwithout saving the result in the destination operand (no Dest). Sets condition codes based on value of Src1Src2\text{Src1} - \text{Src2}
    test* Src2, Src1Src1 & Src2 ->testq %rax,%raxwithout saving the result in the destination operand (no Dest); sets condition code based on value of Src1Src2\text{Src1} \land \text{Src2}; useful when one of the operands is a bit mask
    + +

    jX jump family instructions (branching)

    + +
      +
    • Jump to different part of the program depending on result of previous instructions (i.e., condition codes)
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    jXDescription
    jmpunconditional
    jeEqual / Zero
    jneNot Equal / Not Zero
    jsNegative
    jnsNonnegative
    jgGreater (Signed)
    jgeGreater or Equal (Signed)
    jlLess (Signed)
    jleLess or Equal (Signed
    jaAbove (unsigned)
    jbBelow (unsigned)
    + +

    Example – int abs(int x)

    + +

    in C:

    +
    int abs(int x){
    +  if(x<0)
    +    x=-x;
    +  return x;
    +}
    +
    + +

    in assembly:

    +
    # x in edi, result in eax
    +
    +abs:
    +  movl %edi,%eax # eax <- x
    +  ____ #
    +  ____ # ret if x >= 0
    +  ____ # x = -x
    +
    +endif:
    +  ret
    +
    + +

    int max(int x, int y )- Homework

    + +

    In C:

    + +
    int max(int x, int y){
    +  int result=x;
    +  if(y>x)
    +    result=y;
    +  return result;
    +}
    +
    + +

    In assembly:

    + +
    # x in edi, y in esi, result in eax
    +max:
    +  movl %edi,%eax # result = x
    +  ____
    +  ____
    +  ____
    +  ____
    +  ____
    +endif:
    +  ret 
    +
    + +

    Summary

    + +
      +
    • In C, we can change the execution flow of a program +
        +
      1. Conditionaly +
          +
        • Conditional statements: if/else, switch
        • +
        • Iterative statements: loops
        • +
        +
      2. +
      3. Unconditionally +
          +
        • Functions calls
        • +
        +
      4. +
      +
    • +
    • In x86-64 assembly, we can also change the execution flow of a program +
        +
      • cmp* instruction (compare)
      • +
      • jX instructions (jump)
      • +
      • call and ret instructions
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • (selected) Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/12/Lecture_12_Machine_Level_Prog_Control.pdf b/_site/melody/cmpt-295/12/Lecture_12_Machine_Level_Prog_Control.pdf new file mode 100644 index 0000000..5185bca Binary files /dev/null and b/_site/melody/cmpt-295/12/Lecture_12_Machine_Level_Prog_Control.pdf differ diff --git a/_site/melody/cmpt-295/13/13-updated.html b/_site/melody/cmpt-295/13/13-updated.html new file mode 100644 index 0000000..6cf4645 --- /dev/null +++ b/_site/melody/cmpt-295/13/13-updated.html @@ -0,0 +1,446 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Machine-Level Programming
    • +
    • Lecture 13 – Assembly language – Program Control – cmovX
    • +
    • Iterative Statements – Loops
    • +
    + +

    Last Lecture

    + +
      +
    • In C, we can change the execution flow of a program +
        +
      1. Conditionaly +
          +
        • Conditional statements: if/else, switch
        • +
        • Iterative statements: loops
        • +
        +
      2. +
      3. Unconditionally +
          +
        • Functions calls
        • +
        +
      4. +
      +
    • +
    • In x86-64 assembly, we can also change the execution flow of a program +
        +
      • cmp* instruction (compare)
      • +
      • jX insturction (jump)
      • +
      • call and ret instructions
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • (highlighted) Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    Homework: int max(int x, int y)

    + +

    In C:

    + +
    int max(int x,int y){
    +  int result = x;
    +  if(y>x)
    +    result=y;
    +  return result;
    +}
    +
    + +

    version 1 – with jX instruction

    + +

    In Assembly: # x in %edi, y in %esi, result in %eax

    + +
    max:
    +  movl %edi,%eax #result=x
    +  cmpl %edi,%esi #if y<=x then
    +  jle endif #return
    +  movl %esi,%eax #result=y
    +endif:
    +  ret
    +
    + +

    We branch (jump) when the condition (y > x) is false, i.e., when (y <= x) +-> This technique is called “coding the false condition first” +or ”taking care of …”

    + +

    Conditional move instruction cmovX

    + +

    What C code looks like when using conditional operator:

    + +
    result=test?val2:val1;
    +return result;
    +
    + +

    What logic of assembly +code looks like when using +cmovX (expressed in C):

    + +
    result=val1;
    +if (test) result = val2;
    +return result;
    +
    + +

    Example: cmovle Src,Dest

    + +

    Alternative: int abs(int x)

    + +

    In C:

    + +
    int abs(int x){
    +  if(x<0)
    +    x=-x;
    +  return x;
    +}
    +
    + +

    in assembly: # x in %edi, result in %eax

    + +
    abs:
    +  movl %edi,%eax # result = x
    +  negl %edi # x = -x
    +  cmpl $0,%eax # if x < 0 then
    +  cmovl %edi,%eax # result = -x
    +  ret
    +
    + +

    Advantage of conditional move cmovX

    + +

    Note about branching:

    + +
      +
    • Branches are very disruptive to instruction flow through +microprocessor CPU pipelines
    • +
    • However, since conditional moves (cmovX) do not +require control transfer (no branching/jumping required), +they are less disruptive
    • +
    • So, gcc tries to use them, but only when safe
    • +
    + +

    What do we mean by “safe”?

    + +
      +
    • In result = test ? aVal : anotherVal; both values +(aVal and anotherVal) are computed so their +computation must be “safe”
    • +
    • Example of unsafe computations: +
        +
      1. Expensive computations val = Test(x) ? Hard1(x) : Hard2(x); +
          +
        • Only makes sense when computations are very simple
        • +
        +
      2. +
      3. Risky computations val = p ? *p : 0; +
          +
        • Only makes sense when computations do not crash the application
        • +
        +
      4. +
      5. Computations with side effects val = x > 0 ? x*=7 : x+=3; +
          +
        • Only makes sense when computations do not have side effects
        • +
        +
      6. +
      +
    • +
    + +

    Homework: Example: alternate int max(int x, int y)

    + +

    version 2 – with cmovX instruction

    + +

    In C:

    + +
    int max(int x, int y){
    +  int result = x;
    +  if(y>x)
    +    result = y;
    +  return result;
    +}
    +
    + +

    In Assembly: # x in %edi, y in %esi, result in %eax

    + +
    max:
    +  movl %edi,%eax #result=x
    +  cmpl %edi,%esi #if y>x then
    +  cmovg %esi,%eax #result=y
    +  ret
    +
    + +

    While loop – “coding the false condition first” (updated)

    + +

    int x and int y are arguments to function

    + +

    in C:

    + +
    while(x<y){
    +  //stmts
    +}
    +return;
    +
    + +

    Steps:

    + +
      +
    1. Analyze the condition: x<y
    2. +
    3. write cmpl %esi,%edi
    4. +
    5. List all outcomes: +
        +
      • xy>0    x>yx-y > 0 \implies x>y: exits loop
      • +
      • xy<0    x<yx-y < 0 \implies x< y: loops
      • +
      • xy=0    x=yx-y = 0 \implies x=y: exits loop
      • +
      +
    6. +
    7. Which outcomes “exit loop”?
    8. +
    9. Write for false condition i.e. g & e (jge)
    10. +
    + +

    in assembly:

    + +
    loop: # x in %edi, y in %esi
    +  cmpl %esi,%edi # cmpl y,x
    +  jge endloop
    +  #stmts in loop
    +  jmp loop
    +endloop:
    +  ret
    +
    + +

    Loop Pattern 1

    + +
    loop:
    +  if cond false
    +    goto done:
    +  stmts
    +  goto loop:
    +done:
    +
    + +

    While loop – “jump-to-middle” (updated)

    + +

    int x and int y are arguments to function

    + +

    in C:

    + +
    while(x<y){ //step 1
    +  //stmts
    +}
    +return;
    +
    + +

    Steps:

    + +
      +
    1. (shown in C code)
    2. +
    3. (shwon in assembly)
    4. +
    5. All possible values +
        +
      • xy>0    x>yx-y >0 \implies x>y: exits loop, g
      • +
      • xy<0    x<yx-y < 0 \implies x< y: loops, l
      • +
      • xy=0    x=yx-y = 0 \implies x=y: exits loops, e
      • +
      +
    6. +
    7. Consider outcome ???(can’t read) “loops”
    8. +
    + +

    in assembly:

    + +
      jmp test
    +loop:
    +  # stmts
    +test:
    +  cmpl %esi,%edi #cmpl y,x (step 2)
    +  jl loop (step 5)
    +  ret
    +
    + +

    Loop Pattern 2

    + +
      goto test:
    +loop:
    +  # stmts
    +test:
    +  if cond true
    +    goto loop:
    +done:
    +
    + +

    Do While loop – “jump-to-middle” (updated)

    + +

    int x and int y are arguments to function

    + +

    in C:

    + +
    do{
    +  stmts
    +} while(x<y);
    +return;
    +
    + +

    in assembly:

    + +
    loop:
    +  #stmts
    +test:
    +  jmpl %esi,%edi
    +  jl loop
    +  ret
    +
    + +

    Loop Pattern 2:

    + +
    (deleted) goto test:
    +loop:
    +  stmts
    +test:
    +  if cond true
    +    goto loop:
    +done:
    +
    + +

    For loop (updated)

    + +

    In C:

    + +
    //format: for(initialization; condition testing; increment)
    +for(int i=0;i<n;++i){
    +  //stmts
    +}
    +return;
    +
    + +

    Becomes:

    + +
    i=0; // initialization
    +while(i<n){// condition testing
    +  //stmts
    +  i++; //increment
    +}
    +return;
    +
    + +

    Questions:

    + +
      +
    1. Here are we coding the false condition first?
    2. +
    3. Which loop patterns are we using?
    4. +
    + +

    Which becomes, in Assembly:

    + +
      xorl %ecx, %ecx # initialization
    +loop:             # %ecx (i) <- 0
    +  cmpl %edi, %ecx # while i < n true (testing)
    +  jge endloop     # jump when i>=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
    +
    + +

    Summary

    + +

    Compiler can produce different instruction combinations when assembling the same C code.

    + +

    cmp* and test* instructions set condition codes

    + +
      +
    • In x86-64 assembly, there are no conditional statements, however, +we can alter the execution flow of a program by using … +
        +
      • cmp* instruction (compare)
      • +
      • jX instructions (jump)
      • +
      • call and ret instructions
      • +
      • cmovX instructions -> conditional move
      • +
      +
    • +
    • In x86-64 assembly, there are no iterative statements, however, we +can alter the execution flow of a program by using … +
        +
      • cmp* instruction
      • +
      • jX instructions (jump)
      • +
      +
    • +
    • CPU uses these condition codes to decide whether a … +
        +
      • jX instruction (conditional jump) is to be exectued or a
      • +
      • cmovX instruction (conditional move) is to be exectued
      • +
      +
    • +
    • 2 loop patterns: +
        +
      • “coding the false condition first” -> while loops (hence for loops)
      • +
      • “jump-in-middle” -> while, do-while (hence for loops)
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • (highlighted) Overview of Function Call
      • +
      • (highlighted) Memory Layout and Stack - x86-64 instructions and registers
      • +
      • (highlighted) Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/13/13.html b/_site/melody/cmpt-295/13/13.html new file mode 100644 index 0000000..56316d4 --- /dev/null +++ b/_site/melody/cmpt-295/13/13.html @@ -0,0 +1,415 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Machine-Level Programming
    • +
    • Lecture 13 – Assembly language – Program Control – cmovX
    • +
    • Iterative Statements – Loops
    • +
    + +

    Last Lecture

    + +
      +
    • In C, we can change the execution flow of a program +
        +
      1. Conditionaly +
          +
        • Conditional statements: if/else, switch
        • +
        • Iterative statements: loops
        • +
        +
      2. +
      3. Unconditionally +
          +
        • Functions calls
        • +
        +
      4. +
      +
    • +
    • In x86-64 assembly, we can also change the execution flow of a program +
        +
      • cmp* instruction (compare)
      • +
      • jX insturction (jump)
      • +
      • call and ret instructions
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • (highlighted) Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    Homework: int max(int x, int y)

    + +

    In C:

    + +
    int max(int x,int y){
    +  int result = x;
    +  if(y>x)
    +    result=y;
    +  return result;
    +}
    +
    + +

    version 1 – with jX instruction

    + +

    In Assembly: # x in %edi, y in %esi, result in %eax

    + +
    max:
    +  movl %edi,%eax #result=x
    +  cmpl %edi,%esi #if y<=x then
    +  jle endif #return
    +  movl %esi,%eax #result=y
    +endif:
    +  ret
    +
    + +

    We branch (jump) when the condition (y > x) is false, i.e., when (y <= x) +-> This technique is called “coding the false condition first” +or ”taking care of …”

    + +

    Conditional move instruction cmovX

    + +

    What C code looks like when using conditional operator:

    + +
    result=test?val2:val1;
    +return result;
    +
    + +

    What logic of assembly +code looks like when using +cmovX (expressed in C):

    + +
    result=val1;
    +if (test) result = val2;
    +return result;
    +
    + +

    Example: cmovle Src,Dest

    + +

    Alternative: int abs(int x)

    + +

    In C:

    + +
    int abs(int x){
    +  if(x<0)
    +    x=-x;
    +  return x;
    +}
    +
    + +

    in assembly: # x in %edi, result in %eax

    + +
    abs:
    +  movl %edi,%eax # result = x
    +  negl %edi # x = -x
    +  cmpl $0,%eax # if x < 0 then
    +  cmovl %edi,%eax # result = -x
    +  ret
    +
    + +

    Advantage of conditional move cmovX

    + +

    Note about branching:

    + +
      +
    • Branches are very disruptive to instruction flow through +microprocessor CPU pipelines
    • +
    • However, since conditional moves (cmovX) do not +require control transfer (no branching/jumping required), +they are less disruptive
    • +
    • So, gcc tries to use them, but only when safe
    • +
    + +

    What do we mean by “safe”?

    + +
      +
    • In result = test ? aVal : anotherVal; both values +(aVal and anotherVal) are computed so their +computation must be “safe”
    • +
    • Example of unsafe computations: +
        +
      1. Expensive computations val = Test(x) ? Hard1(x) : Hard2(x); +
          +
        • Only makes sense when computations are very simple
        • +
        +
      2. +
      3. Risky computations val = p ? *p : 0; +
          +
        • Only makes sense when computations do not crash the application
        • +
        +
      4. +
      5. Computations with side effects val = x > 0 ? x*=7 : x+=3; +
          +
        • Only makes sense when computations do not have side effects
        • +
        +
      6. +
      +
    • +
    + +

    Homework: Example: alternate int max(int x, int y)

    + +

    version 2 – with cmovX instruction

    + +

    In C:

    + +
    int max(int x, int y){
    +  int result = x;
    +  if(y>x)
    +    result = y;
    +  return result;
    +}
    +
    + +

    In Assembly: # x in %edi, y in %esi, result in %eax

    + +
    max:
    +  movl %edi,%eax #result=x
    +  cmpl %edi,%esi #if y>x then
    +  cmovg %esi,%eax #result=y
    +  ret
    +
    + +

    While loop – “coding the false condition first”

    + +

    int x and int y are arguments to function

    + +

    in C:

    + +
    while(x<y){
    +  //stmts
    +}
    +return;
    +
    + +

    in assembly:

    + +
    loop:
    +  _____
    +  _____
    +  _____
    +  _____ 
    +  _____
    +  _____
    +  _____
    +endloop:
    +  ret
    +
    + +

    Loop Pattern 1

    + +
    loop:
    +  if cond false
    +    goto done:
    +  stmts
    +  goto loop:
    +done:
    +
    + +

    While loop – “jump-to-middle”

    + +

    int x and int y are arguments to function

    + +

    in C:

    + +
    while(x<y){
    +  //stmts
    +}
    +return;
    +
    + +

    in assembly:

    + +
    loop:
    +  # stmts
    +test:
    +  ____
    +  ____
    +  ____
    +  ____
    +  ret
    +
    + +

    Loop Pattern 2

    + +
      goto test:
    +loop:
    +  # stmts
    +test:
    +  if cond true
    +    goto loop:
    +done:
    +
    + +

    Do While loop – “jump-to-middle”

    + +

    int x and int y are arguments to function

    + +

    in C:

    + +
    do{
    +  stmts
    +} while(x<y);
    +return;
    +
    + +

    in assembly:

    + +
    loop:
    +  #stmts
    +test:
    +  ____
    +  ____
    +  ____
    +  ____
    +  ____
    +  ret
    +
    + +

    Loop Pattern 2:

    + +
    (deleted) goto test:
    +loop:
    +  stmts
    +test:
    +  if cond true
    +    goto loop:
    +done:
    +
    + +

    For loop

    + +

    In C:

    + +
    //format: for(initialization; condition testing; increment)
    +for(int i=0;i<n;++i){
    +  //stmts
    +}
    +return;
    +
    + +

    Becomes:

    + +
    i=0; // initialization
    +while(i<n){// condition testing
    +  //stmts
    +  i++; //increment
    +}
    +return;
    +
    + +

    Which becomes, in Assembly:

    + +
      xorl %ecx, %ecx # initialization
    +loop:             # %ecx (i) <- 0
    +  cmpl %edi, %ecx # while i < n true (testing)
    +  jge endloop     # jump when i>=n (false condition)
    +  #stmts
    +  incl %ecx       # i++ increment
    +  jmp loop        # loop again
    +endloop:
    +  ret
    +
    + +

    Summary

    + +

    Compiler can produce different instruction combinations when assembling the same C code.

    + +

    cmp* and test* instructions set condition codes

    + +
      +
    • In x86-64 assembly, there are no conditional statements, however, +we can alter the execution flow of a program by using … +
        +
      • cmp* instruction (compare)
      • +
      • jX instructions (jump)
      • +
      • call and ret instructions
      • +
      • cmovX instructions -> conditional move
      • +
      +
    • +
    • In x86-64 assembly, there are no iterative statements, however, we +can alter the execution flow of a program by using … +
        +
      • cmp* instruction
      • +
      • jX instructions (jump)
      • +
      +
    • +
    • CPU uses these condition codes to decide whether a … +
        +
      • jX instruction (conditional jump) is to be exectued or a
      • +
      • cmovX instruction (conditional move) is to be exectued
      • +
      +
    • +
    • 2 loop patterns: +
        +
      • “coding the false condition first” -> while loops (hence for loops)
      • +
      • “jump-in-middle” -> while, do-while (hence for loops)
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • (highlighted) Overview of Function Call
      • +
      • (highlighted) Memory Layout and Stack - x86-64 instructions and registers
      • +
      • (highlighted) Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/13/13/index.html b/_site/melody/cmpt-295/13/13/index.html new file mode 100644 index 0000000..0d25dc4 --- /dev/null +++ b/_site/melody/cmpt-295/13/13/index.html @@ -0,0 +1,448 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Machine-Level Programming
    • +
    • Lecture 13 – Assembly language – Program Control – cmovX
    • +
    • Iterative Statements – Loops
    • +
    + +

    Last Lecture

    + +
      +
    • In C, we can change the execution flow of a program +
        +
      1. Conditionaly +
          +
        • Conditional statements: if/else, switch
        • +
        • Iterative statements: loops
        • +
        +
      2. +
      3. Unconditionally +
          +
        • Functions calls
        • +
        +
      4. +
      +
    • +
    • In x86-64 assembly, we can also change the execution flow of a program +
        +
      • cmp* instruction (compare)
      • +
      • jX insturction (jump)
      • +
      • call and ret instructions
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • (highlighted) Loops
    • +
    • Function call – Stack
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    Homework: int max(int x, int y)

    + +

    In C:

    + +
    int max(int x,int y){
    +  int result = x;
    +  if(y>x)
    +    result=y;
    +  return result;
    +}
    +
    + +

    version 1 – with jX instruction

    + +

    In Assembly: # x in %edi, y in %esi, result in %eax

    + +
    max:
    +  movl %edi,%eax #result=x
    +  cmpl %edi,%esi #if y<=x then
    +  jle endif #return
    +  movl %esi,%eax #result=y
    +endif:
    +  ret
    +
    + +

    We branch (jump) when the condition (y > x) is false, i.e., when (y <= x) +-> This technique is called “coding the false condition first” +or ”taking care of …”

    + +

    Conditional move instruction cmovX

    + +

    What C code looks like when using conditional operator:

    + +
    result=test?val2:val1;
    +return result;
    +
    + +

    What logic of assembly +code looks like when using +cmovX (expressed in C):

    + +
    result=val1;
    +if (test) result = val2;
    +return result;
    +
    + +

    Example: cmovle Src,Dest

    + +

    Alternative: int abs(int x)

    + +

    In C:

    + +
    int abs(int x){
    +  if(x<0)
    +    x=-x;
    +  return x;
    +}
    +
    + +

    in assembly: # x in %edi, result in %eax

    + +
    abs:
    +  movl %edi,%eax # result = x
    +  negl %edi # x = -x
    +  cmpl $0,%eax # if x < 0 then
    +  cmovl %edi,%eax # result = -x
    +  ret
    +
    + +

    Advantage of conditional move cmovX

    + +

    Note about branching:

    + +
      +
    • Branches are very disruptive to instruction flow through +microprocessor CPU pipelines
    • +
    • However, since conditional moves (cmovX) do not +require control transfer (no branching/jumping required), +they are less disruptive
    • +
    • So, gcc tries to use them, but only when safe
    • +
    + +

    What do we mean by “safe”?

    + +
      +
    • In result = test ? aVal : anotherVal; both values +(aVal and anotherVal) are computed so their +computation must be “safe”
    • +
    • Example of unsafe computations: +
        +
      1. Expensive computations val = Test(x) ? Hard1(x) : Hard2(x); +
          +
        • Only makes sense when computations are very simple
        • +
        +
      2. +
      3. Risky computations val = p ? *p : 0; +
          +
        • Only makes sense when computations do not crash the application
        • +
        +
      4. +
      5. Computations with side effects val = x > 0 ? x*=7 : x+=3; +
          +
        • Only makes sense when computations do not have side effects
        • +
        +
      6. +
      +
    • +
    + +

    Homework: Example: alternate int max(int x, int y)

    + +

    version 2 – with cmovX instruction

    + +

    In C:

    + +
    int max(int x, int y){
    +  int result = x;
    +  if(y>x)
    +    result = y;
    +  return result;
    +}
    +
    + +

    In Assembly: # x in %edi, y in %esi, result in %eax

    + +
    max:
    +  movl %edi,%eax #result=x
    +  cmpl %edi,%esi #if y>x then
    +  cmovg %esi,%eax #result=y
    +  ret
    +
    + +

    While loop – “coding the false condition first” (updated)

    + +

    int x and int y are arguments to function

    + +

    in C:

    + +
    while(x<y){
    +  //stmts
    +}
    +return;
    +
    + +

    Steps:

    + +
      +
    1. Analyze the condition: x<y
    2. +
    3. write cmpl %esi,%edi
    4. +
    5. List all outcomes: +
        +
      • xy>0    x>yx-y > 0 \implies x>y: exits loop
      • +
      • xy<0    x<yx-y < 0 \implies x< y: loops
      • +
      • xy=0    x=yx-y = 0 \implies x=y: exits loop
      • +
      +
    6. +
    7. Which outcomes “exit loop”?
    8. +
    9. Write for false condition i.e. g & e (jge)
    10. +
    + +

    in assembly:

    + +
    loop: # x in %edi, y in %esi
    +  cmpl %esi,%edi # cmpl y,x
    +  jge endloop
    +  #stmts in loop
    +  jmp loop
    +endloop:
    +  ret
    +
    + +

    Loop Pattern 1

    + +
    loop:
    +  if cond false
    +    goto done:
    +  stmts
    +  goto loop:
    +done:
    +
    + +

    While loop – “jump-to-middle” (updated)

    + +

    int x and int y are arguments to function

    + +

    in C:

    + +
    while(x<y){ //step 1
    +  //stmts
    +}
    +return;
    +
    + +

    Steps:

    + +
      +
    1. (shown in C code)
    2. +
    3. (shwon in assembly)
    4. +
    5. All possible values +
        +
      • xy>0    x>yx-y >0 \implies x>y: exits loop, g
      • +
      • xy<0    x<yx-y < 0 \implies x< y: loops, l
      • +
      • xy=0    x=yx-y = 0 \implies x=y: exits loops, e
      • +
      +
    6. +
    7. Consider outcome ???(can’t read) “loops”
    8. +
    + +

    in assembly:

    + +
      jmp test
    +loop:
    +  # stmts
    +test:
    +  cmpl %esi,%edi #cmpl y,x (step 2)
    +  jl loop (step 5)
    +  ret
    +
    + +

    Loop Pattern 2

    + +
      goto test:
    +loop:
    +  # stmts
    +test:
    +  if cond true
    +    goto loop:
    +done:
    +
    + +

    Do While loop – “jump-to-middle” (updated)

    + +

    int x and int y are arguments to function

    + +

    in C:

    + +
    do{
    +  stmts
    +} while(x<y);
    +return;
    +
    + +

    in assembly:

    + +
    loop:
    +  #stmts
    +test:
    +  jmpl %esi,%edi
    +  jl loop
    +  ret
    +
    + +

    Loop Pattern 2:

    + +
    (deleted) goto test:
    +loop:
    +  stmts
    +test:
    +  if cond true
    +    goto loop:
    +done:
    +
    + +

    For loop (updated)

    + +

    In C:

    + +
    //format: for(initialization; condition testing; increment)
    +for(int i=0;i<n;++i){
    +  //stmts
    +}
    +return;
    +
    + +

    Becomes:

    + +
    i=0; // initialization
    +while(i<n){// condition testing
    +  //stmts
    +  i++; //increment
    +}
    +return;
    +
    + +

    Questions:

    + +
      +
    1. Here are we coding the false condition first?
    2. +
    3. Which loop patterns are we using?
    4. +
    + +

    Which becomes, in Assembly:

    + +
      xorl %ecx, %ecx # initialization
    +loop:             # %ecx (i) <- 0
    +  cmpl %edi, %ecx # while i < n true (testing)
    +  jge endloop     # jump when i>=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
    +
    + +

    Summary

    + +

    Compiler can produce different instruction combinations when assembling the same C code.

    + +

    cmp* and test* instructions set condition codes

    + +
      +
    • In x86-64 assembly, there are no conditional statements, however, +we can alter the execution flow of a program by using … +
        +
      • cmp* instruction (compare)
      • +
      • jX instructions (jump)
      • +
      • call and ret instructions
      • +
      • cmovX instructions -> conditional move
      • +
      +
    • +
    • In x86-64 assembly, there are no iterative statements, however, we +can alter the execution flow of a program by using … +
        +
      • cmp* instruction
      • +
      • jX instructions (jump)
      • +
      +
    • +
    • CPU uses these condition codes to decide whether a … +
        +
      • jX instruction (conditional jump) is to be exectued or a
      • +
      • cmovX instruction (conditional move) is to be exectued
      • +
      +
    • +
    • 2 loop patterns: +
        +
      • “coding the false condition first” -> while loops (hence for loops)
      • +
      • “jump-in-middle” -> while, do-while (hence for loops)
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • (highlighted) Overview of Function Call
      • +
      • (highlighted) Memory Layout and Stack - x86-64 instructions and registers
      • +
      • (highlighted) Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/13/Lecture_13_Machine_Level_Prog_Loops.pdf b/_site/melody/cmpt-295/13/Lecture_13_Machine_Level_Prog_Loops.pdf new file mode 100644 index 0000000..b9f7e3e Binary files /dev/null and b/_site/melody/cmpt-295/13/Lecture_13_Machine_Level_Prog_Loops.pdf differ diff --git a/_site/melody/cmpt-295/13/Lecture_13_Machine_Level_Prog_Loops_Annotated.pdf b/_site/melody/cmpt-295/13/Lecture_13_Machine_Level_Prog_Loops_Annotated.pdf new file mode 100644 index 0000000..4330c78 Binary files /dev/null and b/_site/melody/cmpt-295/13/Lecture_13_Machine_Level_Prog_Loops_Annotated.pdf differ diff --git a/_site/melody/cmpt-295/14/14-updated.html b/_site/melody/cmpt-295/14/14-updated.html new file mode 100644 index 0000000..e622403 --- /dev/null +++ b/_site/melody/cmpt-295/14/14-updated.html @@ -0,0 +1,1039 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Machine-Level Programming
    • +
    • Lecture 14 +
        +
      • Assembly language
      • +
      • Program Control
      • +
      • Function Call and Stack
      • +
      • Passing Control
      • +
      +
    • +
    + +

    Last Lecture

    + +
      +
    • In x86-64 assembly, there are no iterative statements
    • +
    • To alter the execution flow, compiler generates code sequence +that implements these iterative statements (while, do-while +and for loops) using branching method: +
        +
      • cmp* instruction
      • +
      • jX instructions (jump)
      • +
      +
    • +
    • 2 loop patterns: +
        +
      • “coding the false condition first” -> while loops (hence for loops)
      • +
      • “jump-in-middle” -> while, do-while (hence for loops)
      • +
      +
    • +
    + +

    While loop – Question from last lecture “coding the false condition first” (updated)

    + +

    in C:

    + +
    while(x<y){ // 1
    +  //stmts
    +}
    +
    + +

    Cases:

    + +
      +
    • yx>0    y>xy-x >0 \implies y>x: loops, g
    • +
    • yx<0    y<xy-x <0 \implies y< x: exits loop, l
    • +
    • yx=0    y=xy-x =0 \implies y=x: exists loops, e
    • +
    + +

    in assembly: # x in %edi, y in %esi

    + +
    loop:
    +  cmpl %edi, %esi #2
    +  jl endloop
    +  # stmts
    +  jmp loop
    +endloop:
    +  ret
    +
    + +

    Loop Pattern 1

    + +
    loop:
    +  if cond false
    +    goto done:
    +  stmts
    +  goto loop:
    +done:
    +
    + +

    Would this assembly code be the equivalent of our C code?

    + +

    Not quite! We need jle

    + +

    Demo: alternative way of implementing if/else in assembly language (updated)

    + +

    Lecture 12 – ifelse.c and ifelse.s

    + +

    We shall have a look at this code during lecture 15, our review lecture!

    + +

    For loop - Homework

    + +

    In C:

    + +
    format: for(initialization;condition testing;increment){
    +for(i=0;i<;i++){
    +  // stmts
    +}
    +return;
    +
    + +

    Becomes:

    + +
    i=0; //initialization
    +while(i<n){//condition
    +  //stmts
    +  i++; //increment
    +}
    +return;
    +
    + +

    In Assembly:

    + +
      xorl %ecx, %ecx #initialization: %ecx (i) <- 0
    +loop:
    +  cmpl %edi, %ecx #i-n?0 testing
    +  jge endloop #i-n>=0 false condition
    +  #stmts
    +  incl %ecx #i++ increment
    +  jmp loop #loop again
    +endloop:
    +  ret
    +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • (highlighted) Overview of Function Call
      • +
      • (highlighted) Memory Layout and Stack - x86-64 instructions and registers
      • +
      • (highlighted) Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    What happens when a function (caller) calls another function (callee)?

    + +
      +
    1. Control is passed (PC is set) … +
        +
      • To the beginning of the code in callee function
      • +
      • Back to where callee function was called in caller function
      • +
      +
    2. +
    3. Data is passed … +
        +
      • To callee function via function parameter(s)
      • +
      • Back to caller function via return value
      • +
      +
    4. +
    5. Memory is … +
        +
      • Allocated during callee function execution
      • +
      • Deallocated upon return to caller function
      • +
      +
    6. +
    + +

    Code example 1:

    + +
    void who(...) {
    +  int sum = 0;
    +  ...
    +  y = amI(x);
    +  sum = x + y;
    +  return;
    +}
    +
    + +

    Code example 2:

    + +
    int amI(int i)
    +{
    +  int t = 3*i;
    +  int v[10];
    +  ...
    +  return v[t];
    +}
    +
    + +

    Above mechanisms implemented with machine code instructions and described as a set of conventions (ISA)

    + +

    Remember from Lecture 2: Closer look at memory

    + +
      +
    • Seen as a linear array of bytes
    • +
    • 1 byte (8 bits) smallest addressable +unit of memory +
        +
      • Byte-addressable
      • +
      +
    • +
    • Each byte has a unique address
    • +
    • Computer reads a “word size” worth +of bits at a time
    • +
    • Compressed view of memory
    • +
    + +

    Compressed view of memory w/ addresses in cells:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-8
    ...........................
    0x0018
    0x0010
    0x0008
    0x00000x00010x00020x00030x00040x00050x00060x00070x0008
    + +

    Memory Layout

    + +

    Segments:

    + +
      +
    • Stack +
        +
      • Runtime stack, e. g., local variables
      • +
      +
    • +
    • Heap +
        +
      • Dynamically allocated as needed, explicitly released (freed)
      • +
      • When call malloc(), free(), new(), delete, …
      • +
      +
    • +
    • Data +
        +
      • Statically allocated data, e.g., global vars, static vars, string constants
      • +
      +
    • +
    • Text +
        +
      • Executable machine instructions
      • +
      • Read-only
      • +
      +
    • +
    • Shared Libraries +
        +
      • Executable machine instructions
      • +
      • Read-only
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    0x00007FFFFFFFFFFFStack (down arrow)
     Shared Libraries
     Heap (up arrow)
     Data
    0x0000000000400000Text
    0x0000000000000000 
    + +

    Memory Allocation Example

    + +

    Where does everything go?

    + +
    #include ...
    +char hugeArray[1 << 31]; /* 231 = 2GB */
    +int global = 0;
    +int useless(){ return 0; }
    +int main ()
    +{
    +  void *ptr1, *ptr2;
    +  int local = 0;
    +  ptr1 = malloc(1 << 28); /* 228 = 256 MB*/
    +  ptr2 = malloc(1 << 8); /* 28 = 256 B*/
    +
    +  /* Some print statements ... */
    +}
    +
    + + + + + + + + + + + + + + +
    M[]
    Stack (down arrow)
    ...
    Shared Libraries
    ...
    Heap (up arrow)
    Data
    Text
    + +

    Closer look at function call pattern

    + +

    A function may call a function, which may call a function, which may call a function, …

    + +

    Code example 1:

    + +
    who(...) {
    +  ...
    +  ...
    +  are();
    +  ...
    +  ...
    +}
    +
    + +

    Code example 2:

    + +
    are(...) {
    +  ...
    +  you();
    +  ...
    +  you();
    +  ...
    +}
    +
    + +

    Example 3:

    + +
    you(...) {
    +  ...
    +  ...
    +  ...
    +  ...
    +  ...
    +}
    +
    + +
      +
    • When a function (callee) terminates and returns, its most +recent caller resumes which eventually terminates and returns +and its most recent caller resumes …
    • +
    • Does this pattern remind you of anything?
    • +
    + +

    Stack

    + +

    Definition: A stack is a last-in-first-out (LIFO) data structure with two characteristic operations:

    + +
      +
    • push(data)
    • +
    • data = pop() or pop(&data)
    • +
    + +

    Do not have access to anything except what is on (at) top.

    + +

    Image of stack of dinner plates.

    + +

    Closer look at stack

    + +
      +
    • x86-64 assembly language has stack-specific
    • +
    • %rsp +
        +
      • Points to address of last used byte on stack
      • +
      • Initialized to “top of stack” at startup
      • +
      • Stack grows towards low memory address
      • +
      +
    • +
    • pushq src
    • +
    • popq dest
    • +
    + +

    Diagram of stack: +at the top of the stack is %rsp; the stack grows down. +Memory addresses decreese as they go down the stack.

    + +

    x86-64 stack instruction: push

    + +
      +
    • pushq src +
        +
      • Fetch value of operand src
      • +
      • Decrement %rsp by 8
      • +
      • Write value at address given by %rsp
      • +
      +
    • +
    + +

    Diagram of stack after 3 pushes (transcriber’s note: these diagrams are probably wrong, but I need to describe what is there):

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
    %rsp Top
    + +

    x86-64 stack instruction: pop

    + +

    … we pop once.

    + +
      +
    • popq dest +
        +
      • Read value at %rsp (address) and store it in operand dest (must be register)
      • +
      • Increment %rsp by 8
      • +
      +
    • +
    + +

    After we pop once:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] stackName
     data 
     data 
    %rspdataTop
     
    + +

    Passing control mechanism x86-64 instruction: call and ret

    + +
      +
    • call func +
        +
      • push PC
      • +
      • jmp func (set PC to func)
      • +
      +
    • +
    + +

    Effect: return address, i.e., the +address of the instruction after +call func (held in PC) is +pushed onto the stack

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
    %rsp Top
    + +

    Passing control mechanism x86-64 instruction: call and ret

    + +
      +
    • ret +
        +
      • popq PC
      • +
      • jmp PC
      • +
      +
    • +
    + +

    Effect: return address, i.e., the +address of instruction after +call func, is pop’ed from +the stack and stored in PC

    + +

    After returning from the call …

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     data 
    %rsp Top
    + +

    Example

    + +

    Example pt. 1, in C:

    + +
    void multstore(long x, long y, long *dest) {
    +  long t = mult2(x, y);
    +  *dest = t;
    +  return;
    +}
    +
    + +

    Example pt. 1, in Assembly:

    + +
    0000000000400540 <multistore>:
    +  400540: push %rbx #Save %rbx
    +  400541: mov %rdx,%rbx #save Dest
    +  400544: callq 400550 <mult2> #mult2(x,y)
    +  400549: mov %rax,(%rbx) #save as dest
    +  40054c: pop %rbx #restore %rbx
    +  40054d: retq #return
    +
    + +

    Example pt. 2, in C:

    + +
    long mult2(long a, long b) {
    +  long s = a * b;
    +  return s;
    +}
    +
    + +

    Example pt. 2, in Assembly:

    + +
    0000000000400550 <mult2>:
    +  400550: mov %rdi,%rax #a
    +  400553: imul %rsi,%rax #a*b
    +  400557: retq #return
    +
    + +

    Example – Steps 1 and 2

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x120
    %rax 
    %rip0x400540
    %rdi 
    %rsi 
    %rdx 
    + +

    Example – Steps 3 and 4

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
     %rbx 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x118
    %rax 
    %rip400544
    %rdi 
    %rsi 
    %rdx 
    + +

    Example – Steps 5 and 6

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
     %rbx 
     0x400549 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x110
    %rax 
    %rip0x400553
    %rdi 
    %rsi 
    %rdx 
    + +

    Example – Steps 7, 8 and 9

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
     %rbx 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x118
    %rax 
    %rip0x400549
    %rdi 
    %rsi 
    %rdx 
    + +

    Summary

    + +
      +
    • Function call mechanisms: passing control and data, managing memory
    • +
    • Memory layout +
        +
      • Stack (local variables …)
      • +
      • Heap (dynamically allocated data)
      • +
      • Data (statically allocated data)
      • +
      • Text / Shared Libraries (program code)
      • +
      +
    • +
    • “Stack” is the data structure used for function call / return +
        +
      • If multstore calls mult2, then mult2 returns before multstore
      • +
      +
    • +
    • x86-64 stack register and instructions: stack pointer rsp, push and pop
    • +
    • x86-64 function call instructions: call and ret
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • (highlighted) Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/14/14-updated/index.html b/_site/melody/cmpt-295/14/14-updated/index.html new file mode 100644 index 0000000..898e066 --- /dev/null +++ b/_site/melody/cmpt-295/14/14-updated/index.html @@ -0,0 +1,1041 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Machine-Level Programming
    • +
    • Lecture 14 +
        +
      • Assembly language
      • +
      • Program Control
      • +
      • Function Call and Stack
      • +
      • Passing Control
      • +
      +
    • +
    + +

    Last Lecture

    + +
      +
    • In x86-64 assembly, there are no iterative statements
    • +
    • To alter the execution flow, compiler generates code sequence +that implements these iterative statements (while, do-while +and for loops) using branching method: +
        +
      • cmp* instruction
      • +
      • jX instructions (jump)
      • +
      +
    • +
    • 2 loop patterns: +
        +
      • “coding the false condition first” -> while loops (hence for loops)
      • +
      • “jump-in-middle” -> while, do-while (hence for loops)
      • +
      +
    • +
    + +

    While loop – Question from last lecture “coding the false condition first” (updated)

    + +

    in C:

    + +
    while(x<y){ // 1
    +  //stmts
    +}
    +
    + +

    Cases:

    + +
      +
    • yx>0    y>xy-x >0 \implies y>x: loops, g
    • +
    • yx<0    y<xy-x <0 \implies y< x: exits loop, l
    • +
    • yx=0    y=xy-x =0 \implies y=x: exists loops, e
    • +
    + +

    in assembly: # x in %edi, y in %esi

    + +
    loop:
    +  cmpl %edi, %esi #2
    +  jl endloop
    +  # stmts
    +  jmp loop
    +endloop:
    +  ret
    +
    + +

    Loop Pattern 1

    + +
    loop:
    +  if cond false
    +    goto done:
    +  stmts
    +  goto loop:
    +done:
    +
    + +

    Would this assembly code be the equivalent of our C code?

    + +

    Not quite! We need jle

    + +

    Demo: alternative way of implementing if/else in assembly language (updated)

    + +

    Lecture 12 – ifelse.c and ifelse.s

    + +

    We shall have a look at this code during lecture 15, our review lecture!

    + +

    For loop - Homework

    + +

    In C:

    + +
    format: for(initialization;condition testing;increment){
    +for(i=0;i<;i++){
    +  // stmts
    +}
    +return;
    +
    + +

    Becomes:

    + +
    i=0; //initialization
    +while(i<n){//condition
    +  //stmts
    +  i++; //increment
    +}
    +return;
    +
    + +

    In Assembly:

    + +
      xorl %ecx, %ecx #initialization: %ecx (i) <- 0
    +loop:
    +  cmpl %edi, %ecx #i-n?0 testing
    +  jge endloop #i-n>=0 false condition
    +  #stmts
    +  incl %ecx #i++ increment
    +  jmp loop #loop again
    +endloop:
    +  ret
    +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • (highlighted) Overview of Function Call
      • +
      • (highlighted) Memory Layout and Stack - x86-64 instructions and registers
      • +
      • (highlighted) Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    What happens when a function (caller) calls another function (callee)?

    + +
      +
    1. Control is passed (PC is set) … +
        +
      • To the beginning of the code in callee function
      • +
      • Back to where callee function was called in caller function
      • +
      +
    2. +
    3. Data is passed … +
        +
      • To callee function via function parameter(s)
      • +
      • Back to caller function via return value
      • +
      +
    4. +
    5. Memory is … +
        +
      • Allocated during callee function execution
      • +
      • Deallocated upon return to caller function
      • +
      +
    6. +
    + +

    Code example 1:

    + +
    void who(...) {
    +  int sum = 0;
    +  ...
    +  y = amI(x);
    +  sum = x + y;
    +  return;
    +}
    +
    + +

    Code example 2:

    + +
    int amI(int i)
    +{
    +  int t = 3*i;
    +  int v[10];
    +  ...
    +  return v[t];
    +}
    +
    + +

    Above mechanisms implemented with machine code instructions and described as a set of conventions (ISA)

    + +

    Remember from Lecture 2: Closer look at memory

    + +
      +
    • Seen as a linear array of bytes
    • +
    • 1 byte (8 bits) smallest addressable +unit of memory +
        +
      • Byte-addressable
      • +
      +
    • +
    • Each byte has a unique address
    • +
    • Computer reads a “word size” worth +of bits at a time
    • +
    • Compressed view of memory
    • +
    + +

    Compressed view of memory w/ addresses in cells:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-8
    ...........................
    0x0018
    0x0010
    0x0008
    0x00000x00010x00020x00030x00040x00050x00060x00070x0008
    + +

    Memory Layout

    + +

    Segments:

    + +
      +
    • Stack +
        +
      • Runtime stack, e. g., local variables
      • +
      +
    • +
    • Heap +
        +
      • Dynamically allocated as needed, explicitly released (freed)
      • +
      • When call malloc(), free(), new(), delete, …
      • +
      +
    • +
    • Data +
        +
      • Statically allocated data, e.g., global vars, static vars, string constants
      • +
      +
    • +
    • Text +
        +
      • Executable machine instructions
      • +
      • Read-only
      • +
      +
    • +
    • Shared Libraries +
        +
      • Executable machine instructions
      • +
      • Read-only
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    0x00007FFFFFFFFFFFStack (down arrow)
     Shared Libraries
     Heap (up arrow)
     Data
    0x0000000000400000Text
    0x0000000000000000 
    + +

    Memory Allocation Example

    + +

    Where does everything go?

    + +
    #include ...
    +char hugeArray[1 << 31]; /* 231 = 2GB */
    +int global = 0;
    +int useless(){ return 0; }
    +int main ()
    +{
    +  void *ptr1, *ptr2;
    +  int local = 0;
    +  ptr1 = malloc(1 << 28); /* 228 = 256 MB*/
    +  ptr2 = malloc(1 << 8); /* 28 = 256 B*/
    +
    +  /* Some print statements ... */
    +}
    +
    + + + + + + + + + + + + + + +
    M[]
    Stack (down arrow)
    ...
    Shared Libraries
    ...
    Heap (up arrow)
    Data
    Text
    + +

    Closer look at function call pattern

    + +

    A function may call a function, which may call a function, which may call a function, …

    + +

    Code example 1:

    + +
    who(...) {
    +  ...
    +  ...
    +  are();
    +  ...
    +  ...
    +}
    +
    + +

    Code example 2:

    + +
    are(...) {
    +  ...
    +  you();
    +  ...
    +  you();
    +  ...
    +}
    +
    + +

    Example 3:

    + +
    you(...) {
    +  ...
    +  ...
    +  ...
    +  ...
    +  ...
    +}
    +
    + +
      +
    • When a function (callee) terminates and returns, its most +recent caller resumes which eventually terminates and returns +and its most recent caller resumes …
    • +
    • Does this pattern remind you of anything?
    • +
    + +

    Stack

    + +

    Definition: A stack is a last-in-first-out (LIFO) data structure with two characteristic operations:

    + +
      +
    • push(data)
    • +
    • data = pop() or pop(&data)
    • +
    + +

    Do not have access to anything except what is on (at) top.

    + +

    Image of stack of dinner plates.

    + +

    Closer look at stack

    + +
      +
    • x86-64 assembly language has stack-specific
    • +
    • %rsp +
        +
      • Points to address of last used byte on stack
      • +
      • Initialized to “top of stack” at startup
      • +
      • Stack grows towards low memory address
      • +
      +
    • +
    • pushq src
    • +
    • popq dest
    • +
    + +

    Diagram of stack: +at the top of the stack is %rsp; the stack grows down. +Memory addresses decreese as they go down the stack.

    + +

    x86-64 stack instruction: push

    + +
      +
    • pushq src +
        +
      • Fetch value of operand src
      • +
      • Decrement %rsp by 8
      • +
      • Write value at address given by %rsp
      • +
      +
    • +
    + +

    Diagram of stack after 3 pushes (transcriber’s note: these diagrams are probably wrong, but I need to describe what is there):

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
    %rsp Top
    + +

    x86-64 stack instruction: pop

    + +

    … we pop once.

    + +
      +
    • popq dest +
        +
      • Read value at %rsp (address) and store it in operand dest (must be register)
      • +
      • Increment %rsp by 8
      • +
      +
    • +
    + +

    After we pop once:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] stackName
     data 
     data 
    %rspdataTop
     
    + +

    Passing control mechanism x86-64 instruction: call and ret

    + +
      +
    • call func +
        +
      • push PC
      • +
      • jmp func (set PC to func)
      • +
      +
    • +
    + +

    Effect: return address, i.e., the +address of the instruction after +call func (held in PC) is +pushed onto the stack

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
    %rsp Top
    + +

    Passing control mechanism x86-64 instruction: call and ret

    + +
      +
    • ret +
        +
      • popq PC
      • +
      • jmp PC
      • +
      +
    • +
    + +

    Effect: return address, i.e., the +address of instruction after +call func, is pop’ed from +the stack and stored in PC

    + +

    After returning from the call …

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     data 
    %rsp Top
    + +

    Example

    + +

    Example pt. 1, in C:

    + +
    void multstore(long x, long y, long *dest) {
    +  long t = mult2(x, y);
    +  *dest = t;
    +  return;
    +}
    +
    + +

    Example pt. 1, in Assembly:

    + +
    0000000000400540 <multistore>:
    +  400540: push %rbx #Save %rbx
    +  400541: mov %rdx,%rbx #save Dest
    +  400544: callq 400550 <mult2> #mult2(x,y)
    +  400549: mov %rax,(%rbx) #save as dest
    +  40054c: pop %rbx #restore %rbx
    +  40054d: retq #return
    +
    + +

    Example pt. 2, in C:

    + +
    long mult2(long a, long b) {
    +  long s = a * b;
    +  return s;
    +}
    +
    + +

    Example pt. 2, in Assembly:

    + +
    0000000000400550 <mult2>:
    +  400550: mov %rdi,%rax #a
    +  400553: imul %rsi,%rax #a*b
    +  400557: retq #return
    +
    + +

    Example – Steps 1 and 2

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x120
    %rax 
    %rip0x400540
    %rdi 
    %rsi 
    %rdx 
    + +

    Example – Steps 3 and 4

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
     %rbx 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x118
    %rax 
    %rip400544
    %rdi 
    %rsi 
    %rdx 
    + +

    Example – Steps 5 and 6

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
     %rbx 
     0x400549 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x110
    %rax 
    %rip0x400553
    %rdi 
    %rsi 
    %rdx 
    + +

    Example – Steps 7, 8 and 9

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
     %rbx 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x118
    %rax 
    %rip0x400549
    %rdi 
    %rsi 
    %rdx 
    + +

    Summary

    + +
      +
    • Function call mechanisms: passing control and data, managing memory
    • +
    • Memory layout +
        +
      • Stack (local variables …)
      • +
      • Heap (dynamically allocated data)
      • +
      • Data (statically allocated data)
      • +
      • Text / Shared Libraries (program code)
      • +
      +
    • +
    • “Stack” is the data structure used for function call / return +
        +
      • If multstore calls mult2, then mult2 returns before multstore
      • +
      +
    • +
    • x86-64 stack register and instructions: stack pointer rsp, push and pop
    • +
    • x86-64 function call instructions: call and ret
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • (highlighted) Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/14/14.html b/_site/melody/cmpt-295/14/14.html new file mode 100644 index 0000000..c8e8192 --- /dev/null +++ b/_site/melody/cmpt-295/14/14.html @@ -0,0 +1,1029 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Machine-Level Programming
    • +
    • Lecture 14 +
        +
      • Assembly language
      • +
      • Program Control
      • +
      • Function Call and Stack
      • +
      • Passing Control
      • +
      +
    • +
    + +

    Demo: alternative way of implementing if/else in assembly language

    + +
      +
    • Lecture 12 – ifelse.c and ifelse.s
    • +
    + +

    Last Lecture

    + +
      +
    • In x86-64 assembly, there are no iterative statements
    • +
    • To alter the execution flow, compiler generates code sequence +that implements these iterative statements (while, do-while +and for loops) using branching method: +
        +
      • cmp* instruction
      • +
      • jX instructions (jump)
      • +
      +
    • +
    • 2 loop patterns: +
        +
      • “coding the false condition first” -> while loops (hence for loops)
      • +
      • “jump-in-middle” -> while, do-while (hence for loops)
      • +
      +
    • +
    + +

    While loop – Question from last lecture “coding the false condition first”

    + +

    in C:

    + +
    while(x<y){
    +  //stmts
    +}
    +
    + +

    in assembly: # x in %edi, y in %esi

    + +
    loop:
    +  cmpl %edi, %esi
    +  jl endloop
    +  # stmts
    +  jmp loop
    +endloop:
    +  ret
    +
    + +

    Loop Pattern 1

    + +
    loop:
    +  if cond false
    +    goto done:
    +  stmts
    +  goto loop:
    +done:
    +
    + +

    Would this assembly code be the equivalent of our C code?

    + +

    For loop - Homework

    + +

    In C:

    + +
    format: for(initialization;condition testing;increment){
    +for(i=0;i<;i++){
    +  // stmts
    +}
    +return;
    +
    + +

    Becomes:

    + +
    i=0; //initialization
    +while(i<n){//condition
    +  //stmts
    +  i++; //increment
    +}
    +return;
    +
    + +

    In Assembly:

    + +
      xorl %ecx, %ecx #initialization: %ecx (i) <- 0
    +loop:
    +  cmpl %edi, %ecx #i-n?0 testing
    +  jge endloop #i-n>=0 false condition
    +  #stmts
    +  incl %ecx #i++ increment
    +  jmp loop #loop again
    +endloop:
    +  ret
    +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • (highlighted) Overview of Function Call
      • +
      • (highlighted) Memory Layout and Stack - x86-64 instructions and registers
      • +
      • (highlighted) Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    What happens when a function (caller) calls another function (callee)?

    + +
      +
    1. Control is passed (PC is set) … +
        +
      • To the beginning of the code in callee function
      • +
      • Back to where callee function was called in caller function
      • +
      +
    2. +
    3. Data is passed … +
        +
      • To callee function via function parameter(s)
      • +
      • Back to caller function via return value
      • +
      +
    4. +
    5. Memory is … +
        +
      • Allocated during callee function execution
      • +
      • Deallocated upon return to caller function
      • +
      +
    6. +
    + +

    Code example 1:

    + +
    void who(...) {
    +  int sum = 0;
    +  ...
    +  y = amI(x);
    +  sum = x + y;
    +  return;
    +}
    +
    + +

    Code example 2:

    + +
    int amI(int i)
    +{
    +  int t = 3*i;
    +  int v[10];
    +  ...
    +  return v[t];
    +}
    +
    + +

    Above mechanisms implemented with machine code instructions and described as a set of conventions (ISA)

    + +

    Remember from Lecture 2: Closer look at memory

    + +
      +
    • Seen as a linear array of bytes
    • +
    • 1 byte (8 bits) smallest addressable +unit of memory +
        +
      • Byte-addressable
      • +
      +
    • +
    • Each byte has a unique address
    • +
    • Computer reads a “word size” worth +of bits at a time
    • +
    • Compressed view of memory
    • +
    + +

    Compressed view of memory w/ addresses in cells:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-8
    ...........................
    0x0018
    0x0010
    0x0008
    0x00000x00010x00020x00030x00040x00050x00060x00070x0008
    + +

    Memory Layout

    + +

    Segments:

    + +
      +
    • Stack +
        +
      • Runtime stack, e. g., local variables
      • +
      +
    • +
    • Heap +
        +
      • Dynamically allocated as needed, explicitly released (freed)
      • +
      • When call malloc(), free(), new(), delete, …
      • +
      +
    • +
    • Data +
        +
      • Statically allocated data, e.g., global vars, static vars, string constants
      • +
      +
    • +
    • Text +
        +
      • Executable machine instructions
      • +
      • Read-only
      • +
      +
    • +
    • Shared Libraries +
        +
      • Executable machine instructions
      • +
      • Read-only
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    0x00007FFFFFFFFFFFStack (down arrow)
     Shared Libraries
     Heap (up arrow)
     Data
    0x0000000000400000Text
    0x0000000000000000 
    + +

    Memory Allocation Example

    + +

    Where does everything go?

    + +
    #include ...
    +char hugeArray[1 << 31]; /* 231 = 2GB */
    +int global = 0;
    +int useless(){ return 0; }
    +int main ()
    +{
    +  void *ptr1, *ptr2;
    +  int local = 0;
    +  ptr1 = malloc(1 << 28); /* 228 = 256 MB*/
    +  ptr2 = malloc(1 << 8); /* 28 = 256 B*/
    +
    +  /* Some print statements ... */
    +}
    +
    + + + + + + + + + + + + + + +
    M[]
    Stack (down arrow)
    ...
    Shared Libraries
    ...
    Heap (up arrow)
    Data
    Text
    + +

    Closer look at function call pattern

    + +

    A function may call a function, which may call a function, which may call a function, …

    + +

    Code example 1:

    + +
    who(...) {
    +  ...
    +  ...
    +  are();
    +  ...
    +  ...
    +}
    +
    + +

    Code example 2:

    + +
    are(...) {
    +  ...
    +  you();
    +  ...
    +  you();
    +  ...
    +}
    +
    + +

    Example 3:

    + +
    you(...) {
    +  ...
    +  ...
    +  ...
    +  ...
    +  ...
    +}
    +
    + +
      +
    • When a function (callee) terminates and returns, its most +recent caller resumes which eventually terminates and returns +and its most recent caller resumes …
    • +
    • Does this pattern remind you of anything?
    • +
    + +

    Stack

    + +

    Definition: A stack is a last-in-first-out (LIFO) data structure with two characteristic operations:

    + +
      +
    • push(data)
    • +
    • data = pop() or pop(&data)
    • +
    + +

    Do not have access to anything except what is on (at) top.

    + +

    Image of stack of dinner plates.

    + +

    Closer look at stack

    + +
      +
    • x86-64 assembly language has stack-specific
    • +
    • %rsp +
        +
      • Points to address of last used byte on stack
      • +
      • Initialized to “top of stack” at startup
      • +
      • Stack grows towards low memory address
      • +
      +
    • +
    • pushq src
    • +
    • popq dest
    • +
    + +

    Diagram of stack: +at the top of the stack is %rsp; the stack grows down. +Memory addresses decreese as they go down the stack.

    + +

    x86-64 stack instruction: push

    + +
      +
    • pushq src +
        +
      • Fetch value of operand src
      • +
      • Decrement %rsp by 8
      • +
      • Write value at address given by %rsp
      • +
      +
    • +
    + +

    Diagram of stack after 3 pushes (transcriber’s note: these diagrams are probably wrong, but I need to describe what is there):

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
    %rsp Top
    + +

    x86-64 stack instruction: pop

    + +

    … we pop once.

    + +
      +
    • popq dest +
        +
      • Read value at %rsp (address) and store it in operand dest (must be register)
      • +
      • Increment %rsp by 8
      • +
      +
    • +
    + +

    After we pop once:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] stackName
     data 
     data 
    %rspdataTop
     
    + +

    Passing control mechanism x86-64 instruction: call and ret

    + +
      +
    • call func +
        +
      • push PC
      • +
      • jmp func (set PC to func)
      • +
      +
    • +
    + +

    Effect: return address, i.e., the +address of the instruction after +call func (held in PC) is +pushed onto the stack

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
    %rsp Top
    + +

    Passing control mechanism x86-64 instruction: call and ret

    + +
      +
    • ret +
        +
      • popq PC
      • +
      • jmp PC
      • +
      +
    • +
    + +

    Effect: return address, i.e., the +address of instruction after +call func, is pop’ed from +the stack and stored in PC

    + +

    After returning from the call …

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     data 
    %rsp Top
    + +

    Example

    + +

    Example pt. 1, in C:

    + +
    void multstore(long x, long y, long *dest) {
    +  long t = mult2(x, y);
    +  *dest = t;
    +  return;
    +}
    +
    + +

    Example pt. 1, in Assembly:

    + +
    0000000000400540 <multistore>:
    +  400540: push %rbx #Save %rbx
    +  400541: mov %rdx,%rbx #save Dest
    +  400544: callq 400550 <mult2> #mult2(x,y)
    +  400549: mov %rax,(%rbx) #save as dest
    +  40054c: pop %rbx #restore %rbx
    +  40054d: retq #return
    +
    + +

    Example pt. 2, in C:

    + +
    long mult2(long a, long b) {
    +  long s = a * b;
    +  return s;
    +}
    +
    + +

    Example pt. 2, in Assembly:

    + +
    0000000000400550 <mult2>:
    +  400550: mov %rdi,%rax #a
    +  400553: imul %rsi,%rax #a*b
    +  400557: retq #return
    +
    + +

    Example – Steps 1 and 2

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x120
    %rax 
    %rip0x400540
    %rdi 
    %rsi 
    %rdx 
    + +

    Example – Steps 3 and 4

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
     %rbx 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x118
    %rax 
    %rip400544
    %rdi 
    %rsi 
    %rdx 
    + +

    Example – Steps 5 and 6

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
     %rbx 
     0x400549 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x110
    %rax 
    %rip0x400553
    %rdi 
    %rsi 
    %rdx 
    + +

    Example – Steps 7, 8 and 9

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
     %rbx 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x118
    %rax 
    %rip0x400549
    %rdi 
    %rsi 
    %rdx 
    + +

    Summary

    + +
      +
    • Function call mechanisms: passing control and data, managing memory
    • +
    • Memory layout +
        +
      • Stack (local variables …)
      • +
      • Heap (dynamically allocated data)
      • +
      • Data (statically allocated data)
      • +
      • Text / Shared Libraries (program code)
      • +
      +
    • +
    • “Stack” is the data structure used for function call / return +
        +
      • If multstore calls mult2, then mult2 returns before multstore
      • +
      +
    • +
    • x86-64 stack register and instructions: stack pointer rsp, push and pop
    • +
    • x86-64 function call instructions: call and ret
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • (highlighted) Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/14/14/index.html b/_site/melody/cmpt-295/14/14/index.html new file mode 100644 index 0000000..d86d58b --- /dev/null +++ b/_site/melody/cmpt-295/14/14/index.html @@ -0,0 +1,1031 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Machine-Level Programming
    • +
    • Lecture 14 +
        +
      • Assembly language
      • +
      • Program Control
      • +
      • Function Call and Stack
      • +
      • Passing Control
      • +
      +
    • +
    + +

    Demo: alternative way of implementing if/else in assembly language

    + +
      +
    • Lecture 12 – ifelse.c and ifelse.s
    • +
    + +

    Last Lecture

    + +
      +
    • In x86-64 assembly, there are no iterative statements
    • +
    • To alter the execution flow, compiler generates code sequence +that implements these iterative statements (while, do-while +and for loops) using branching method: +
        +
      • cmp* instruction
      • +
      • jX instructions (jump)
      • +
      +
    • +
    • 2 loop patterns: +
        +
      • “coding the false condition first” -> while loops (hence for loops)
      • +
      • “jump-in-middle” -> while, do-while (hence for loops)
      • +
      +
    • +
    + +

    While loop – Question from last lecture “coding the false condition first”

    + +

    in C:

    + +
    while(x<y){
    +  //stmts
    +}
    +
    + +

    in assembly: # x in %edi, y in %esi

    + +
    loop:
    +  cmpl %edi, %esi
    +  jl endloop
    +  # stmts
    +  jmp loop
    +endloop:
    +  ret
    +
    + +

    Loop Pattern 1

    + +
    loop:
    +  if cond false
    +    goto done:
    +  stmts
    +  goto loop:
    +done:
    +
    + +

    Would this assembly code be the equivalent of our C code?

    + +

    For loop - Homework

    + +

    In C:

    + +
    format: for(initialization;condition testing;increment){
    +for(i=0;i<;i++){
    +  // stmts
    +}
    +return;
    +
    + +

    Becomes:

    + +
    i=0; //initialization
    +while(i<n){//condition
    +  //stmts
    +  i++; //increment
    +}
    +return;
    +
    + +

    In Assembly:

    + +
      xorl %ecx, %ecx #initialization: %ecx (i) <- 0
    +loop:
    +  cmpl %edi, %ecx #i-n?0 testing
    +  jge endloop #i-n>=0 false condition
    +  #stmts
    +  incl %ecx #i++ increment
    +  jmp loop #loop again
    +endloop:
    +  ret
    +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • (highlighted) Overview of Function Call
      • +
      • (highlighted) Memory Layout and Stack - x86-64 instructions and registers
      • +
      • (highlighted) Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    What happens when a function (caller) calls another function (callee)?

    + +
      +
    1. Control is passed (PC is set) … +
        +
      • To the beginning of the code in callee function
      • +
      • Back to where callee function was called in caller function
      • +
      +
    2. +
    3. Data is passed … +
        +
      • To callee function via function parameter(s)
      • +
      • Back to caller function via return value
      • +
      +
    4. +
    5. Memory is … +
        +
      • Allocated during callee function execution
      • +
      • Deallocated upon return to caller function
      • +
      +
    6. +
    + +

    Code example 1:

    + +
    void who(...) {
    +  int sum = 0;
    +  ...
    +  y = amI(x);
    +  sum = x + y;
    +  return;
    +}
    +
    + +

    Code example 2:

    + +
    int amI(int i)
    +{
    +  int t = 3*i;
    +  int v[10];
    +  ...
    +  return v[t];
    +}
    +
    + +

    Above mechanisms implemented with machine code instructions and described as a set of conventions (ISA)

    + +

    Remember from Lecture 2: Closer look at memory

    + +
      +
    • Seen as a linear array of bytes
    • +
    • 1 byte (8 bits) smallest addressable +unit of memory +
        +
      • Byte-addressable
      • +
      +
    • +
    • Each byte has a unique address
    • +
    • Computer reads a “word size” worth +of bits at a time
    • +
    • Compressed view of memory
    • +
    + +

    Compressed view of memory w/ addresses in cells:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-8
    ...........................
    0x0018
    0x0010
    0x0008
    0x00000x00010x00020x00030x00040x00050x00060x00070x0008
    + +

    Memory Layout

    + +

    Segments:

    + +
      +
    • Stack +
        +
      • Runtime stack, e. g., local variables
      • +
      +
    • +
    • Heap +
        +
      • Dynamically allocated as needed, explicitly released (freed)
      • +
      • When call malloc(), free(), new(), delete, …
      • +
      +
    • +
    • Data +
        +
      • Statically allocated data, e.g., global vars, static vars, string constants
      • +
      +
    • +
    • Text +
        +
      • Executable machine instructions
      • +
      • Read-only
      • +
      +
    • +
    • Shared Libraries +
        +
      • Executable machine instructions
      • +
      • Read-only
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    0x00007FFFFFFFFFFFStack (down arrow)
     Shared Libraries
     Heap (up arrow)
     Data
    0x0000000000400000Text
    0x0000000000000000 
    + +

    Memory Allocation Example

    + +

    Where does everything go?

    + +
    #include ...
    +char hugeArray[1 << 31]; /* 231 = 2GB */
    +int global = 0;
    +int useless(){ return 0; }
    +int main ()
    +{
    +  void *ptr1, *ptr2;
    +  int local = 0;
    +  ptr1 = malloc(1 << 28); /* 228 = 256 MB*/
    +  ptr2 = malloc(1 << 8); /* 28 = 256 B*/
    +
    +  /* Some print statements ... */
    +}
    +
    + + + + + + + + + + + + + + +
    M[]
    Stack (down arrow)
    ...
    Shared Libraries
    ...
    Heap (up arrow)
    Data
    Text
    + +

    Closer look at function call pattern

    + +

    A function may call a function, which may call a function, which may call a function, …

    + +

    Code example 1:

    + +
    who(...) {
    +  ...
    +  ...
    +  are();
    +  ...
    +  ...
    +}
    +
    + +

    Code example 2:

    + +
    are(...) {
    +  ...
    +  you();
    +  ...
    +  you();
    +  ...
    +}
    +
    + +

    Example 3:

    + +
    you(...) {
    +  ...
    +  ...
    +  ...
    +  ...
    +  ...
    +}
    +
    + +
      +
    • When a function (callee) terminates and returns, its most +recent caller resumes which eventually terminates and returns +and its most recent caller resumes …
    • +
    • Does this pattern remind you of anything?
    • +
    + +

    Stack

    + +

    Definition: A stack is a last-in-first-out (LIFO) data structure with two characteristic operations:

    + +
      +
    • push(data)
    • +
    • data = pop() or pop(&data)
    • +
    + +

    Do not have access to anything except what is on (at) top.

    + +

    Image of stack of dinner plates.

    + +

    Closer look at stack

    + +
      +
    • x86-64 assembly language has stack-specific
    • +
    • %rsp +
        +
      • Points to address of last used byte on stack
      • +
      • Initialized to “top of stack” at startup
      • +
      • Stack grows towards low memory address
      • +
      +
    • +
    • pushq src
    • +
    • popq dest
    • +
    + +

    Diagram of stack: +at the top of the stack is %rsp; the stack grows down. +Memory addresses decreese as they go down the stack.

    + +

    x86-64 stack instruction: push

    + +
      +
    • pushq src +
        +
      • Fetch value of operand src
      • +
      • Decrement %rsp by 8
      • +
      • Write value at address given by %rsp
      • +
      +
    • +
    + +

    Diagram of stack after 3 pushes (transcriber’s note: these diagrams are probably wrong, but I need to describe what is there):

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
    %rsp Top
    + +

    x86-64 stack instruction: pop

    + +

    … we pop once.

    + +
      +
    • popq dest +
        +
      • Read value at %rsp (address) and store it in operand dest (must be register)
      • +
      • Increment %rsp by 8
      • +
      +
    • +
    + +

    After we pop once:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] stackName
     data 
     data 
    %rspdataTop
     
    + +

    Passing control mechanism x86-64 instruction: call and ret

    + +
      +
    • call func +
        +
      • push PC
      • +
      • jmp func (set PC to func)
      • +
      +
    • +
    + +

    Effect: return address, i.e., the +address of the instruction after +call func (held in PC) is +pushed onto the stack

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
    %rsp Top
    + +

    Passing control mechanism x86-64 instruction: call and ret

    + +
      +
    • ret +
        +
      • popq PC
      • +
      • jmp PC
      • +
      +
    • +
    + +

    Effect: return address, i.e., the +address of instruction after +call func, is pop’ed from +the stack and stored in PC

    + +

    After returning from the call …

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     data 
    %rsp Top
    + +

    Example

    + +

    Example pt. 1, in C:

    + +
    void multstore(long x, long y, long *dest) {
    +  long t = mult2(x, y);
    +  *dest = t;
    +  return;
    +}
    +
    + +

    Example pt. 1, in Assembly:

    + +
    0000000000400540 <multistore>:
    +  400540: push %rbx #Save %rbx
    +  400541: mov %rdx,%rbx #save Dest
    +  400544: callq 400550 <mult2> #mult2(x,y)
    +  400549: mov %rax,(%rbx) #save as dest
    +  40054c: pop %rbx #restore %rbx
    +  40054d: retq #return
    +
    + +

    Example pt. 2, in C:

    + +
    long mult2(long a, long b) {
    +  long s = a * b;
    +  return s;
    +}
    +
    + +

    Example pt. 2, in Assembly:

    + +
    0000000000400550 <mult2>:
    +  400550: mov %rdi,%rax #a
    +  400553: imul %rsi,%rax #a*b
    +  400557: retq #return
    +
    + +

    Example – Steps 1 and 2

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x120
    %rax 
    %rip0x400540
    %rdi 
    %rsi 
    %rdx 
    + +

    Example – Steps 3 and 4

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
     %rbx 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x118
    %rax 
    %rip400544
    %rdi 
    %rsi 
    %rdx 
    + +

    Example – Steps 5 and 6

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
     %rbx 
     0x400549 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x110
    %rax 
    %rip0x400553
    %rdi 
    %rsi 
    %rdx 
    + +

    Example – Steps 7, 8 and 9

    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterM[] StackName
     ret address 
     %rbx 
    %rsp Top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    %rsp0x118
    %rax 
    %rip0x400549
    %rdi 
    %rsi 
    %rdx 
    + +

    Summary

    + +
      +
    • Function call mechanisms: passing control and data, managing memory
    • +
    • Memory layout +
        +
      • Stack (local variables …)
      • +
      • Heap (dynamically allocated data)
      • +
      • Data (statically allocated data)
      • +
      • Text / Shared Libraries (program code)
      • +
      +
    • +
    • “Stack” is the data structure used for function call / return +
        +
      • If multstore calls mult2, then mult2 returns before multstore
      • +
      +
    • +
    • x86-64 stack register and instructions: stack pointer rsp, push and pop
    • +
    • x86-64 function call instructions: call and ret
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • (highlighted) Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/14/Lecture_14_Machine_Level_Prog_Fcn_Call_Stack.pdf b/_site/melody/cmpt-295/14/Lecture_14_Machine_Level_Prog_Fcn_Call_Stack.pdf new file mode 100644 index 0000000..b822a3f Binary files /dev/null and b/_site/melody/cmpt-295/14/Lecture_14_Machine_Level_Prog_Fcn_Call_Stack.pdf differ diff --git a/_site/melody/cmpt-295/14/Lecture_14_Machine_Level_Prog_Fcn_Call_Stack_Annotated.pdf b/_site/melody/cmpt-295/14/Lecture_14_Machine_Level_Prog_Fcn_Call_Stack_Annotated.pdf new file mode 100644 index 0000000..50d60c9 Binary files /dev/null and b/_site/melody/cmpt-295/14/Lecture_14_Machine_Level_Prog_Fcn_Call_Stack_Annotated.pdf differ diff --git a/_site/melody/cmpt-295/15/15.html b/_site/melody/cmpt-295/15/15.html new file mode 100644 index 0000000..9738313 --- /dev/null +++ b/_site/melody/cmpt-295/15/15.html @@ -0,0 +1,223 @@ + + + + + | tait.tech + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Machine-Level Programming
    • +
    • Lecture 15 +
        +
      • Assembly language
      • +
      • Program Control
      • +
      • Function Call and Stack
      • +
      • Passing Control – cont’d
      • +
      +
    • +
    + +

    Happy Lunar New Year!

    + +
      +
    • 新年快乐 / 新年快樂 – Xīnnián kuàile
    • +
    • Cung Chúc Tân Xuân
    • +
    • Chúc Mừng Năm Mới
    • +
    • 새해 복 많이 받으세요 – saehae bog manh-i bad-euseyo
    • +
    • 过年好 / 過年好 – Guò nián hǎo
    • +
    + +

    Homework

    + +

    Memory Allocation Example

    + +

    Where does everything go?

    + +

    Code:

    + +
    #include ... //shared libraries
    +char hugeArray[1 << 31]; /* 231 = 2GB */// data
    +int global = 0; // data
    +int useless(){ return 0; } // text
    +int main ()
    +{
    +  void *ptr1, *ptr2; //stack
    +  int local = 0; //stack
    +  ptr1 = malloc(1 << 28); /* 228 = 256 MB*/ //heap
    +  ptr2 = malloc(1 << 8); /* 28 = 256 B*///heap
    +
    +  /* Some print statements ... */
    +}
    +
    + +

    Stack in list form:

    + +
      +
    • Stack (down arrow)
    • +
    • +
    • Shared libraries
    • +
    • +
    • heap (up arrow)
    • +
    • data
    • +
    • Text
    • +
    • +
    + +

    Why 8?

    + +
      +
    • pushq src +
        +
      • Fetch value of operand src
      • +
      • Decrement %rsp by 8
      • +
      • Write value at address given by %rsp
      • +
      +
    • +
    • popq dest +
        +
      • Read value at %rsp (address) and +store it in operand dest (must be register)
      • +
      • Increment %rsp by 8
      • +
      +
    • +
    + +

    1) %rsp contains the memory address 0x0018

    + + + + + + + + + + + + + + + + + + + + + + +
    RegisterMemory Address
    %rsp0x0018
     0x0010
     0x0008
    + +

    2) %rsp contains the memory address ____

    + + + + + + + + + + + + + + + + + + + + + + +
    RegisterMemory Address
     0x0018
    %rsp0x0010
     0x0008
    + +

    Last Lecture

    + +
      +
    • Function call mechanisms: 1) passing control, 2) passing data, 3) +managing local data on memory (stack) +
        +
      • Memory layout
      • +
      • Stack (local variables …)
      • +
      • Heap (dynamically allocated data)
      • +
      • Data (statically allocated data)
      • +
      • Text / Shared Libraries (program code)
      • +
      +
    • +
    • A “stack” is the right data structure for function call / return +
        +
      • If multstore calls mult2, then mult2 returns before multstore returns
      • +
      +
    • +
    • x86-64 stack register and instructions: stack pointer %rsp, push and pop
    • +
    + +

    (unless I’m missing something, this contains exactly the same slides as Lecture 14 here. Everything in the “example 1 steps X and Y” portion, read that for this section)

    + +

    Summary

    + +
      +
    • Function call mechanisms: 1) passing control, 2) passing data, 3) managing +local data on memory (stack)
    • +
    • Memory layout +
        +
      • Stack (local variables …)
      • +
      • Heap (dynamically allocated data)
      • +
      • Data (statically allocated data)
      • +
      • Text / Shared Libraries (program code)
      • +
      +
    • +
    • A “stack” is the right data structure for function call / return +
        +
      • If multstore calls mult2, then mult2 returns before multstore returns
      • +
      +
    • +
    • x86-64 stack register and instructions: stack pointer %rsp, push and pop
    • +
    • x86-64 function call instructions: call and ret
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack – Recursion +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • (highlighted) Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/15/15/index.html b/_site/melody/cmpt-295/15/15/index.html new file mode 100644 index 0000000..04fd773 --- /dev/null +++ b/_site/melody/cmpt-295/15/15/index.html @@ -0,0 +1,225 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Unit - Machine-Level Programming
    • +
    • Lecture 15 +
        +
      • Assembly language
      • +
      • Program Control
      • +
      • Function Call and Stack
      • +
      • Passing Control – cont’d
      • +
      +
    • +
    + +

    Happy Lunar New Year!

    + +
      +
    • 新年快乐 / 新年快樂 – Xīnnián kuàile
    • +
    • Cung Chúc Tân Xuân
    • +
    • Chúc Mừng Năm Mới
    • +
    • 새해 복 많이 받으세요 – saehae bog manh-i bad-euseyo
    • +
    • 过年好 / 過年好 – Guò nián hǎo
    • +
    + +

    Homework

    + +

    Memory Allocation Example

    + +

    Where does everything go?

    + +

    Code:

    + +
    #include ... //shared libraries
    +char hugeArray[1 << 31]; /* 231 = 2GB */// data
    +int global = 0; // data
    +int useless(){ return 0; } // text
    +int main ()
    +{
    +  void *ptr1, *ptr2; //stack
    +  int local = 0; //stack
    +  ptr1 = malloc(1 << 28); /* 228 = 256 MB*/ //heap
    +  ptr2 = malloc(1 << 8); /* 28 = 256 B*///heap
    +
    +  /* Some print statements ... */
    +}
    +
    + +

    Stack in list form:

    + +
      +
    • Stack (down arrow)
    • +
    • +
    • Shared libraries
    • +
    • +
    • heap (up arrow)
    • +
    • data
    • +
    • Text
    • +
    • +
    + +

    Why 8?

    + +
      +
    • pushq src +
        +
      • Fetch value of operand src
      • +
      • Decrement %rsp by 8
      • +
      • Write value at address given by %rsp
      • +
      +
    • +
    • popq dest +
        +
      • Read value at %rsp (address) and +store it in operand dest (must be register)
      • +
      • Increment %rsp by 8
      • +
      +
    • +
    + +

    1) %rsp contains the memory address 0x0018

    + + + + + + + + + + + + + + + + + + + + + + +
    RegisterMemory Address
    %rsp0x0018
     0x0010
     0x0008
    + +

    2) %rsp contains the memory address ____

    + + + + + + + + + + + + + + + + + + + + + + +
    RegisterMemory Address
     0x0018
    %rsp0x0010
     0x0008
    + +

    Last Lecture

    + +
      +
    • Function call mechanisms: 1) passing control, 2) passing data, 3) +managing local data on memory (stack) +
        +
      • Memory layout
      • +
      • Stack (local variables …)
      • +
      • Heap (dynamically allocated data)
      • +
      • Data (statically allocated data)
      • +
      • Text / Shared Libraries (program code)
      • +
      +
    • +
    • A “stack” is the right data structure for function call / return +
        +
      • If multstore calls mult2, then mult2 returns before multstore returns
      • +
      +
    • +
    • x86-64 stack register and instructions: stack pointer %rsp, push and pop
    • +
    + +

    (unless I’m missing something, this contains exactly the same slides as Lecture 14 here. Everything in the “example 1 steps X and Y” portion, read that for this section)

    + +

    Summary

    + +
      +
    • Function call mechanisms: 1) passing control, 2) passing data, 3) managing +local data on memory (stack)
    • +
    • Memory layout +
        +
      • Stack (local variables …)
      • +
      • Heap (dynamically allocated data)
      • +
      • Data (statically allocated data)
      • +
      • Text / Shared Libraries (program code)
      • +
      +
    • +
    • A “stack” is the right data structure for function call / return +
        +
      • If multstore calls mult2, then mult2 returns before multstore returns
      • +
      +
    • +
    • x86-64 stack register and instructions: stack pointer %rsp, push and pop
    • +
    • x86-64 function call instructions: call and ret
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack – Recursion +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • (highlighted) Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/15/Lecture_15_Machine_Level_Prog_Fcn_Call_Passing_Control.pdf b/_site/melody/cmpt-295/15/Lecture_15_Machine_Level_Prog_Fcn_Call_Passing_Control.pdf new file mode 100644 index 0000000..ebed8d2 Binary files /dev/null and b/_site/melody/cmpt-295/15/Lecture_15_Machine_Level_Prog_Fcn_Call_Passing_Control.pdf differ diff --git a/_site/melody/cmpt-295/16/16.html b/_site/melody/cmpt-295/16/16.html new file mode 100644 index 0000000..5976b20 --- /dev/null +++ b/_site/melody/cmpt-295/16/16.html @@ -0,0 +1,1721 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Lecture 16 – Midterm 1 Review Session
    • +
    + +

    Go over Rounding - Lecture 6 Slide 13:

    + +

    Rounding

    + +
      +
    1. Round up
    2. +
    3. Round down
    4. +
    5. Round half way -> when bits to right of rounding position are 10002\text{100\dots 0}_{2} +
        +
      • Round to even number: produces 0 as the least significant bit of rounded result.
      • +
      +
    6. +
    + +

    Example: Round to nearest 1/41/4 (2 bits right of binary point):

    + +

    The third bit after the binary point is the 24th bit. Imagine the second bit as bit 23 of frac of IEEE.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueBinaryRoundedActionRounded Value
    23322 \frac{3}{32}10.000112\text{10.00011}_{2}10.002\text{10.00}_{2}(<1/2–down)22
    23162 \frac{3}{16}10.00.1102\text{10.00.110}_{2}10.012\text{10.01}_{2}(>1/2–up)2142 \frac{1}{4}
    2782 \frac{7}{8}10.111002\text{10.11100}_{2}11.002\text{11.00}_{2}(1/2–up to even))33
    2582 \frac{5}{8}10.101002\text{10.10100}_{2}10.102\text{10.10}_{2}(1/2–down to even)2122 \frac{1}{2}
    + +

    Assignment 3 Question 1 a. iii

    + +

    Transcriber’s note: the rounding bit will be highlighted by spelling out the number instaed of using the 0/1 characters

    + +

    frac: 1100 1100 1100 1100 1100 11zero0 11001100 \space 1100 \space 1100 \space 1100 \space 1100 \space 11\text{zero}0 \space \overline{1100}

    + +

    Assignment 3 Question 1 a. iv

    + +

    frac: 0101 0101 0101 0101 0101 01zero 1010101 \space 0101 \space 0101 \space 0101 \space 0101 \space 01\text{zero} \space \overline{101}

    + +

    Assignment#2 Question 2 g., h., i., k.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    + + Exponent + + Fraction + + Value +
    + Description + + Bit Representation/th> + + exp + + E + + 2E2^{E} + + frac + + M + + M2EM 2^{E} + + V + + Decimal +
    + zero + + 0 000 00 + + 0 + + -2 + + 1/41/4 + + 0/40/4 + + 0/40/4 + + 0/160/16 + + 0 + + 0.0 +
    + Smallest positive denormalized + + 0 000 01 + + 0 + + -2 + + 1/41/4 + + 1/41/4 + + 1/41/4 + + 1/161/16 + + 1/161/16 + + 0.0625 +
    + + 0 000 10 + + 0 + + -2 + + 1/41/4 + + 2/4=1/22/4=1/2 + + 2/4=1/22/4=1/2 + + 2/162/16 + + 2/162/16 + + 0.125 +
    + Largest positive denormalized + + 0 000 11 + + 0 + + -2 + + 1/41/4 + + 3/43/4 + + 3/43/4 + + 3/163/16 + + 3/163/16 + + 0.1875 +
    + Smallest positive normalized + + 0 001 00 + + 1 + + -2 + + 1/41/4 + + 0/40/4 + + 4/4=14/4=1 + + 4/164/16 + + 4/164/16 + + 0.25 +
    + + 0 001 01 + + 1 + + -2 + + 1/41/4 + + 1/41/4 + + 5/45/4 + + 5/165/16 + + 5/165/16 + + 0.3125 +
    + + 0 001 10 + + 1 + + -2 + + 1/41/4 + + 2/4=1/22/4=1/2 + + 6/46/4 + + 6/166/16 + + 6/166/16 + + 0.375 +
    + + 0 001 11 + + 1 + + -2 + + 1/41/4 + + 3/43/4 + + 7/47/4 + + 7/167/16 + + 7/167/16 + + 0.4375 +
    + + 0 010 00 + + 2 + + -1 + + 1/21/2 + + 0/40/4 + + 4/4=14/4=1 + + 4/84/8 + + 4/84/8 + + 0.5 +
    + + 0 010 01 + + 2 + + -1 + + 1/21/2 + + 1/41/4 + + 5/45/4 + + 5/85/8 + + 5/85/8 + + 0.625 +
    + + 0 010 11 + + 2 + + -1 + + 1/21/2 + + 3/43/4 + + 7/47/4 + + 7/87/8 + + 7/87/8 + + 0.875 +
    + One + + 0 011 00 + + 3 + + 0 + + 1 + + 0/40/4 + + 4/4=14/4=1 + + 4/44/4 + + 4/44/4 + + 1.0 +
    + + 0 011 01 + + 3 + + 0 + + 1 + + 1/41/4 + + 5/45/4 + + 5/45/4 + + 5/45/4 + + 1.25 +
    + + 0 011 10 + + 3 + + 0 + + 1 + + 2/4=1/22/4=1/2 + + 6/46/4 + + 6/46/4 + + 6/46/4 + + 1.5 +
    + + 0 011 11 + + 3 + + 0 + + 1 + + 3/43/4 + + 7/47/4 + + 7/47/4 + + 7/47/4 + + 1.75 +
    + + 0 100 00 + + 4 + + 1 + + 2 + + 0/40/4 + + 4/4=14/4=1 + + 8/48/4 + + 8/48/4 + + 2 +
    + + 0 100 01 + + 4 + + 1 + + 2 + + 1/41/4 + + 5/45/4 + + 10/410/4 + + 10/410/4 + + 2.5 +
    + + 0 100 10 + + 4 + + 1 + + 2 + + 2/4=1/22/4=1/2 + + 6/46/4 + + 12/412/4 + + 12/412/4 + + 3 +
    + + 0 100 11 + + 4 + + 1 + + 2 + + 3/43/4 + + 7/47/4 + + 14/414/4 + + 14/414/4 + + 3.5 +
    + + 0 101 00 + + 5 + + 2 + + 4 + + 0/40/4 + + 4/4=14/4=1 + + 16/416/4 + + 16/416/4 + + 4 +
    + + 0 101 01 + + 5 + + 2 + + 4 + + 1/41/4 + + 5/45/4 + + 20/420/4 + + 20/420/4 + + 5 +
    + + 0 101 10 + + 5 + + 2 + + 4 + + 2/4=1/22/4=1/2 + + 6/46/4 + + 24/424/4 + + 24/424/4 + + 6 +
    + + 0 101 11 + + 5 + + 2 + + 4 + + 3/43/4 + + 7/47/4 + + 28/428/4 + + 28/428/4 + + 7 +
    + + 0 110 00 + + 6 + + 3 + + 8 + + 0/40/4 + + 4/4=14/4=1 + + 32/432/4 + + 32/432/4 + + 8 +
    + + 0 110 01 + + 6 + + 3 + + 8 + + 1/41/4 + + 5/45/4 + + 40/440/4 + + 40/440/4 + + 10 +
    + + 0 110 10 + + 6 + + 3 + + 8 + + 2/4=1/22/4=1/2 + + 6/46/4 + + 48/448/4 + + 48/448/4 + + 12 +
    + Largest positive normalized + + 0 110 11 + + 6 + + 3 + + 8 + + 3/43/4 + + 7/47/4 + + 56/456/4 + + 56/456/4 + + 14 +
    + + infinity + + 0 111 0 + + - + + - + + - + + - + + - + + - + + \infty + + - +
    + NaN + + + - + + - + + - + + - + + - + + - + + NaN + + - +
    + + +

    g. What is the “range” (not contiguous) of fractional decimal numbers that can be represented using this 6-bit floating-point representation?

    + +

    “range” of real numbers [14.014.0][-14.0\dots 14.0] not considering ±\pm\infty and NaN (since it’s not a contiguous range)

    + +

    h. What is the range of the normalized exponent E (E found in the equation v=(-1)sM2Ev=\text{(-1)}^{s} M 2^{E}) which can be represented by this 6-bit floating representation?

    + +

    Range of E[25]E \rightarrow [-2\dots 5] is the same as exp[001110]\text{exp} \rightarrow [001\dots 110]

    + +

    Denormalized exponent E:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    expEtype
    000-2denormalized
    001-2normalized
    010-1normalized
    0110normalized
    1001normlaized
    1012normalized
    1103normalized
    111±\pm\infty, NaNdenormlaized
    + +

    i. Give an example of a fractional decimal number that can be represented using this 6-bit floating-point representaetion, but is within the “range” of representable values.

    + +

    11.0 cannot be represented but it is within the range.

    + +

    From lecture 6 Slide 15

    + +

    What does Epsilon mean? A small positive quantity.

    +
      +
    1. the 5th letter of the Greek alphabet — see Alphabet Table. 2: an arbitrarily small positive quantity in mathematical analysis.
    2. +
    + +
      +
    • exp and frac: interpreted as unsigned values.
    • +
    • if frac = 000…0 -> M = 1.0
    • +
    • if frac = 111…1 -> M=2.0ϵM = 2.0 - \epsilon (where ϵ\epsilon means a very small value)
    • +
    + +

    k. How close is the value of the frac of the largest normalized number to 1? In other words, how close is M to 2, i.e.What is E (epsilon) in this equation: 1M<2ϵ1\leq M \lt 2 - \epsilon? Express E as a fractional decimal number.

    + +

    First, let’s fix the above equation 1M<2ϵ1 \leq M < 2 - \epsilon. It should be 1M<21 \leq M < 2.

    + +

    Remember:

    + +
      +
    • + 1.0M<2.01.0 \leq M < 2.0 +
    • +
    • + 1.0M2.0ϵ1.0 \leq M \leq 2.0 - \epsilon +
    • +
    • + 1.0(1+frac)2.0ϵ1.0 \leq (1 + \text{frac}) \leq 2.0 - \epsilon +
    • +
    • + 0.0frac1.0ϵ0.0 \leq \text{frac} \leq 1.0 - \epsilon +
    • +
    + +

    answer:

    + +

    The value of “frac” of the largest denormalized number is .11=3/4=0.7510\text{.11} = 3/4 = \text{0.75}_{10}

    + +

    How close is the value of the “frac” of the largest normalized number to 1=1/4=0.25101 = 1/4 = \text{0.25}_{10}

    + +

    Assignment#3 Question 1

    + +

    1) [10 points] Memory addressing modes – Marked by Aditi

    + +

    Assume the following values are stored at the indicated memory addresses and registers.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Memory AddressValue
    0x2300x23
    0x2340x00
    0x2350x01
    0x23A0xED
    0x2400xFF
    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rdi0x230
    %rsi0x234
    %rcx0x4
    %rax0x1
    + +

    Imagine the operands in the table below are the Src (source) operands for some unspecififed assembly instructions (any instruction except lea*), fill in the following table with the appropriate answers.

    + +

    Note: We do not need to know what the assembly instructions are in order to fill the table.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OperandOperand Value (expressed in hexidecimal)Operand Form (Choices are: immediate, register or one of the 9 memory addressing modes)
    %rsi0x234Register
    (%rdi)0x23Indurect memory address mode
    $0x23A0x23Aimmedaite value
    0x2400xffAbsolute memory addressing mode (this answer is preferable to “Imm” as it is more specific than “Imm” and highlights the fact that it does not require a “S” – see first row of tables below)
    10(%rdi)0xED“Base + displacement” memory addressing mode
    560(%rcx,%rax)0x01indexed memory addressing mode
    -550(,%rdi,2)0xEDScaled indexed memory addressing mode
    0x6(%rdi,%rax,4)0xEDScaled indexed addressing mode
    + +

    Still using the first table listed above displaying the value stored at various memory addresses and registers, fill in the following tables with three different Src (source) operands for some unspecififed assembly instructions (any instructions except lea*). For each row, this operand must result in the operand Value listed and must satified the Operand Form listed.

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OperandValueOperand Form (Choices are: immediate, registers or one of the 9 memory addressing modes)
    0x2340x00Absolute memory addressing mode
    (%rdi,%rax,4)0x00Scaled indexed memory address mode
    (%rdi,%rcx)0x00Indexed memory addressing mode
    + +

    Other answers are possible!

    + +

    Assignment#3 Question 2

    + +

    2) [2 marks] Machine level instructions and their memory locations – Marked by Aditi

    + +

    Consider a function called arith, defined in a file called arith.c and called from the main function found in the file called main.c.

    + +

    This function arith, performs some arithmatic manipulation on three parameters.

    + +

    Compiling main.c and arith.c files, we create an executable called ar, then we execute the command:

    + +
    objdump -d ar > arith.objdump
    +
    + +

    We display the partial content of arith.objdump below. The file arith.objdump is the disassembled version of the executable file ar.

    + +

    Your task is to fill in its missing parts, which have been underlined:

    + +
    0000000000400527 <arith>:
    +  400527:   48 8d 04 37       lea   (%rdi,%rsi,1)
    +  40052b:   48 01 d0          add   %rdx,%rax
    +  40052e:   48 8d 0c 76       lea   (%rsi,%rsi,2),%rcx
    +  400532:   48 c1 e1 04       shl   $0x4,%rcx
    +  400536:   48 8d 54 0f 04    lea   0x4(%rdi,%rcx,1),%rdx
    +  40053b:   48 0f af c2       imul  %rdx,%rax
    +  40053f:   c3                retq
    +
    + +

    Assignment#4 Question 2

    + +

    In the assembly code, there are a lot more steps than in the +C code, so how to match them and create the C code.

    + +

    Consier the following assembly code:

    + +
    # long func(long x,int n)
    +# x in %rdi, n in %esi, result in %rax
    +
    +func:
    +  movl %esi,%ecx
    +  movl $1,%edx
    +  movl $0,%eax
    +  jmp cond
    +loop:
    +  movq %rdi,%r8
    +  addq %rdx,%r8
    +  orq %r8,%rax
    +  salq %c1,%rdx # shift left %rdx by content of %c1*
    +cond:
    +  testq %rdx,%rdx # %rdx <- %rdx & %rdx
    +  jne loop # jump if not zero (when %rdx & %rdx != 0)
    +  ret # faill thru to ret (when %rdx & %rdx == 0)
    +
    + +

    Hand tracing code!

    + +

    From our Lectures 14 and 15

    + +

    Example pt.1 in C

    + +
    // multstore is caller
    +// x is %rdi
    +// y is %rsi
    +// dest is %rdx
    +void multstore(long x, long y, long *dest) {
    +  long t = mult2(x, y);
    +  *dest = t;
    +  return;
    +}
    +
    + +

    Example pt.2 in C:

    + +
    // mult2 callee
    +// a is %rdi
    +// b is %rsi
    +long mult2(long a, long b) {
    +  long s = a * b;
    +  return s;
    +}
    +
    + +

    Example pt.1 in assembly:

    + +
    0000000000400540 <multstore>:
    +  400540: push %rbx # Save %rbx (1)
    +  400541: mov %rdx,%rbx # Save dest (2)
    +  400544: callq 400550 <mult2> # mult2(x,y) (3)
    +  400549: mov %rax,(%rbx) # Save at Dest (7)
    +  40054c: pop %rbx # Restore %rbx (8)
    +  40054d: retq # Return (9)
    +
    + +

    Example pt.2 in assembly:

    + +
    0000000000400550 <mult2>:
    +  400550: mov %rdi,%rax # a (4)
    +  400553: imul %rsi,%rax # a*b (5)
    +  400557: retq # Return (6)
    +
    + + + +

    Example – Steps 1 and 2

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressM[] Stack ValueNote
     ret addressreturn address of caller of multstore
    (deleted) %rsp(new) %rbx(deleted) top
    (new) %rsp (new) top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbxdest (5)
    %rsp(deleted) 0x120 (new) 118
    %rax 
    %rip (PC)0x400 (deleted) 540 (deleted) 541 (1) (new) 544 (4)
    %rdix
    %rsiy
    %rdxdest (mem. address)
    + +

    Example – Steps 3 and 4

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressM[] Stack ValueNote
     ret address 
    (deleted) %rsp(deleted) %rbx (new) ret. address 0x400549 (3) 
    (2) %rsp top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbxdest
    %rsp(deleted) 0x118 (new) 110
    %raxa
    %rip (PC)0x400 (deleted)544 (deleted)549(1) (deleted)550(4) (new)553
    %rdix -> a
    %rsiy -> b
    %rdxdest
    + +

    Example – Steps 5 and 6

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressM[] Stack ValueNote
     ret address 
    (5) (new) %rsp%rbx 
    (deleted) %rsp(new) 0x400549top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbxdest
    %rsp(deleted) 0x110 (new) 118
    %raxa*b
    %rip (PC)0x400549
    %rdix -> a
    %rsiy -> b
    %rdxdest
    + +

    Example – Steps 7, 8 and 9

    + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressM[] Stack ValueNote
     ret address 
    %rsp%rbxTop
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbxdest
    %rsp0x118
    %raxa*b
    %rip0x400549
    %rdix (a)
    %rsiy (b)
    %rdxdest
    + +

    Next next lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack – Recursion +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • (highlighted) Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/16/16/index.html b/_site/melody/cmpt-295/16/16/index.html new file mode 100644 index 0000000..74fbbe0 --- /dev/null +++ b/_site/melody/cmpt-295/16/16/index.html @@ -0,0 +1,1723 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295

    + +
      +
    • Lecture 16 – Midterm 1 Review Session
    • +
    + +

    Go over Rounding - Lecture 6 Slide 13:

    + +

    Rounding

    + +
      +
    1. Round up
    2. +
    3. Round down
    4. +
    5. Round half way -> when bits to right of rounding position are 10002\text{100\dots 0}_{2} +
        +
      • Round to even number: produces 0 as the least significant bit of rounded result.
      • +
      +
    6. +
    + +

    Example: Round to nearest 1/41/4 (2 bits right of binary point):

    + +

    The third bit after the binary point is the 24th bit. Imagine the second bit as bit 23 of frac of IEEE.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueBinaryRoundedActionRounded Value
    23322 \frac{3}{32}10.000112\text{10.00011}_{2}10.002\text{10.00}_{2}(<1/2–down)22
    23162 \frac{3}{16}10.00.1102\text{10.00.110}_{2}10.012\text{10.01}_{2}(>1/2–up)2142 \frac{1}{4}
    2782 \frac{7}{8}10.111002\text{10.11100}_{2}11.002\text{11.00}_{2}(1/2–up to even))33
    2582 \frac{5}{8}10.101002\text{10.10100}_{2}10.102\text{10.10}_{2}(1/2–down to even)2122 \frac{1}{2}
    + +

    Assignment 3 Question 1 a. iii

    + +

    Transcriber’s note: the rounding bit will be highlighted by spelling out the number instaed of using the 0/1 characters

    + +

    frac: 1100 1100 1100 1100 1100 11zero0 11001100 \space 1100 \space 1100 \space 1100 \space 1100 \space 11\text{zero}0 \space \overline{1100}

    + +

    Assignment 3 Question 1 a. iv

    + +

    frac: 0101 0101 0101 0101 0101 01zero 1010101 \space 0101 \space 0101 \space 0101 \space 0101 \space 01\text{zero} \space \overline{101}

    + +

    Assignment#2 Question 2 g., h., i., k.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    + + Exponent + + Fraction + + Value +
    + Description + + Bit Representation/th> + + exp + + E + + 2E2^{E} + + frac + + M + + M2EM 2^{E} + + V + + Decimal +
    + zero + + 0 000 00 + + 0 + + -2 + + 1/41/4 + + 0/40/4 + + 0/40/4 + + 0/160/16 + + 0 + + 0.0 +
    + Smallest positive denormalized + + 0 000 01 + + 0 + + -2 + + 1/41/4 + + 1/41/4 + + 1/41/4 + + 1/161/16 + + 1/161/16 + + 0.0625 +
    + + 0 000 10 + + 0 + + -2 + + 1/41/4 + + 2/4=1/22/4=1/2 + + 2/4=1/22/4=1/2 + + 2/162/16 + + 2/162/16 + + 0.125 +
    + Largest positive denormalized + + 0 000 11 + + 0 + + -2 + + 1/41/4 + + 3/43/4 + + 3/43/4 + + 3/163/16 + + 3/163/16 + + 0.1875 +
    + Smallest positive normalized + + 0 001 00 + + 1 + + -2 + + 1/41/4 + + 0/40/4 + + 4/4=14/4=1 + + 4/164/16 + + 4/164/16 + + 0.25 +
    + + 0 001 01 + + 1 + + -2 + + 1/41/4 + + 1/41/4 + + 5/45/4 + + 5/165/16 + + 5/165/16 + + 0.3125 +
    + + 0 001 10 + + 1 + + -2 + + 1/41/4 + + 2/4=1/22/4=1/2 + + 6/46/4 + + 6/166/16 + + 6/166/16 + + 0.375 +
    + + 0 001 11 + + 1 + + -2 + + 1/41/4 + + 3/43/4 + + 7/47/4 + + 7/167/16 + + 7/167/16 + + 0.4375 +
    + + 0 010 00 + + 2 + + -1 + + 1/21/2 + + 0/40/4 + + 4/4=14/4=1 + + 4/84/8 + + 4/84/8 + + 0.5 +
    + + 0 010 01 + + 2 + + -1 + + 1/21/2 + + 1/41/4 + + 5/45/4 + + 5/85/8 + + 5/85/8 + + 0.625 +
    + + 0 010 11 + + 2 + + -1 + + 1/21/2 + + 3/43/4 + + 7/47/4 + + 7/87/8 + + 7/87/8 + + 0.875 +
    + One + + 0 011 00 + + 3 + + 0 + + 1 + + 0/40/4 + + 4/4=14/4=1 + + 4/44/4 + + 4/44/4 + + 1.0 +
    + + 0 011 01 + + 3 + + 0 + + 1 + + 1/41/4 + + 5/45/4 + + 5/45/4 + + 5/45/4 + + 1.25 +
    + + 0 011 10 + + 3 + + 0 + + 1 + + 2/4=1/22/4=1/2 + + 6/46/4 + + 6/46/4 + + 6/46/4 + + 1.5 +
    + + 0 011 11 + + 3 + + 0 + + 1 + + 3/43/4 + + 7/47/4 + + 7/47/4 + + 7/47/4 + + 1.75 +
    + + 0 100 00 + + 4 + + 1 + + 2 + + 0/40/4 + + 4/4=14/4=1 + + 8/48/4 + + 8/48/4 + + 2 +
    + + 0 100 01 + + 4 + + 1 + + 2 + + 1/41/4 + + 5/45/4 + + 10/410/4 + + 10/410/4 + + 2.5 +
    + + 0 100 10 + + 4 + + 1 + + 2 + + 2/4=1/22/4=1/2 + + 6/46/4 + + 12/412/4 + + 12/412/4 + + 3 +
    + + 0 100 11 + + 4 + + 1 + + 2 + + 3/43/4 + + 7/47/4 + + 14/414/4 + + 14/414/4 + + 3.5 +
    + + 0 101 00 + + 5 + + 2 + + 4 + + 0/40/4 + + 4/4=14/4=1 + + 16/416/4 + + 16/416/4 + + 4 +
    + + 0 101 01 + + 5 + + 2 + + 4 + + 1/41/4 + + 5/45/4 + + 20/420/4 + + 20/420/4 + + 5 +
    + + 0 101 10 + + 5 + + 2 + + 4 + + 2/4=1/22/4=1/2 + + 6/46/4 + + 24/424/4 + + 24/424/4 + + 6 +
    + + 0 101 11 + + 5 + + 2 + + 4 + + 3/43/4 + + 7/47/4 + + 28/428/4 + + 28/428/4 + + 7 +
    + + 0 110 00 + + 6 + + 3 + + 8 + + 0/40/4 + + 4/4=14/4=1 + + 32/432/4 + + 32/432/4 + + 8 +
    + + 0 110 01 + + 6 + + 3 + + 8 + + 1/41/4 + + 5/45/4 + + 40/440/4 + + 40/440/4 + + 10 +
    + + 0 110 10 + + 6 + + 3 + + 8 + + 2/4=1/22/4=1/2 + + 6/46/4 + + 48/448/4 + + 48/448/4 + + 12 +
    + Largest positive normalized + + 0 110 11 + + 6 + + 3 + + 8 + + 3/43/4 + + 7/47/4 + + 56/456/4 + + 56/456/4 + + 14 +
    + + infinity + + 0 111 0 + + - + + - + + - + + - + + - + + - + + \infty + + - +
    + NaN + + + - + + - + + - + + - + + - + + - + + NaN + + - +
    + + +

    g. What is the “range” (not contiguous) of fractional decimal numbers that can be represented using this 6-bit floating-point representation?

    + +

    “range” of real numbers [14.014.0][-14.0\dots 14.0] not considering ±\pm\infty and NaN (since it’s not a contiguous range)

    + +

    h. What is the range of the normalized exponent E (E found in the equation v=(-1)sM2Ev=\text{(-1)}^{s} M 2^{E}) which can be represented by this 6-bit floating representation?

    + +

    Range of E[25]E \rightarrow [-2\dots 5] is the same as exp[001110]\text{exp} \rightarrow [001\dots 110]

    + +

    Denormalized exponent E:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    expEtype
    000-2denormalized
    001-2normalized
    010-1normalized
    0110normalized
    1001normlaized
    1012normalized
    1103normalized
    111±\pm\infty, NaNdenormlaized
    + +

    i. Give an example of a fractional decimal number that can be represented using this 6-bit floating-point representaetion, but is within the “range” of representable values.

    + +

    11.0 cannot be represented but it is within the range.

    + +

    From lecture 6 Slide 15

    + +

    What does Epsilon mean? A small positive quantity.

    +
      +
    1. the 5th letter of the Greek alphabet — see Alphabet Table. 2: an arbitrarily small positive quantity in mathematical analysis.
    2. +
    + +
      +
    • exp and frac: interpreted as unsigned values.
    • +
    • if frac = 000…0 -> M = 1.0
    • +
    • if frac = 111…1 -> M=2.0ϵM = 2.0 - \epsilon (where ϵ\epsilon means a very small value)
    • +
    + +

    k. How close is the value of the frac of the largest normalized number to 1? In other words, how close is M to 2, i.e.What is E (epsilon) in this equation: 1M<2ϵ1\leq M \lt 2 - \epsilon? Express E as a fractional decimal number.

    + +

    First, let’s fix the above equation 1M<2ϵ1 \leq M < 2 - \epsilon. It should be 1M<21 \leq M < 2.

    + +

    Remember:

    + +
      +
    • + 1.0M<2.01.0 \leq M < 2.0 +
    • +
    • + 1.0M2.0ϵ1.0 \leq M \leq 2.0 - \epsilon +
    • +
    • + 1.0(1+frac)2.0ϵ1.0 \leq (1 + \text{frac}) \leq 2.0 - \epsilon +
    • +
    • + 0.0frac1.0ϵ0.0 \leq \text{frac} \leq 1.0 - \epsilon +
    • +
    + +

    answer:

    + +

    The value of “frac” of the largest denormalized number is .11=3/4=0.7510\text{.11} = 3/4 = \text{0.75}_{10}

    + +

    How close is the value of the “frac” of the largest normalized number to 1=1/4=0.25101 = 1/4 = \text{0.25}_{10}

    + +

    Assignment#3 Question 1

    + +

    1) [10 points] Memory addressing modes – Marked by Aditi

    + +

    Assume the following values are stored at the indicated memory addresses and registers.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Memory AddressValue
    0x2300x23
    0x2340x00
    0x2350x01
    0x23A0xED
    0x2400xFF
    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rdi0x230
    %rsi0x234
    %rcx0x4
    %rax0x1
    + +

    Imagine the operands in the table below are the Src (source) operands for some unspecififed assembly instructions (any instruction except lea*), fill in the following table with the appropriate answers.

    + +

    Note: We do not need to know what the assembly instructions are in order to fill the table.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OperandOperand Value (expressed in hexidecimal)Operand Form (Choices are: immediate, register or one of the 9 memory addressing modes)
    %rsi0x234Register
    (%rdi)0x23Indurect memory address mode
    $0x23A0x23Aimmedaite value
    0x2400xffAbsolute memory addressing mode (this answer is preferable to “Imm” as it is more specific than “Imm” and highlights the fact that it does not require a “S” – see first row of tables below)
    10(%rdi)0xED“Base + displacement” memory addressing mode
    560(%rcx,%rax)0x01indexed memory addressing mode
    -550(,%rdi,2)0xEDScaled indexed memory addressing mode
    0x6(%rdi,%rax,4)0xEDScaled indexed addressing mode
    + +

    Still using the first table listed above displaying the value stored at various memory addresses and registers, fill in the following tables with three different Src (source) operands for some unspecififed assembly instructions (any instructions except lea*). For each row, this operand must result in the operand Value listed and must satified the Operand Form listed.

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OperandValueOperand Form (Choices are: immediate, registers or one of the 9 memory addressing modes)
    0x2340x00Absolute memory addressing mode
    (%rdi,%rax,4)0x00Scaled indexed memory address mode
    (%rdi,%rcx)0x00Indexed memory addressing mode
    + +

    Other answers are possible!

    + +

    Assignment#3 Question 2

    + +

    2) [2 marks] Machine level instructions and their memory locations – Marked by Aditi

    + +

    Consider a function called arith, defined in a file called arith.c and called from the main function found in the file called main.c.

    + +

    This function arith, performs some arithmatic manipulation on three parameters.

    + +

    Compiling main.c and arith.c files, we create an executable called ar, then we execute the command:

    + +
    objdump -d ar > arith.objdump
    +
    + +

    We display the partial content of arith.objdump below. The file arith.objdump is the disassembled version of the executable file ar.

    + +

    Your task is to fill in its missing parts, which have been underlined:

    + +
    0000000000400527 <arith>:
    +  400527:   48 8d 04 37       lea   (%rdi,%rsi,1)
    +  40052b:   48 01 d0          add   %rdx,%rax
    +  40052e:   48 8d 0c 76       lea   (%rsi,%rsi,2),%rcx
    +  400532:   48 c1 e1 04       shl   $0x4,%rcx
    +  400536:   48 8d 54 0f 04    lea   0x4(%rdi,%rcx,1),%rdx
    +  40053b:   48 0f af c2       imul  %rdx,%rax
    +  40053f:   c3                retq
    +
    + +

    Assignment#4 Question 2

    + +

    In the assembly code, there are a lot more steps than in the +C code, so how to match them and create the C code.

    + +

    Consier the following assembly code:

    + +
    # long func(long x,int n)
    +# x in %rdi, n in %esi, result in %rax
    +
    +func:
    +  movl %esi,%ecx
    +  movl $1,%edx
    +  movl $0,%eax
    +  jmp cond
    +loop:
    +  movq %rdi,%r8
    +  addq %rdx,%r8
    +  orq %r8,%rax
    +  salq %c1,%rdx # shift left %rdx by content of %c1*
    +cond:
    +  testq %rdx,%rdx # %rdx <- %rdx & %rdx
    +  jne loop # jump if not zero (when %rdx & %rdx != 0)
    +  ret # faill thru to ret (when %rdx & %rdx == 0)
    +
    + +

    Hand tracing code!

    + +

    From our Lectures 14 and 15

    + +

    Example pt.1 in C

    + +
    // multstore is caller
    +// x is %rdi
    +// y is %rsi
    +// dest is %rdx
    +void multstore(long x, long y, long *dest) {
    +  long t = mult2(x, y);
    +  *dest = t;
    +  return;
    +}
    +
    + +

    Example pt.2 in C:

    + +
    // mult2 callee
    +// a is %rdi
    +// b is %rsi
    +long mult2(long a, long b) {
    +  long s = a * b;
    +  return s;
    +}
    +
    + +

    Example pt.1 in assembly:

    + +
    0000000000400540 <multstore>:
    +  400540: push %rbx # Save %rbx (1)
    +  400541: mov %rdx,%rbx # Save dest (2)
    +  400544: callq 400550 <mult2> # mult2(x,y) (3)
    +  400549: mov %rax,(%rbx) # Save at Dest (7)
    +  40054c: pop %rbx # Restore %rbx (8)
    +  40054d: retq # Return (9)
    +
    + +

    Example pt.2 in assembly:

    + +
    0000000000400550 <mult2>:
    +  400550: mov %rdi,%rax # a (4)
    +  400553: imul %rsi,%rax # a*b (5)
    +  400557: retq # Return (6)
    +
    + + + +

    Example – Steps 1 and 2

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressM[] Stack ValueNote
     ret addressreturn address of caller of multstore
    (deleted) %rsp(new) %rbx(deleted) top
    (new) %rsp (new) top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbxdest (5)
    %rsp(deleted) 0x120 (new) 118
    %rax 
    %rip (PC)0x400 (deleted) 540 (deleted) 541 (1) (new) 544 (4)
    %rdix
    %rsiy
    %rdxdest (mem. address)
    + +

    Example – Steps 3 and 4

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressM[] Stack ValueNote
     ret address 
    (deleted) %rsp(deleted) %rbx (new) ret. address 0x400549 (3) 
    (2) %rsp top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbxdest
    %rsp(deleted) 0x118 (new) 110
    %raxa
    %rip (PC)0x400 (deleted)544 (deleted)549(1) (deleted)550(4) (new)553
    %rdix -> a
    %rsiy -> b
    %rdxdest
    + +

    Example – Steps 5 and 6

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressM[] Stack ValueNote
     ret address 
    (5) (new) %rsp%rbx 
    (deleted) %rsp(new) 0x400549top
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbxdest
    %rsp(deleted) 0x110 (new) 118
    %raxa*b
    %rip (PC)0x400549
    %rdix -> a
    %rsiy -> b
    %rdxdest
    + +

    Example – Steps 7, 8 and 9

    + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressM[] Stack ValueNote
     ret address 
    %rsp%rbxTop
    + +

    Registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rbxdest
    %rsp0x118
    %raxa*b
    %rip0x400549
    %rdix (a)
    %rsiy (b)
    %rdxdest
    + +

    Next next lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack – Recursion +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • (highlighted) Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/16/Lecture_16_Midterm_1_Review_Session.pdf b/_site/melody/cmpt-295/16/Lecture_16_Midterm_1_Review_Session.pdf new file mode 100644 index 0000000..2fac853 Binary files /dev/null and b/_site/melody/cmpt-295/16/Lecture_16_Midterm_1_Review_Session.pdf differ diff --git a/_site/melody/cmpt-295/17/17.html b/_site/melody/cmpt-295/17/17.html new file mode 100644 index 0000000..a124a85 --- /dev/null +++ b/_site/melody/cmpt-295/17/17.html @@ -0,0 +1,22 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295 – Lecture 17 – Midterm 1

    + +

    (Note: that’s it, no slides)

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/17/17/index.html b/_site/melody/cmpt-295/17/17/index.html new file mode 100644 index 0000000..d128b70 --- /dev/null +++ b/_site/melody/cmpt-295/17/17/index.html @@ -0,0 +1,24 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 – Lecture 17 – Midterm 1

    + +

    (Note: that’s it, no slides)

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/17/Lecture_17_Midterm_1_1211.pdf b/_site/melody/cmpt-295/17/Lecture_17_Midterm_1_1211.pdf new file mode 100644 index 0000000..a72ab8b Binary files /dev/null and b/_site/melody/cmpt-295/17/Lecture_17_Midterm_1_1211.pdf differ diff --git a/_site/melody/cmpt-295/18/18.html b/_site/melody/cmpt-295/18/18.html new file mode 100644 index 0000000..31e7c2a --- /dev/null +++ b/_site/melody/cmpt-295/18/18.html @@ -0,0 +1,603 @@ + + + + + | tait.tech + + + + +
    +
    +

    CMPT 295: Unit - Machine-Level Programming

    + +

    Lecture 18:

    + +
      +
    • Assembly language
    • +
    • Program Control
    • +
    • Function Call and Stack
    • +
    • Passing Data
    • +
    + +

    Last Lecture

    + +
      +
    • Function call mechanisms: 1) passing control, 2) passing data, 3) +managing local data on memory (stack)
    • +
    • Memory layout +
        +
      • Stack (local variables …)
      • +
      • Heap (dynamically allocated data)
      • +
      • Data (statically allocated data)
      • +
      • Text / Shared Libraries (program code)
      • +
      +
    • +
    • A “stack” is the right data structure for function call / return +
        +
      • If multstore calls mult2, then mult2 returns before multstore returns
      • +
      +
    • +
    • x86-64 stack register and instructions: stack pointer %rsp, push and pop
    • +
    • x86-64 function call instructions: call and ret
    • +
    + +

    From Lecture 17 – Slide 14

    + +

    Why 8?

    + +
      +
    • pushq src +
        +
      • Fetch value of operand src
      • +
      • Decrement %rsp by 8
      • +
      • Write value at address given by %rsp
      • +
      +
    • +
    • popq dest +
        +
      • Read value at %rsp (address) and +store it in operand dest (must be register)
      • +
      • Increment %rsp by 8
      • +
      +
    • +
    + +

    1) %rsp contains the memory address 0x0018

    + + + + + + + + + + + + + + + + + + + + + + +
    RegisterMemory Address
    %rsp0x0018
     0x0010
     0x0008
    + +

    2) %rsp contains the memory address 0x0010

    + + + + + + + + + + + + + + + + + + + + + + +
    RegisterMemory Address
     0x0018
    %rsp0x0010
     0x0008
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • (highlighted) Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    2. Passing data mechanism – using stack x86-64 function call convention

    + +
      +
    1. Caller and callee functions must obey function call convention when +passing data during function call +
        +
      • Caller:
      • +
      +
        +
      • Before calling the callee function, the caller must copy the callee’s arguments (1 to 6) into specific registers: If there is a … +
          +
        • 1st argument -> %rdi (or %edi, or %di or %dil)
        • +
        • 2nd argument -> %rsi (or %esi, or %si or %sil)
        • +
        • 3rd argument -> %rdx (or %edx, or %dx or %dl)
        • +
        • 4th argument -> %rcx (or %ecx, or %cx or %cl)
        • +
        • 5th argument -> %r8 (or %r8d, or %r8w or %r8b)
        • +
        • 6th argument -> %r9 (or %r9d, or %r9w or %r9b) + * Callee:
        • +
        +
      • +
      • Before returning to caller, callee must copy returned value into register %rax
      • +
      +
    2. +
    + +

    Passing data mechanism – Example of passing arguments in registers and returning return value

    + +

    C code:

    + +
    long plus(long x, long y){
    +  return x+y;
    +}
    +
    +void sum_store(long x, long y, long *dest)
    +{
    +  long sum = plus(x,y);
    +  *dest = sum;
    +}
    +
    +int main(int argc, char *argv[]) {
    +  if (argc == 3) {
    +    long x = atoi(argv[1]);
    +    long y = atoi(argv[2]);
    +    long result;
    +    sum_store(x, y, &result);
    +    printf("%ld + %ld --> %ld\n", x, y, result);
    +  else printf("2 numbers required\n");
    +  return 0;
    +}
    +
    + +

    Assembly code:

    + +
    sum_store:
    +.LFB40:
    +  .cfi_startproc
    +  endbr64
    +  addq  %rsi,%rdi
    +  movq %rdi,(%rdx)
    +  ret
    +main:
    +  pushq %r13
    +  pushq %r12
    +  pushq %rbx
    +  subq $16,%rsp
    +  movq %fs:40,%rax
    +  movq %rax,8(%rsp)
    +  xorl %eax,%eax
    +  cmpl $3,%edi # highlighted
    +  je .L7 # highlighted
    +  leaq .LC1(%rip), %rdi
    +  call puts@PLT
    +.L3:
    +  movq 8(%rsp),%rax
    +  xorq %fs:40,%rax
    +  jne .L8
    +  addq $16,%rsp
    +  xorl %eax,%eax
    +  popq %rbx
    +  popq %r12
    +  popq %r13
    +  ret
    +.L7:
    +  movq 8(%rsi),%rdi
    +  movq %rsi,%rbx
    +  movl $10,%edx
    +  xorl %esi,%esi
    +  call strtol@PLT
    +  movq 16(%rbx),%rdi
    +  xorl %esi,%esi
    +  movl $10,%edx
    +  movslq %eax,%r12 # highlighted
    +  call strtol@PLT
    +  movq %rsp,%rdx # highlighted
    +  movq %r12,%rdi # highlighted
    +  movslq %eax,%r13 # highlighted
    +  movq %r13,%rsi # highlighted
    +  call sum_store@PLT # highlighted
    +  movq (%rsp),%r8
    +  movq %r13,%rcx
    +  movq %r12,%rdx
    +  leaq .LC0(%rip),%rsi
    +  movl $1,%edi
    +  xorl %eax,%eax
    +  call __printf_chk@PLT
    +  jmp .L3
    +.L8:
    +  call __stack_chk_fail@PLT
    +
    + +

    What if the callee function has more than 6 arguments?

    + +
      +
    1. Caller and callee functions must obey function call convention when passing data during function call +
        +
      • Caller:
      • +
      +
        +
      • Before calling the callee function, the caller must copy the callee’s arguments (1 to 6) into specific registers: …
      • +
      • If a callee function has more than 6 arguments … then must push the rest of the arguments on the stack in reverse order + * Callee:
      • +
      • Before returning to caller, callee must copy returned value into register %rax
      • +
      +
    2. +
    + +

    2. Passing data mechanism – using stack x86-64 function call convention

    + +

    2) When passing data that is a memory address (i.e., a +pointer) during function call

    + +
      +
    • Caller: +
        +
      • Must make use of the stack in order to create such memory address
      • +
      +
    • +
    + +

    Passing data mechanism – Examples of local variables, arguments and pointers on the stack

    + +

    C code:

    + +
    long call_proc()
    +{
    +  long x1 = 1; //How to push x4 and &x4 onto stack?
    +  int x2 = 2;
    +  short x3 = 3;
    +  char x4 = 4;
    +  proc(x1, &x1, x2, &x2,
    +       x3, &x3, x4, &x4);
    +  return (x1+x2)*(x3-x4);
    +}
    +
    + +

    Assembly:

    + +
    call_proc:
    +  subq $40, %rsp
    +  movq $1, 32(%rsp) # local variable
    +  movl $2, 28(%rsp) # local variable
    +  movw $3, 26(%rsp) # local variable
    +  movb $4, 25(%rsp) # local variable
    +  movq 32(%rsp), %rdi
    +  movl 28(%rsp), %edx
    +  leaq 25(%rsp), %rax
    +  movq %rax, 8(%rsp)
    +  movl $4, (%rsp)
    +  leaq 32(%rsp), %rsi
    +  leaq 28(%rsp), %rcx
    +  leaq 26(%rsp), %r9
    +  movl $3, %r8d
    +  callq proc
    +  ...
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    base + displacementStack Variable
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +

    Register Table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +

    Summary

    + +
      +
    • Passing data mechanism +
        +
      • x86-64 function call convention:
      • +
      +
    • +
    + +

    First 6 arguments:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameRegister
    argument 1%rdi
    argument 2%rsi
    argument 3%rdx
    argument 4%rcx
    argument 5%r8
    argument 6%r9
    return value%rax
    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterStackNote
      
     argument nStored onto the stack in reverse order
     Stored onto the stack in reverse order
     argument 8Stored onto the stack in reverse order
    %rspargument 7Stored onto the stack in reverse order
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • (highlighted) Managing local data
      • +
      • (highlighted) Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/18/18/index.html b/_site/melody/cmpt-295/18/18/index.html new file mode 100644 index 0000000..331ed04 --- /dev/null +++ b/_site/melody/cmpt-295/18/18/index.html @@ -0,0 +1,605 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    CMPT 295: Unit - Machine-Level Programming

    + +

    Lecture 18:

    + +
      +
    • Assembly language
    • +
    • Program Control
    • +
    • Function Call and Stack
    • +
    • Passing Data
    • +
    + +

    Last Lecture

    + +
      +
    • Function call mechanisms: 1) passing control, 2) passing data, 3) +managing local data on memory (stack)
    • +
    • Memory layout +
        +
      • Stack (local variables …)
      • +
      • Heap (dynamically allocated data)
      • +
      • Data (statically allocated data)
      • +
      • Text / Shared Libraries (program code)
      • +
      +
    • +
    • A “stack” is the right data structure for function call / return +
        +
      • If multstore calls mult2, then mult2 returns before multstore returns
      • +
      +
    • +
    • x86-64 stack register and instructions: stack pointer %rsp, push and pop
    • +
    • x86-64 function call instructions: call and ret
    • +
    + +

    From Lecture 17 – Slide 14

    + +

    Why 8?

    + +
      +
    • pushq src +
        +
      • Fetch value of operand src
      • +
      • Decrement %rsp by 8
      • +
      • Write value at address given by %rsp
      • +
      +
    • +
    • popq dest +
        +
      • Read value at %rsp (address) and +store it in operand dest (must be register)
      • +
      • Increment %rsp by 8
      • +
      +
    • +
    + +

    1) %rsp contains the memory address 0x0018

    + + + + + + + + + + + + + + + + + + + + + + +
    RegisterMemory Address
    %rsp0x0018
     0x0010
     0x0008
    + +

    2) %rsp contains the memory address 0x0010

    + + + + + + + + + + + + + + + + + + + + + + +
    RegisterMemory Address
     0x0018
    %rsp0x0010
     0x0008
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • (highlighted) Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    2. Passing data mechanism – using stack x86-64 function call convention

    + +
      +
    1. Caller and callee functions must obey function call convention when +passing data during function call +
        +
      • Caller:
      • +
      +
        +
      • Before calling the callee function, the caller must copy the callee’s arguments (1 to 6) into specific registers: If there is a … +
          +
        • 1st argument -> %rdi (or %edi, or %di or %dil)
        • +
        • 2nd argument -> %rsi (or %esi, or %si or %sil)
        • +
        • 3rd argument -> %rdx (or %edx, or %dx or %dl)
        • +
        • 4th argument -> %rcx (or %ecx, or %cx or %cl)
        • +
        • 5th argument -> %r8 (or %r8d, or %r8w or %r8b)
        • +
        • 6th argument -> %r9 (or %r9d, or %r9w or %r9b) + * Callee:
        • +
        +
      • +
      • Before returning to caller, callee must copy returned value into register %rax
      • +
      +
    2. +
    + +

    Passing data mechanism – Example of passing arguments in registers and returning return value

    + +

    C code:

    + +
    long plus(long x, long y){
    +  return x+y;
    +}
    +
    +void sum_store(long x, long y, long *dest)
    +{
    +  long sum = plus(x,y);
    +  *dest = sum;
    +}
    +
    +int main(int argc, char *argv[]) {
    +  if (argc == 3) {
    +    long x = atoi(argv[1]);
    +    long y = atoi(argv[2]);
    +    long result;
    +    sum_store(x, y, &result);
    +    printf("%ld + %ld --> %ld\n", x, y, result);
    +  else printf("2 numbers required\n");
    +  return 0;
    +}
    +
    + +

    Assembly code:

    + +
    sum_store:
    +.LFB40:
    +  .cfi_startproc
    +  endbr64
    +  addq  %rsi,%rdi
    +  movq %rdi,(%rdx)
    +  ret
    +main:
    +  pushq %r13
    +  pushq %r12
    +  pushq %rbx
    +  subq $16,%rsp
    +  movq %fs:40,%rax
    +  movq %rax,8(%rsp)
    +  xorl %eax,%eax
    +  cmpl $3,%edi # highlighted
    +  je .L7 # highlighted
    +  leaq .LC1(%rip), %rdi
    +  call puts@PLT
    +.L3:
    +  movq 8(%rsp),%rax
    +  xorq %fs:40,%rax
    +  jne .L8
    +  addq $16,%rsp
    +  xorl %eax,%eax
    +  popq %rbx
    +  popq %r12
    +  popq %r13
    +  ret
    +.L7:
    +  movq 8(%rsi),%rdi
    +  movq %rsi,%rbx
    +  movl $10,%edx
    +  xorl %esi,%esi
    +  call strtol@PLT
    +  movq 16(%rbx),%rdi
    +  xorl %esi,%esi
    +  movl $10,%edx
    +  movslq %eax,%r12 # highlighted
    +  call strtol@PLT
    +  movq %rsp,%rdx # highlighted
    +  movq %r12,%rdi # highlighted
    +  movslq %eax,%r13 # highlighted
    +  movq %r13,%rsi # highlighted
    +  call sum_store@PLT # highlighted
    +  movq (%rsp),%r8
    +  movq %r13,%rcx
    +  movq %r12,%rdx
    +  leaq .LC0(%rip),%rsi
    +  movl $1,%edi
    +  xorl %eax,%eax
    +  call __printf_chk@PLT
    +  jmp .L3
    +.L8:
    +  call __stack_chk_fail@PLT
    +
    + +

    What if the callee function has more than 6 arguments?

    + +
      +
    1. Caller and callee functions must obey function call convention when passing data during function call +
        +
      • Caller:
      • +
      +
        +
      • Before calling the callee function, the caller must copy the callee’s arguments (1 to 6) into specific registers: …
      • +
      • If a callee function has more than 6 arguments … then must push the rest of the arguments on the stack in reverse order + * Callee:
      • +
      • Before returning to caller, callee must copy returned value into register %rax
      • +
      +
    2. +
    + +

    2. Passing data mechanism – using stack x86-64 function call convention

    + +

    2) When passing data that is a memory address (i.e., a +pointer) during function call

    + +
      +
    • Caller: +
        +
      • Must make use of the stack in order to create such memory address
      • +
      +
    • +
    + +

    Passing data mechanism – Examples of local variables, arguments and pointers on the stack

    + +

    C code:

    + +
    long call_proc()
    +{
    +  long x1 = 1; //How to push x4 and &x4 onto stack?
    +  int x2 = 2;
    +  short x3 = 3;
    +  char x4 = 4;
    +  proc(x1, &x1, x2, &x2,
    +       x3, &x3, x4, &x4);
    +  return (x1+x2)*(x3-x4);
    +}
    +
    + +

    Assembly:

    + +
    call_proc:
    +  subq $40, %rsp
    +  movq $1, 32(%rsp) # local variable
    +  movl $2, 28(%rsp) # local variable
    +  movw $3, 26(%rsp) # local variable
    +  movb $4, 25(%rsp) # local variable
    +  movq 32(%rsp), %rdi
    +  movl 28(%rsp), %edx
    +  leaq 25(%rsp), %rax
    +  movq %rax, 8(%rsp)
    +  movl $4, (%rsp)
    +  leaq 32(%rsp), %rsi
    +  leaq 28(%rsp), %rcx
    +  leaq 26(%rsp), %r9
    +  movl $3, %r8d
    +  callq proc
    +  ...
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    base + displacementStack Variable
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +

    Register Table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +

    Summary

    + +
      +
    • Passing data mechanism +
        +
      • x86-64 function call convention:
      • +
      +
    • +
    + +

    First 6 arguments:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameRegister
    argument 1%rdi
    argument 2%rsi
    argument 3%rdx
    argument 4%rcx
    argument 5%r8
    argument 6%r9
    return value%rax
    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterStackNote
      
     argument nStored onto the stack in reverse order
     Stored onto the stack in reverse order
     argument 8Stored onto the stack in reverse order
    %rspargument 7Stored onto the stack in reverse order
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • (highlighted) Managing local data
      • +
      • (highlighted) Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/18/Lecture_18_Machine_Level_Prog_Fcn_Call_Passing_Data.pdf b/_site/melody/cmpt-295/18/Lecture_18_Machine_Level_Prog_Fcn_Call_Passing_Data.pdf new file mode 100644 index 0000000..670edcc Binary files /dev/null and b/_site/melody/cmpt-295/18/Lecture_18_Machine_Level_Prog_Fcn_Call_Passing_Data.pdf differ diff --git a/_site/melody/cmpt-295/19/19.html b/_site/melody/cmpt-295/19/19.html new file mode 100644 index 0000000..334d0e7 --- /dev/null +++ b/_site/melody/cmpt-295/19/19.html @@ -0,0 +1,994 @@ + + + + + | tait.tech + + + + +
    +
    +

    CMPT 295: Unit - Machine-Level Programming

    + +

    Lecture 19:

    + +
      +
    • Assembly language
    • +
    • Program Control
    • +
    • Function Call and Stack
    • +
    • Managing Local Data
    • +
    + +

    Last lecture

    + +
      +
    • Passing data mechanism +
        +
      • x86-64 function call convention:
      • +
      +
    • +
    + +

    First 6 arguments:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameRegister
    argument 1%rdi
    argument 2%rsi
    argument 3%rdx
    argument 4%rcx
    argument 5%r8
    argument 6%r9
    return value%rax
    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterStackNote
      
     argument nStored onto the stack in reverse order
     Stored onto the stack in reverse order
     argument 8Stored onto the stack in reverse order
    %rspargument 7Stored onto the stack in reverse order
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • (highlted) Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    To recap …

    + +
      +
    • Overview of Function Call mechanisms: +
        +
      • What happens when a function (caller) calls another function (callee)? +
          +
        1. Control is passed … + * To the beginning of the code in callee function + * Back to where callee function was called in caller function
        2. +
        3. Data is passed … (last lecture) + * To callee function via function parameter(s) + * Back to caller function via return value
        4. +
        5. Memory is … (allocated a stack frame on the stack, but what can be stored on this stack frame?) + * Allocated when callee function starts executing + * Deallocated when callee function stops executing
        6. +
        +
      • +
      +
    • +
    + +

    3. Managing local data

    + +
      +
    • When writing assembly programs, what can we use when we need space for our local data? +
        +
      • We can use registers! +
          +
        • Yes! Registers are our first choice as they are the fastest storage location on a computer.
        • +
        +
      • +
      • OK! but, since registers are shared by all functions in x86-64 assembly language, we need to follow some convention, otherwise … :
      • +
      +
    • +
    + +

    Assembly 1 (x86-64 function call convention):

    + +
    who:
    +  ...
    +  movq $15213, %rbx
    +  call amI
    +  addq %rbx, %rax
    +  ...
    +  ret
    +
    + +

    Assembly 2:

    + +
    amI:
    +  ...
    +  subq $18213, %rbx
    +  ...
    +  ret
    +
    + +

    Register Table:

    + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    + +

    3. Managing local data - “register saving” convention => callee saved registers

    + +

    “register saving” conventions:

    + +
      +
    1. calle saved registers
    2. +
    + +

    When we need space for our local data …

    + +
      +
    1. Registers +
        +
      • A function can utilise unused registers (only when needed)
      • +
      • Some registers are referred to as callee saved registers:
      • +
      +
        +
      • %rbx, %rbp, %r12 to %r15 (and %ebx, %bx, %bl, …) + * Callee saved registers means that … + * the callee function must preserve the values of these registers before using them, + * then restore their values before the control is returned (through the execution of ret instruction) to the caller function
      • +
      +
    2. +
    + +

    3. Managing local data - “register saving” convention => callee saved registers

    + +
      +
    • How can callee preserve the values of these callee saved registers before using them? +
        +
      • Example of a scenario: +
          +
        • Caller uses %r13
        • +
        • Caller calls callee
        • +
        • At the start of callee, callee pushq %r13
        • +
        • Then callee uses %r13
        • +
        • Then before execution flow returns from callee to caller (via ret), callee popq %r13 (Note: If callee pushq more than 1 register, then callee popq them in reverse order)
        • +
        • The execution flow returns to caller which continues using %r13
        • +
        +
      • +
      +
    • +
    + +

    Callee saved registers:

    + +

    Upon return from +callee, caller can +always assume that +these registers still +contain the values +caller stored in them +before calling callee!

    + +

    3. Managing local data - “register saving” convention => caller saved registers

    + +

    Register saving conventions:

    + +
      +
    1. Callee saved registers
    2. +
    3. +

      Caller saved registers

      +
    4. +
    5. Registers (cont’d) +
        +
      • Some registers are referred to as caller saved registers:
      • +
      +
        +
      • %r10, %r11, %rax and all 6 registers used for passing data as arguments to callee (and %r10d, %r10w, %r10b, …) + * Caller saved registers means that … + * the caller function must preserve the values of these registers before …
      • +
      • setting up the callee‘s argument(s) into the appropriate “data passing as argument” register(s) and
      • +
      • calling the callee + * then once the control is returned to the caller, the caller must restore their values before using them.
      • +
      +
    6. +
    + +

    Managing local data - “register saving” convention => caller saved registers

    + +
      +
    • How can caller preserve the values of these caller saved registers before using them? +
        +
      • Example of a scenario: +
          +
        • Caller uses %r10
        • +
        • Before calling callee, caller pushq %r10 then calls callee
        • +
        • Callee uses %r10
        • +
        • Then after the execution flow has returned from callee to caller (via ret), caller popq %r10 (If caller pushq more than 1 register, then caller popq them in reverse order)
        • +
        • Caller continues using %r10
        • +
        +
      • +
      +
    • +
    + +

    caller saved registers:

    + +

    Callee can always +assume that caller has +saved the content of +these registers, so it is +“safe” for callee to +use them!

    + +

    x86-64 “register saving” convention

    + +

    Solution 1:

    + +
    who:
    +  ...
    +  movq $15213, %rbx
    +  call amI
    +  addq %rbx, %rax
    +  ...
    +  ret
    +
    +amI:
    +  subq $18213, %rbx
    +  ret
    +
    + +

    Solution 2:

    + +
    who:
    +  ...
    +  movq $15213, %r10
    +
    +  call amI
    +
    +  addq %r10, %rax
    +  ...
    +  ret
    +
    + +
    amI:
    +  ...
    +  subq $18213, %r10
    +  ...
    +  ret
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    base + displacementStack VariablePurpose
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
    + +

    Register Table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +

    3. Managing local data => spilling

    + +
      +
    • When writing assembly programs, what can we use when we need space for our local data? +
        +
      • We can use stack! (If we run out of registers).
      • +
      +
    • +
    • +

      2) Stack

      + +
        +
      • A function can use the stack to store the values of its local variables and for temporary spaceMust remember to clean-up the stack before returning to caller!
      • +
      +
    • +
    • Set-up and Clean-up code: +
        +
      • Example: subq $16, %rsp and addq $16, %rsp
      • +
      +
    • +
    • To spill onto the stack: +
        +
      • Example: movq %rax, 56(%rsp)
      • +
      +
    • +
    + +

    Must remember to clean-up the stack before returning to caller.

    + +

    Local variables on Stack – Example

    + +
    long incr(long *p, long val)
    +{
    +  long x = *p;
    +  long y = x + val;
    +  *p = y;
    +  return x;
    +}
    +
    + +
    long call_incr() {
    +  long v1 = 15213;
    +  long v2 = incr(&v1, 3000);
    +  return v1+v2;
    +}
    +
    + +

    Assembly:

    + +
    call_incr:
    +  subq $16, %rsp # highlighted
    +  movq $15213, 8(%rsp) # highlighted
    +  movl $3000, %esi
    +  leaq 8(%rsp), %rdi
    +  call incr
    +  addq 8(%rsp), %rax
    +  addq $16, %rsp
    +  ret
    +
    + + + + + + + + + + + + +
    RegisterM[] Stack
    %rsp 
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    base + displacementStack VariablePurpose
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
    + +

    Register Table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +

    Summary - x86-64 “register saving” convention

    + +

    callee saved registers:

    + +
      +
    • Callee must save & restore before modifying
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValueNote
    %rbx  
    %r12  
    %r13  
    %r14  
    %r15  
    %rbp Parameters/arguments
    %rsp return value
    + +

    caller saved registers:

    + +
      +
    • Caller must save & restore
    • +
    • Can be modified by callee
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValueNote
    %rax Return value
    %rax Parameters/arguments
    %rdi Parameters/arguments
    %rsi Parameters/arguments
    %rdx Parameters/arguments
    %rcx Parameters/arguments
    %r8 Parameters/arguments
    %r9 Parameters/arguments
    %r10  
    %r11  
    + +

    Summary - x86-64 conventions and stack frame

    + +
      +
    • caller preserves caller saved registers (arrow)
    • +
    • caller passes arguments (arrow)
    • +
    • caller calls callee (arrow)
    • +
    • callee preserves callee saved registers (arrow)
    • +
    • callee constructs local vars (get stack space) (arrow)
    • +
    • callee performs function
    • +
    • callee recycles local vars (restore stack space)
    • +
    • callee restores callee saved registers
    • +
    • callee returns to caller
    • +
    • caller pops arguments
    • +
    • caller restores caller saved registers
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    M[] StackNote
    caller frame
    caller saved regscaller frame
    args 7 … ncaller frame
    return addresscaller frame
    callee saved regscallee frame
    %rsp/Top/local varscallee frame
    + +

    Next lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • (highlighted) Recursion
      • +
      +
    • +
    • (highlighted) Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/19/19/index.html b/_site/melody/cmpt-295/19/19/index.html new file mode 100644 index 0000000..7595ef8 --- /dev/null +++ b/_site/melody/cmpt-295/19/19/index.html @@ -0,0 +1,996 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    CMPT 295: Unit - Machine-Level Programming

    + +

    Lecture 19:

    + +
      +
    • Assembly language
    • +
    • Program Control
    • +
    • Function Call and Stack
    • +
    • Managing Local Data
    • +
    + +

    Last lecture

    + +
      +
    • Passing data mechanism +
        +
      • x86-64 function call convention:
      • +
      +
    • +
    + +

    First 6 arguments:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameRegister
    argument 1%rdi
    argument 2%rsi
    argument 3%rdx
    argument 4%rcx
    argument 5%r8
    argument 6%r9
    return value%rax
    + +

    Stack:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterStackNote
      
     argument nStored onto the stack in reverse order
     Stored onto the stack in reverse order
     argument 8Stored onto the stack in reverse order
    %rspargument 7Stored onto the stack in reverse order
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • (highlted) Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    To recap …

    + +
      +
    • Overview of Function Call mechanisms: +
        +
      • What happens when a function (caller) calls another function (callee)? +
          +
        1. Control is passed … + * To the beginning of the code in callee function + * Back to where callee function was called in caller function
        2. +
        3. Data is passed … (last lecture) + * To callee function via function parameter(s) + * Back to caller function via return value
        4. +
        5. Memory is … (allocated a stack frame on the stack, but what can be stored on this stack frame?) + * Allocated when callee function starts executing + * Deallocated when callee function stops executing
        6. +
        +
      • +
      +
    • +
    + +

    3. Managing local data

    + +
      +
    • When writing assembly programs, what can we use when we need space for our local data? +
        +
      • We can use registers! +
          +
        • Yes! Registers are our first choice as they are the fastest storage location on a computer.
        • +
        +
      • +
      • OK! but, since registers are shared by all functions in x86-64 assembly language, we need to follow some convention, otherwise … :
      • +
      +
    • +
    + +

    Assembly 1 (x86-64 function call convention):

    + +
    who:
    +  ...
    +  movq $15213, %rbx
    +  call amI
    +  addq %rbx, %rax
    +  ...
    +  ret
    +
    + +

    Assembly 2:

    + +
    amI:
    +  ...
    +  subq $18213, %rbx
    +  ...
    +  ret
    +
    + +

    Register Table:

    + + + + + + + + + + + + + + +
    RegisterValue
    %rbx 
    + +

    3. Managing local data - “register saving” convention => callee saved registers

    + +

    “register saving” conventions:

    + +
      +
    1. calle saved registers
    2. +
    + +

    When we need space for our local data …

    + +
      +
    1. Registers +
        +
      • A function can utilise unused registers (only when needed)
      • +
      • Some registers are referred to as callee saved registers:
      • +
      +
        +
      • %rbx, %rbp, %r12 to %r15 (and %ebx, %bx, %bl, …) + * Callee saved registers means that … + * the callee function must preserve the values of these registers before using them, + * then restore their values before the control is returned (through the execution of ret instruction) to the caller function
      • +
      +
    2. +
    + +

    3. Managing local data - “register saving” convention => callee saved registers

    + +
      +
    • How can callee preserve the values of these callee saved registers before using them? +
        +
      • Example of a scenario: +
          +
        • Caller uses %r13
        • +
        • Caller calls callee
        • +
        • At the start of callee, callee pushq %r13
        • +
        • Then callee uses %r13
        • +
        • Then before execution flow returns from callee to caller (via ret), callee popq %r13 (Note: If callee pushq more than 1 register, then callee popq them in reverse order)
        • +
        • The execution flow returns to caller which continues using %r13
        • +
        +
      • +
      +
    • +
    + +

    Callee saved registers:

    + +

    Upon return from +callee, caller can +always assume that +these registers still +contain the values +caller stored in them +before calling callee!

    + +

    3. Managing local data - “register saving” convention => caller saved registers

    + +

    Register saving conventions:

    + +
      +
    1. Callee saved registers
    2. +
    3. +

      Caller saved registers

      +
    4. +
    5. Registers (cont’d) +
        +
      • Some registers are referred to as caller saved registers:
      • +
      +
        +
      • %r10, %r11, %rax and all 6 registers used for passing data as arguments to callee (and %r10d, %r10w, %r10b, …) + * Caller saved registers means that … + * the caller function must preserve the values of these registers before …
      • +
      • setting up the callee‘s argument(s) into the appropriate “data passing as argument” register(s) and
      • +
      • calling the callee + * then once the control is returned to the caller, the caller must restore their values before using them.
      • +
      +
    6. +
    + +

    Managing local data - “register saving” convention => caller saved registers

    + +
      +
    • How can caller preserve the values of these caller saved registers before using them? +
        +
      • Example of a scenario: +
          +
        • Caller uses %r10
        • +
        • Before calling callee, caller pushq %r10 then calls callee
        • +
        • Callee uses %r10
        • +
        • Then after the execution flow has returned from callee to caller (via ret), caller popq %r10 (If caller pushq more than 1 register, then caller popq them in reverse order)
        • +
        • Caller continues using %r10
        • +
        +
      • +
      +
    • +
    + +

    caller saved registers:

    + +

    Callee can always +assume that caller has +saved the content of +these registers, so it is +“safe” for callee to +use them!

    + +

    x86-64 “register saving” convention

    + +

    Solution 1:

    + +
    who:
    +  ...
    +  movq $15213, %rbx
    +  call amI
    +  addq %rbx, %rax
    +  ...
    +  ret
    +
    +amI:
    +  subq $18213, %rbx
    +  ret
    +
    + +

    Solution 2:

    + +
    who:
    +  ...
    +  movq $15213, %r10
    +
    +  call amI
    +
    +  addq %r10, %rax
    +  ...
    +  ret
    +
    + +
    amI:
    +  ...
    +  subq $18213, %r10
    +  ...
    +  ret
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    base + displacementStack VariablePurpose
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
    + +

    Register Table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +

    3. Managing local data => spilling

    + +
      +
    • When writing assembly programs, what can we use when we need space for our local data? +
        +
      • We can use stack! (If we run out of registers).
      • +
      +
    • +
    • +

      2) Stack

      + +
        +
      • A function can use the stack to store the values of its local variables and for temporary spaceMust remember to clean-up the stack before returning to caller!
      • +
      +
    • +
    • Set-up and Clean-up code: +
        +
      • Example: subq $16, %rsp and addq $16, %rsp
      • +
      +
    • +
    • To spill onto the stack: +
        +
      • Example: movq %rax, 56(%rsp)
      • +
      +
    • +
    + +

    Must remember to clean-up the stack before returning to caller.

    + +

    Local variables on Stack – Example

    + +
    long incr(long *p, long val)
    +{
    +  long x = *p;
    +  long y = x + val;
    +  *p = y;
    +  return x;
    +}
    +
    + +
    long call_incr() {
    +  long v1 = 15213;
    +  long v2 = incr(&v1, 3000);
    +  return v1+v2;
    +}
    +
    + +

    Assembly:

    + +
    call_incr:
    +  subq $16, %rsp # highlighted
    +  movq $15213, 8(%rsp) # highlighted
    +  movl $3000, %esi
    +  leaq 8(%rsp), %rdi
    +  call incr
    +  addq 8(%rsp), %rax
    +  addq $16, %rsp
    +  ret
    +
    + + + + + + + + + + + + +
    RegisterM[] Stack
    %rsp 
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    base + displacementStack VariablePurpose
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
    + +

    Register Table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +

    Summary - x86-64 “register saving” convention

    + +

    callee saved registers:

    + +
      +
    • Callee must save & restore before modifying
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValueNote
    %rbx  
    %r12  
    %r13  
    %r14  
    %r15  
    %rbp Parameters/arguments
    %rsp return value
    + +

    caller saved registers:

    + +
      +
    • Caller must save & restore
    • +
    • Can be modified by callee
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValueNote
    %rax Return value
    %rax Parameters/arguments
    %rdi Parameters/arguments
    %rsi Parameters/arguments
    %rdx Parameters/arguments
    %rcx Parameters/arguments
    %r8 Parameters/arguments
    %r9 Parameters/arguments
    %r10  
    %r11  
    + +

    Summary - x86-64 conventions and stack frame

    + +
      +
    • caller preserves caller saved registers (arrow)
    • +
    • caller passes arguments (arrow)
    • +
    • caller calls callee (arrow)
    • +
    • callee preserves callee saved registers (arrow)
    • +
    • callee constructs local vars (get stack space) (arrow)
    • +
    • callee performs function
    • +
    • callee recycles local vars (restore stack space)
    • +
    • callee restores callee saved registers
    • +
    • callee returns to caller
    • +
    • caller pops arguments
    • +
    • caller restores caller saved registers
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    M[] StackNote
    caller frame
    caller saved regscaller frame
    args 7 … ncaller frame
    return addresscaller frame
    callee saved regscallee frame
    %rsp/Top/local varscallee frame
    + +

    Next lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • (highlighted) Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • (highlighted) Recursion
      • +
      +
    • +
    • (highlighted) Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/19/Lecture_19_Machine_Level_Prog_Fcn_Call_Mem_Mgmt.pdf b/_site/melody/cmpt-295/19/Lecture_19_Machine_Level_Prog_Fcn_Call_Mem_Mgmt.pdf new file mode 100644 index 0000000..ebff073 Binary files /dev/null and b/_site/melody/cmpt-295/19/Lecture_19_Machine_Level_Prog_Fcn_Call_Mem_Mgmt.pdf differ diff --git a/_site/melody/cmpt-295/20/20.html b/_site/melody/cmpt-295/20/20.html new file mode 100644 index 0000000..f8da210 --- /dev/null +++ b/_site/melody/cmpt-295/20/20.html @@ -0,0 +1,1178 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    CMPT 295 - Unit - Machine-Level Programming

    + +

    Lecture 20:

    + +
      +
    • Assembly language
    • +
    • Array
    • +
    • 1D
    • +
    + +

    Last Lecture - x86-64 “register saving” convention

    + +

    callee saved registers:

    + +
      +
    • Callee must save & restore
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterNote
    %rbx 
    %r12 
    %r13 
    %r14 
    %r15 
    %rbpparameters/arguments
    %rspreturn value
    + +

    caller saved registers:

    + +
      +
    • Caller must save & restore
    • +
    • Can be modified by callee
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterNote
    %raxReturn value
    %rdiParameters/arguments
    %rsiParameters/arguments
    %rdxParameters/arguments
    %rcxParameters/arguments
    %r8Parameters/arguments
    %r9Parameters/arguments
    %r10 
    %r11 
    + +

    Last Lecture - x86-64 conventions and stack frame

    + +
      +
    • caller preserves caller saved registers (arrow)
    • +
    • caller passes arguments (arrow)
    • +
    • caller calls callee (arrow)
    • +
    • callee preserves callee saved registers (arrow)
    • +
    • callee constructs local vars (get stack space) (arrow)
    • +
    • callee performs function
    • +
    • callee recycles local vars (restore stack space)
    • +
    • callee restores callee saved registers
    • +
    • callee returns to caller
    • +
    • caller pops arguments
    • +
    • caller restores caller saved registers
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    M[] StackNote
    caller frame
    caller saved regscaller frame
    args 7 … ncaller frame
    return addresscaller frame
    callee saved regscallee frame
    %rsp/Top/local varscallee frame
    + +

    Recursion in x86-64

    + +
      +
    • Handled without special instruction (We already know how to implement recursive functions in x86-64 assembly language) +
        +
      1. Call / return mechanism still follow the stack pattern. + * If P (first invocation of P) calls itself (second invocation of P), then the second invocation of P must terminate before the first invocation of P can do so.
      2. +
      3. Each invocation of the recursive function has its own stack frame. + * Saved registers & local variables + * Argument 7..n (if any) + * Saved return address +
          +
        • Function call conventions still required and implemented +
            +
          • Passing arguments and returning return value.
          • +
          +
        • +
        • Regular saving conventions still required and implemented +
            +
          • Prevent one function from corrupting antoher’s data (stored in registers)
          • +
          +
        • +
        +
      4. +
      +
    • +
    • Also works for mutual recursion +
        +
      • P calls Q then Q calls P
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + +
    LabelM[] Stack
     Stack frame of 1st invocation of P
     Stack frame of 2nd invocation of P
    topStack frmae of 3rd invocation of P
    + +

    Recursive Function – countOnesR(…)

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0){
    +    return 0;
    +  } else {
    +    return (x & 1) + countOnesR(x >> 1);
    +  }
    +}
    +
    + +

    What does this function do?

    + +

    Recursive Function – Example – Base Case

    + +

    C program:

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0) //highlighted
    +    return 0; //highlighted
    +  else
    +    return (x & 1) + countOnesR(x >> 1);
    +}
    +
    + +

    Assembly:

    + +
    countOnesR:
    +  xorl %eax, %eax #highlighted
    +  testq %rdi, %rdi #highlighted
    +  je done #highlighted
    +  pushq %rbx
    +  movq %rdi, %rbx
    +  andl $1, %ebx
    +  shrq %rdi
    +  call countOnesR
    +  addq %rbx, %rax
    +  popq %rbx
    +done: #highlighted
    +  ret #highlighted
    +
    + +

    Recursive Function – Example - Saving registers

    + +

    C code:

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0)
    +    return 0;
    +  else
    +    return (x & 1) + countOnesR(x >> 1);
    +}
    +
    + +

    Assembly:

    + +
    countOnesR:
    +  xorl %eax, %eax
    +  testq %rdi, %rdi
    +  je done
    +  pushq %rbx #highlighted
    +  movq %rdi, %rbx
    +  andl $1, %ebx
    +  shrq %rdi
    +  call countOnesR
    +  addq %rbx, %rax
    +  popq %rbx #highlighted
    +done:
    +  ret
    +
    + +

    Recursive Function – Example - Call Setup

    + +

    C code:

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0)
    +    return 0;
    +  else
    +    return (x & 1) + countOnesR(x >> 1); // between the parenthasies are highlighted
    +}
    +
    + +

    Assembly code:

    + +
    countOnesR:
    +  xorl %eax, %eax
    +  testq %rdi, %rdi
    +  je done
    +  pushq %rbx
    +  movq %rdi, %rbx #highlighted
    +  andl $1, %ebx #highlighted
    +  shrq %rdi #highlighted
    +  call countOnesR
    +  addq %rbx, %rax
    +  popq %rbx
    +done:
    +  ret
    +
    + +

    Recursive Function – Example – Recursive Call

    + +

    C code:

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0)
    +    return 0;
    +  else
    +    return (x & 1) + countOnesR(x >> 1);// countOnesR(x >> 1) is highlighted
    +}
    +
    + +

    Assembly:

    + +
    countOnesR:
    +  xorl %eax, %eax
    +  testq %rdi, %rdi
    +  je done
    +  pushq %rbx
    +  movq %rdi, %rbx
    +  andl $1, %ebx
    +  shrq %rdi
    +  call countOnesR
    +  addq %rbx, %rax
    +  popq %rbx
    +done:
    +  ret
    +
    + +

    Recursive Function – Example – Result

    + +

    C code:

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0)
    +    return 0;
    +  else
    +    return (x & 1) + countOnesR(x >> 1); // + is highlighted
    +}
    +
    + +

    Assembly:

    + +
    countOnesR:
    +  xorl %eax, %eax
    +  testq %rdi, %rdi
    +  je done
    +  pushq %rbx
    +  movq %rdi, %rbx
    +  andl $1, %ebx
    +  shrq %rdi
    +  call countOnesR
    +  addq %rbx, %rax #highlighted
    +  popq %rbx
    +done:
    +  ret
    +
    + +

    Recursive Function – Example – Clean-up and return

    + +

    C code:

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0)
    +    return 0;
    +  else
    +    return (x & 1) + countOnesR(x >> 1);
    +}
    +
    + +

    Assembly:

    + +
    countOnesR:
    +  xorl %eax, %eax
    +  testq %rdi, %rdi
    +  je done
    +  pushq %rbx
    +  movq %rdi, %rbx
    +  andl $1, %ebx
    +  shrq %rdi
    +  call countOnesR
    +  addq %rbx, %rax
    +  popq %rbx
    +done:
    +  ret
    +
    + +

    Recursive Function – Example – Test Cases

    + +
      +
    • Test Case 1 +
        +
      • Input: x = 0
      • +
      • Expected result: 0
      • +
      +
    • +
    • Test Case 2 +
        +
      • Input: x = 7
      • +
      • Expected result: 3
      • +
      +
    • +
    + +
    countOnesR:
    +  xorl %eax, %eax
    +  testq %rdi, %rdi
    +  je done
    +  pushq %rbx
    +  movq %rdi, %rbx
    +  andl $1, %ebx
    +  shrq %rdi
    +  call countOnesR
    +  addq %rbx, %rax
    +  popq %rbx
    +done:
    +  ret
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    base + displacementStack Variable
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +

    Register table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +

    1D Array – in C

    + +

    In C, arrays are also pointers!

    + +

    Diagram of: int x[5];

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Address/VariableValue
    x1
    x+15
    x+22
    x+31
    x+43
    x+5end of array
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    I want…TypeValue of result
    x[4]int 
    xint * 
    x+1int * 
    &x[2]int * 
    x[5]int 
    *(x+1)int 
    x+iint * 
    + +

    1D Array – in C and in x86-64

    + +

    T A[n];

    + +
      +
    • … is an array of data type T and length n => in C
    • +
    • … is a contiguously allocated region of n * L bytes in memory where L = sizeof(T) => in x86-64
    • +
    + +

    char x[12];

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Name/AddressValue
    x 
      
      
      
      
      
      
      
      
      
      
    x+? 
    + +

    int x[5];

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Name/AddressValue
    x 
      
      
      
    x+? 
      
    + +

    long x[3];

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Name/AddressValue
    x 
    x+? 
      
      
    + +

    char *x[3];

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Name/AddressValue
    x 
      
      
    x+? 
    + +

    We access arrays differently in x86-64 than in C!

    + +

    Accessing 1D Array – in x86-64

    + +
      +
    • Use index arithmatic to compute memory address of each array element +
        +
      • Memory address of A[i]=A+i×LA[i] = A+i \times L +
          +
        • where A is base memory address +
            +
          • i.e., A memory address of array element 0
          • +
          +
        • +
        +
      • +
      • Can access other array elements by incrementing A by i*L
      • +
      +
    • +
    + +

    1D Array – in x86-64

    + +

    int x[5];

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Name/AddressValue
    x1
    x+45
    x+82
    x+121
    x+163
    x+20end of array
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    I want…TypeValue of result (x86-64 instruction)
    x[4]int 
    xint * 
    x+1int * 
    &x[2]int * 
    x[5]int 
    *(x+1)int 
    x+iint * 
    + +

    Manipulating 1D array – Example - main.c

    + +

    This program defines 4 arrays:

    + +
      +
    • an array of char’s,
    • +
    • an array of short’s,
    • +
    • an array of int’s,
    • +
    • an array of long’s
    • +
    + +

    then it calls the appropriate sum function, i.e., the one +that sums elements of the correct data type.

    + +
    #include <stdio.h>
    +#include <stdlib.h>
    +
    +char sumChar(char*,int);
    +short sumShort(short*,int);
    +int sumInt(int*,int);
    +long sumLong(long*,int);
    +
    +#define N 6
    +
    +char AC[N] = {-58,22,101,-15,72,27}; // Expected results: sum = -107
    +short AS[N] = {-58,22,101,-15,72,27}; // Expected results: sum = 149
    +int AI[N] = {258,522,1010,-15,-3372,27};// Expected results: -1570
    +long AL[N] = {258,522,1010,-15,-3370,27}; // Expected results: -1570
    +
    +int main(){
    +  printf("The total of AC is %hhi.\n", sumChar(AC, N));
    +  printf("The total of AS is %hi.\n", sumShort(AS, N));
    +  printf("The total of AI is %d.\n", sumInt(AI, N));
    +  printf("The total of AL is %ld.\n", sumLong(AL, N));
    +  return;
    +}
    +
    + +

    Manipulating 1D array – Example - sums.s - Part 1

    + +
      +
    • Register %rdi contains starting address of array (base address of array)
    • +
    • Register %esi contains size of array (N)
    • +
    • Register %ecx contains array index
    • +
    • Register %al or %ax contains the running sum
    • +
    + +

    sumChar assembly:

    + +
      .global sumChar
    +sumChar:
    +  movl $0,%eax
    +  movl $0,%ecx
    +loopChar:
    +  cmpl %ecx,%esi
    +  jle endloop1
    +  addb (%rdi,%rcx,1),%al
    +  incl %ecx
    +  jmp loopChar
    +endloop1:
    +  ret
    +
    + +

    sumShort assembly:

    + +
      .global sumShort
    +sumShort:
    +  xorl %eax,%eax
    +  xorl %ecx,%ecx
    +  jmp condl
    +loopShort:
    +  addw (%rdi,%rcx,2), %ax
    +  incl %ecx
    +condl:
    +  cmpl %esi,%ecx
    +  jne loopShort
    +  ret
    +
    + +

    Manipulating 1D array – Example - sums.s - Part 2

    + +

    Register %rdi contains starting address of array (base address of array)

    +
      +
    • Register %esi contains size of array (N)
    • +
    • Register %ecx contains array index
    • +
    • Register %eax or %rax contains the running sum
    • +
    + +

    sumInt assembly:

    + +
      .global sumint
    +sumInt:
    +  xorl %eax,%eax
    +  xorl %ecx,%ecx
    +  jmp cond2
    +loopInt:
    +  addl (%rdi,%rcx,4),%eax
    +  incl %ecx
    +cond2:
    +  jmpl %esi,%ecx
    +  jne loopInt
    +  ret
    +
    + +

    sumLong in assembly:

    + +
      .global sumLong
    +sumLong:
    +  movq $0,%rax
    +  movl $0,%ecx
    +loopLoop:
    +  cmpl %ecx,%esi,
    +  jle endloop
    +  addq (%rdi,%rcx,8),%rax
    +  incl %ecx
    +  jmp loopLoop
    +endloop:
    +  ret
    +
    + +

    Warning!

    + +
      +
    • Forgetting that the memory addresses of contiguous array +cells differ by the size of the content of these cells instead of +by 1 is a common mistake in assembly language +programming
    • +
    + +

    Summary

    + +
      +
    • Arrays +
        +
      • Elements packed into contiguous region of memory
      • +
      • Use index arithmetic to locate individual elements
      • +
      • 1D array: address of A[i] = A + i * L
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation
    • +
    • Memory addressing modes
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • (highlighted) Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/20/20/index.html b/_site/melody/cmpt-295/20/20/index.html new file mode 100644 index 0000000..f8da210 --- /dev/null +++ b/_site/melody/cmpt-295/20/20/index.html @@ -0,0 +1,1178 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    CMPT 295 - Unit - Machine-Level Programming

    + +

    Lecture 20:

    + +
      +
    • Assembly language
    • +
    • Array
    • +
    • 1D
    • +
    + +

    Last Lecture - x86-64 “register saving” convention

    + +

    callee saved registers:

    + +
      +
    • Callee must save & restore
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterNote
    %rbx 
    %r12 
    %r13 
    %r14 
    %r15 
    %rbpparameters/arguments
    %rspreturn value
    + +

    caller saved registers:

    + +
      +
    • Caller must save & restore
    • +
    • Can be modified by callee
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterNote
    %raxReturn value
    %rdiParameters/arguments
    %rsiParameters/arguments
    %rdxParameters/arguments
    %rcxParameters/arguments
    %r8Parameters/arguments
    %r9Parameters/arguments
    %r10 
    %r11 
    + +

    Last Lecture - x86-64 conventions and stack frame

    + +
      +
    • caller preserves caller saved registers (arrow)
    • +
    • caller passes arguments (arrow)
    • +
    • caller calls callee (arrow)
    • +
    • callee preserves callee saved registers (arrow)
    • +
    • callee constructs local vars (get stack space) (arrow)
    • +
    • callee performs function
    • +
    • callee recycles local vars (restore stack space)
    • +
    • callee restores callee saved registers
    • +
    • callee returns to caller
    • +
    • caller pops arguments
    • +
    • caller restores caller saved registers
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    M[] StackNote
    caller frame
    caller saved regscaller frame
    args 7 … ncaller frame
    return addresscaller frame
    callee saved regscallee frame
    %rsp/Top/local varscallee frame
    + +

    Recursion in x86-64

    + +
      +
    • Handled without special instruction (We already know how to implement recursive functions in x86-64 assembly language) +
        +
      1. Call / return mechanism still follow the stack pattern. + * If P (first invocation of P) calls itself (second invocation of P), then the second invocation of P must terminate before the first invocation of P can do so.
      2. +
      3. Each invocation of the recursive function has its own stack frame. + * Saved registers & local variables + * Argument 7..n (if any) + * Saved return address +
          +
        • Function call conventions still required and implemented +
            +
          • Passing arguments and returning return value.
          • +
          +
        • +
        • Regular saving conventions still required and implemented +
            +
          • Prevent one function from corrupting antoher’s data (stored in registers)
          • +
          +
        • +
        +
      4. +
      +
    • +
    • Also works for mutual recursion +
        +
      • P calls Q then Q calls P
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + +
    LabelM[] Stack
     Stack frame of 1st invocation of P
     Stack frame of 2nd invocation of P
    topStack frmae of 3rd invocation of P
    + +

    Recursive Function – countOnesR(…)

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0){
    +    return 0;
    +  } else {
    +    return (x & 1) + countOnesR(x >> 1);
    +  }
    +}
    +
    + +

    What does this function do?

    + +

    Recursive Function – Example – Base Case

    + +

    C program:

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0) //highlighted
    +    return 0; //highlighted
    +  else
    +    return (x & 1) + countOnesR(x >> 1);
    +}
    +
    + +

    Assembly:

    + +
    countOnesR:
    +  xorl %eax, %eax #highlighted
    +  testq %rdi, %rdi #highlighted
    +  je done #highlighted
    +  pushq %rbx
    +  movq %rdi, %rbx
    +  andl $1, %ebx
    +  shrq %rdi
    +  call countOnesR
    +  addq %rbx, %rax
    +  popq %rbx
    +done: #highlighted
    +  ret #highlighted
    +
    + +

    Recursive Function – Example - Saving registers

    + +

    C code:

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0)
    +    return 0;
    +  else
    +    return (x & 1) + countOnesR(x >> 1);
    +}
    +
    + +

    Assembly:

    + +
    countOnesR:
    +  xorl %eax, %eax
    +  testq %rdi, %rdi
    +  je done
    +  pushq %rbx #highlighted
    +  movq %rdi, %rbx
    +  andl $1, %ebx
    +  shrq %rdi
    +  call countOnesR
    +  addq %rbx, %rax
    +  popq %rbx #highlighted
    +done:
    +  ret
    +
    + +

    Recursive Function – Example - Call Setup

    + +

    C code:

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0)
    +    return 0;
    +  else
    +    return (x & 1) + countOnesR(x >> 1); // between the parenthasies are highlighted
    +}
    +
    + +

    Assembly code:

    + +
    countOnesR:
    +  xorl %eax, %eax
    +  testq %rdi, %rdi
    +  je done
    +  pushq %rbx
    +  movq %rdi, %rbx #highlighted
    +  andl $1, %ebx #highlighted
    +  shrq %rdi #highlighted
    +  call countOnesR
    +  addq %rbx, %rax
    +  popq %rbx
    +done:
    +  ret
    +
    + +

    Recursive Function – Example – Recursive Call

    + +

    C code:

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0)
    +    return 0;
    +  else
    +    return (x & 1) + countOnesR(x >> 1);// countOnesR(x >> 1) is highlighted
    +}
    +
    + +

    Assembly:

    + +
    countOnesR:
    +  xorl %eax, %eax
    +  testq %rdi, %rdi
    +  je done
    +  pushq %rbx
    +  movq %rdi, %rbx
    +  andl $1, %ebx
    +  shrq %rdi
    +  call countOnesR
    +  addq %rbx, %rax
    +  popq %rbx
    +done:
    +  ret
    +
    + +

    Recursive Function – Example – Result

    + +

    C code:

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0)
    +    return 0;
    +  else
    +    return (x & 1) + countOnesR(x >> 1); // + is highlighted
    +}
    +
    + +

    Assembly:

    + +
    countOnesR:
    +  xorl %eax, %eax
    +  testq %rdi, %rdi
    +  je done
    +  pushq %rbx
    +  movq %rdi, %rbx
    +  andl $1, %ebx
    +  shrq %rdi
    +  call countOnesR
    +  addq %rbx, %rax #highlighted
    +  popq %rbx
    +done:
    +  ret
    +
    + +

    Recursive Function – Example – Clean-up and return

    + +

    C code:

    + +
    /* Recursive counter of 1’s */
    +long countOnesR(unsigned long x) {
    +  if (x == 0)
    +    return 0;
    +  else
    +    return (x & 1) + countOnesR(x >> 1);
    +}
    +
    + +

    Assembly:

    + +
    countOnesR:
    +  xorl %eax, %eax
    +  testq %rdi, %rdi
    +  je done
    +  pushq %rbx
    +  movq %rdi, %rbx
    +  andl $1, %ebx
    +  shrq %rdi
    +  call countOnesR
    +  addq %rbx, %rax
    +  popq %rbx
    +done:
    +  ret
    +
    + +

    Recursive Function – Example – Test Cases

    + +
      +
    • Test Case 1 +
        +
      • Input: x = 0
      • +
      • Expected result: 0
      • +
      +
    • +
    • Test Case 2 +
        +
      • Input: x = 7
      • +
      • Expected result: 3
      • +
      +
    • +
    + +
    countOnesR:
    +  xorl %eax, %eax
    +  testq %rdi, %rdi
    +  je done
    +  pushq %rbx
    +  movq %rdi, %rbx
    +  andl $1, %ebx
    +  shrq %rdi
    +  call countOnesR
    +  addq %rbx, %rax
    +  popq %rbx
    +done:
    +  ret
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    base + displacementStack Variable
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +

    Register table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +

    1D Array – in C

    + +

    In C, arrays are also pointers!

    + +

    Diagram of: int x[5];

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Address/VariableValue
    x1
    x+15
    x+22
    x+31
    x+43
    x+5end of array
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    I want…TypeValue of result
    x[4]int 
    xint * 
    x+1int * 
    &x[2]int * 
    x[5]int 
    *(x+1)int 
    x+iint * 
    + +

    1D Array – in C and in x86-64

    + +

    T A[n];

    + +
      +
    • … is an array of data type T and length n => in C
    • +
    • … is a contiguously allocated region of n * L bytes in memory where L = sizeof(T) => in x86-64
    • +
    + +

    char x[12];

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Name/AddressValue
    x 
      
      
      
      
      
      
      
      
      
      
    x+? 
    + +

    int x[5];

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Name/AddressValue
    x 
      
      
      
    x+? 
      
    + +

    long x[3];

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Name/AddressValue
    x 
    x+? 
      
      
    + +

    char *x[3];

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Name/AddressValue
    x 
      
      
    x+? 
    + +

    We access arrays differently in x86-64 than in C!

    + +

    Accessing 1D Array – in x86-64

    + +
      +
    • Use index arithmatic to compute memory address of each array element +
        +
      • Memory address of A[i]=A+i×LA[i] = A+i \times L +
          +
        • where A is base memory address +
            +
          • i.e., A memory address of array element 0
          • +
          +
        • +
        +
      • +
      • Can access other array elements by incrementing A by i*L
      • +
      +
    • +
    + +

    1D Array – in x86-64

    + +

    int x[5];

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Name/AddressValue
    x1
    x+45
    x+82
    x+121
    x+163
    x+20end of array
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    I want…TypeValue of result (x86-64 instruction)
    x[4]int 
    xint * 
    x+1int * 
    &x[2]int * 
    x[5]int 
    *(x+1)int 
    x+iint * 
    + +

    Manipulating 1D array – Example - main.c

    + +

    This program defines 4 arrays:

    + +
      +
    • an array of char’s,
    • +
    • an array of short’s,
    • +
    • an array of int’s,
    • +
    • an array of long’s
    • +
    + +

    then it calls the appropriate sum function, i.e., the one +that sums elements of the correct data type.

    + +
    #include <stdio.h>
    +#include <stdlib.h>
    +
    +char sumChar(char*,int);
    +short sumShort(short*,int);
    +int sumInt(int*,int);
    +long sumLong(long*,int);
    +
    +#define N 6
    +
    +char AC[N] = {-58,22,101,-15,72,27}; // Expected results: sum = -107
    +short AS[N] = {-58,22,101,-15,72,27}; // Expected results: sum = 149
    +int AI[N] = {258,522,1010,-15,-3372,27};// Expected results: -1570
    +long AL[N] = {258,522,1010,-15,-3370,27}; // Expected results: -1570
    +
    +int main(){
    +  printf("The total of AC is %hhi.\n", sumChar(AC, N));
    +  printf("The total of AS is %hi.\n", sumShort(AS, N));
    +  printf("The total of AI is %d.\n", sumInt(AI, N));
    +  printf("The total of AL is %ld.\n", sumLong(AL, N));
    +  return;
    +}
    +
    + +

    Manipulating 1D array – Example - sums.s - Part 1

    + +
      +
    • Register %rdi contains starting address of array (base address of array)
    • +
    • Register %esi contains size of array (N)
    • +
    • Register %ecx contains array index
    • +
    • Register %al or %ax contains the running sum
    • +
    + +

    sumChar assembly:

    + +
      .global sumChar
    +sumChar:
    +  movl $0,%eax
    +  movl $0,%ecx
    +loopChar:
    +  cmpl %ecx,%esi
    +  jle endloop1
    +  addb (%rdi,%rcx,1),%al
    +  incl %ecx
    +  jmp loopChar
    +endloop1:
    +  ret
    +
    + +

    sumShort assembly:

    + +
      .global sumShort
    +sumShort:
    +  xorl %eax,%eax
    +  xorl %ecx,%ecx
    +  jmp condl
    +loopShort:
    +  addw (%rdi,%rcx,2), %ax
    +  incl %ecx
    +condl:
    +  cmpl %esi,%ecx
    +  jne loopShort
    +  ret
    +
    + +

    Manipulating 1D array – Example - sums.s - Part 2

    + +

    Register %rdi contains starting address of array (base address of array)

    +
      +
    • Register %esi contains size of array (N)
    • +
    • Register %ecx contains array index
    • +
    • Register %eax or %rax contains the running sum
    • +
    + +

    sumInt assembly:

    + +
      .global sumint
    +sumInt:
    +  xorl %eax,%eax
    +  xorl %ecx,%ecx
    +  jmp cond2
    +loopInt:
    +  addl (%rdi,%rcx,4),%eax
    +  incl %ecx
    +cond2:
    +  jmpl %esi,%ecx
    +  jne loopInt
    +  ret
    +
    + +

    sumLong in assembly:

    + +
      .global sumLong
    +sumLong:
    +  movq $0,%rax
    +  movl $0,%ecx
    +loopLoop:
    +  cmpl %ecx,%esi,
    +  jle endloop
    +  addq (%rdi,%rcx,8),%rax
    +  incl %ecx
    +  jmp loopLoop
    +endloop:
    +  ret
    +
    + +

    Warning!

    + +
      +
    • Forgetting that the memory addresses of contiguous array +cells differ by the size of the content of these cells instead of +by 1 is a common mistake in assembly language +programming
    • +
    + +

    Summary

    + +
      +
    • Arrays +
        +
      • Elements packed into contiguous region of memory
      • +
      • Use index arithmetic to locate individual elements
      • +
      • 1D array: address of A[i] = A + i * L
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation
    • +
    • Memory addressing modes
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • (highlighted) Array
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/20/Lecture_20_Machine_Level_Prog_Array_1D.pdf b/_site/melody/cmpt-295/20/Lecture_20_Machine_Level_Prog_Array_1D.pdf new file mode 100644 index 0000000..94c6906 Binary files /dev/null and b/_site/melody/cmpt-295/20/Lecture_20_Machine_Level_Prog_Array_1D.pdf differ diff --git a/_site/melody/cmpt-295/21/21.html b/_site/melody/cmpt-295/21/21.html new file mode 100644 index 0000000..5cd0e9d --- /dev/null +++ b/_site/melody/cmpt-295/21/21.html @@ -0,0 +1,354 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 - Unit - Machine-Level Programming

    + +

    Lecture 21:

    + +
      +
    • Assembly language
    • +
    • Array
    • +
    • 2D
    • +
    + +

    Last lecture

    + +
      +
    • Recursion +
        +
      • Handled without special instruction +
          +
        • Stack frames
        • +
        • x86-64 Function call and Register saving conventions
        • +
        +
      • +
      +
    • +
    • Manipulation of arrays – in x86-64 +
        +
      • From x86-64’s perspective, an array is a contiguously allocated region of n * L bytes in memory where L = sizeof( T ) and T -> data type of elements stored in array
      • +
      +
    • +
    • Compute memory address of each array element +
        +
      • + A[i]=A+iLA[i] = A + i * L +
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • (highlighted) Array (cont’d)
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    2D Array

    + +

    Visual representation:

    + +
      +
    • A[0][0] … A[0][C-1]
    • +
    • A[1][0] … A[1][C-1]
    • +
    • +
    • A[R-1][0] … A[R-1][C-1]
    • +
    + +

    T A[P][C]:

    + +
      +
    • (in C) is a 2D array of data type T, R rows, C columns
    • +
    • (in x86-64) is a contiguously allocated region of R×C×LR \times C \times L bytes in memory where L = sizeof(T)
    • +
    + +

    Array layout in memory: Row-major ordering -> Example using int A[R][C]:

    + +
      +
    • A[0][0]
    • +
    • +
    • A[0][C-1]
    • +
    • A[1][0]
    • +
    • +
    • A[1][C-1]
    • +
    • +
    • A[R-1][0]
    • +
    • +
    • A[R-1][C-1]
    • +
    + +

    Accessing a row of 2D array

    + +

    T A[R][C];

    + +
      +
    • A[i] is an array of C elements (row i of array A)
    • +
    • Memory address of each row A[i]: A + (i * C * L) +
        +
      • where A is base memory address
      • +
      +
    • +
    • Can access other rows by incrementing A by i * C * L
    • +
    • Example using int A[R][C];
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CellsRowMemory Arithmatic
    A[0][0]A[0]A
    A[0] 
    A[0][C-1]A[0] 
    A[1][0]A[1]A + (1*C*4)
    A[1] 
    A[1][C-1]A[1] 
      
    A[R-1][0]A[R-1]A + ((R-1)*C*4)
    A[R-1] 
    A[R-1][C-1]A[R-1] 
    + +

    Accessing an element of 2D array

    + +

    T A[R][C];

    + +

    *A[i][j] is element of type T, which requires L bytes +*Memory address of each element A[i][j]: +A + (i * C * L) + (j * L) = A + (i * C

    + +
      +
    • j) * L
    • +
    + +

    *Example using int A[R][C]; +A[0] +A[0][0]

    + +

    A +6

    + +

    + +

    A[R-1]

    + +

    A[i] +A[0][C-1] …

    + +

    + +

    A+(iC4)

    + +

    A[i][j]

    + +

    + +

    … A[R-1][0]

    + +

    + +

    A+((R-1)C4)

    + +

    A[R-1][C-1]

    + +

    Homework

    + +

    Example: int A[3][5];

    +
      +
    • In memory: +0
    • +
    + +

    1

    + +

    2

    + +

    3

    + +

    4

    + +

    5

    + +

    6

    + +

    7

    + +

    8

    + +

    9

    + +

    10

    + +

    11

    + +

    12

    + +
      +
    • Let’s compute the memory address to access:
    • +
    • +

      A[2]:

      +
    • +
    • A[2][3]:
    • +
    + +

    7

    + +

    13

    + +

    14

    + +

    Demo - Accessing an element of 2D array

    + +

    8

    + +
      +
    • copy.s on our course web site
    • +
    + +

    Summary

    +
      +
    • Manipulation of 2D arrays – in x86-64
    • +
    + +

    *From x86-64’s perspective, a 2D array is a contiguously +allocated region of R * C * L bytes in memory where +L = sizeof( T ) and T -> data type of elements stored +in array +*2D Array layout in memory: Row-Major ordering +*Memory address of each row A[i]: A + (i * C * L) +*Memory address of each element A[i][j]: +A + (i * C * L) + (j * L)

    + +

    => A + (i * C +9

    + +
      +
    • j) * L
    • +
    + +

    Next Lecture

    +
      +
    • Introduction
    • +
    • +

      C program -> assembly code -> machine level code

      +
    • +
    • Assembly language basics: data, move operation
    • +
    • +

      Memory addressing modes

      +
    • +
    • +

      Operation leaq and Arithmetic & logical operations

      +
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack
    • +
    • +

      Overview of Function Call

      +
    • +
    • Memory Layout and Stack - x86-64 instructions and registers
    • +
    • Passing control
    • +
    • Passing data – Calling Conventions
    • +
    • +

      Managing local data

      +
    • +
    • +

      Recursion

      +
    • +
    • Array
    • +
    • Buffer Overflow
    • +
    + +

    10

    + +
      +
    • Floating-point operations
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/21/21/index.html b/_site/melody/cmpt-295/21/21/index.html new file mode 100644 index 0000000..5e98bae --- /dev/null +++ b/_site/melody/cmpt-295/21/21/index.html @@ -0,0 +1,377 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 - Unit - Machine-Level Programming

    + +

    Lecture 21:

    + +
      +
    • Assembly language
    • +
    • Array
    • +
    • 2D
    • +
    + +

    Last lecture

    + +
      +
    • Recursion +
        +
      • Handled without special instruction +
          +
        • Stack frames
        • +
        • x86-64 Function call and Register saving conventions
        • +
        +
      • +
      +
    • +
    • Manipulation of arrays – in x86-64 +
        +
      • From x86-64’s perspective, an array is a contiguously allocated region of n * L bytes in memory where L = sizeof( T ) and T -> data type of elements stored in array
      • +
      +
    • +
    • Compute memory address of each array element +
        +
      • + A[i]=A+iLA[i] = A + i * L +
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • (highlighted) Array (cont’d)
    • +
    • Buffer Overflow
    • +
    • Floating-point operations
    • +
    + +

    2D Array

    + +

    Visual representation:

    + +
      +
    • A[0][0] … A[0][C-1]
    • +
    • A[1][0] … A[1][C-1]
    • +
    • +
    • A[R-1][0] … A[R-1][C-1]
    • +
    + +

    T A[P][C]:

    + +
      +
    • (in C) is a 2D array of data type T, R rows, C columns
    • +
    • (in x86-64) is a contiguously allocated region of R×C×LR \times C \times L bytes in memory where L = sizeof(T)
    • +
    + +

    Array layout in memory: Row-major ordering -> Example using int A[R][C]:

    + +
      +
    • A[0][0]
    • +
    • +
    • A[0][C-1]
    • +
    • A[1][0]
    • +
    • +
    • A[1][C-1]
    • +
    • +
    • A[R-1][0]
    • +
    • +
    • A[R-1][C-1]
    • +
    + +

    Accessing a row of 2D array

    + +

    T A[R][C];

    + +
      +
    • A[i] is an array of C elements (row i of array A)
    • +
    • Memory address of each row A[i]: A + (i * C * L) +
        +
      • where A is base memory address
      • +
      +
    • +
    • Can access other rows by incrementing A by i * C * L
    • +
    • Example using int A[R][C];
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    CellsRowMemory Arithmatic
    A[0][0]A[0]A
    A[0] 
    A[0][C-1]A[0] 
    A[1][0]A[1]A + (1*C*4)
    A[1] 
    A[1][C-1]A[1] 
      
    A[R-1][0]A[R-1]A + ((R-1)*C*4)
    A[R-1] 
    A[R-1][C-1]A[R-1] 
    + +

    Accessing an element of 2D array

    + +

    T A[R][C];

    + +
      +
    • A[i][j] is element of type T, which requires L bytes
    • +
    • Memory address of each element A[i][j]: +A + (i * C * L) + (j * L) = A + (i * C + j) * L
    • +
    • Example using int A[R][C];
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ElementRowPointer Arithmatic
    A[0][0]A[0]A
    A[0] 
    A[0][C-1]A[0] 
    A[i]A+(iC4)
    A[i][j]A[i] 
    A[R-1][0]A[R-1]A+((R-1)C4)
    A[R-1] 
    A[R-1][C-1]A[R-1] 
    + +

    Homework

    + +

    Example: int A[3][5];

    + +

    In memory:

    + +
      +
    • 0 (red)
    • +
    • 1 (red)
    • +
    • 2 (red)
    • +
    • 3 (red)
    • +
    • 4 (red)
    • +
    • 5 (yellow)
    • +
    • 6 (yellow)
    • +
    • 7 (yellow)
    • +
    • 8 (yellow)
    • +
    • 9 (yellow)
    • +
    • 10 (green)
    • +
    • 11 (green)
    • +
    • 12 (green)
    • +
    • 13 (green)
    • +
    • 14 (green)
    • +
    • 15 (green)
    • +
    + +

    Let’s compute the memory address to access:

    + +
      +
    • A[2]: ____
    • +
    • A[2][3]: ____
    • +
    + +

    Demo - Accessing an element of 2D array

    + +

    Code:

    + +
    #define N 4
    +
    +char A[N][N] = {1,-2,3,-4,
    +                -5,6,-7,8,
    +                -1,2,-3,4,
    +                5,-6,7,-8};
    +
    +char C[N][N];
    +
    +void main(){
    +  printf("Original matrix: \n");
    +  printMatrixByRow(A, N);
    +  
    +  printf("Copy: \n");
    +  copy(A,C,N);
    +  printMatrixByRow(C, N);
    +
    +  return;
    +}
    +
    +// Print N elements in a row
    +void printMatrixByRow(void *D, int n){
    +  /* transcriber's note: code ends here */
    +
    + +

    Note: copy.s on our course web site

    + +

    Summary

    + +
      +
    • Manipulation of 2D arrays – in x86-64 +
        +
      • From x86-64’s perspective, a 2D array is a contiguously +allocated region of R * C * L bytes in memory where +L = sizeof( T ) and T -> data type of elements stored +in array
      • +
      +
    • +
    • 2D Array layout in memory: Row-Major ordering
    • +
    • Memory address of each row A[i]: A + (i * C * L)
    • +
    • Memory address of each element A[i][j]: +A + (i * C * L) + (j * L) => A + (i * C + j) * L
    • +
    + +

    Next Lecture

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • (highlighted) Buffer Overflow
    • +
    • (highlighted) Floating-point operations
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/21/Lecture_21_Machine_Level_Prog_Array_2D.pdf b/_site/melody/cmpt-295/21/Lecture_21_Machine_Level_Prog_Array_2D.pdf new file mode 100644 index 0000000..0c36db2 Binary files /dev/null and b/_site/melody/cmpt-295/21/Lecture_21_Machine_Level_Prog_Array_2D.pdf differ diff --git a/_site/melody/cmpt-295/22/22.html b/_site/melody/cmpt-295/22/22.html new file mode 100644 index 0000000..e0a05e3 --- /dev/null +++ b/_site/melody/cmpt-295/22/22.html @@ -0,0 +1,1149 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 - Unit - Machine-Level Programming

    + +

    Lecture 22:

    + +
      +
    • Buffer Overflow + Floating-point data & operations
    • +
    + +

    Last lecture

    + +
      +
    • Manipulation of 2D arrays – in x86-64 +
        +
      • From x86-64’s perspective, a 2D array is a contiguously +allocated region of R * C * L bytes in memory where +L = sizeof( T ) and T -> data type of elements stored +in array
      • +
      • 2D Array layout in memory: Row-Major ordering
      • +
      • Memory address of each row A[i]: A + (i * C * L)
      • +
      • Memory address of each element A[i][j]: A + (i * C * L) + (j * L) => A + (i * C + j) * L
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • (highlighted) Buffer Overflow
    • +
    • (highlighted) Floating-point data & operations
    • +
    + +

    Buffer Overflow

    + +

    C and Stack … so far

    + +
      +
    • C does not perform any bound checks on arrays
    • +
    • stored on the stack +
        +
      • Local variables in C programs
      • +
      • Callee and caller saved registers
      • +
      • Return addresses
      • +
      +
    • +
    • As we saw in Lab 2 and Lab 4, this may lead to trouble
    • +
    + +

    What kind of trouble? -> buffer overflow (overrun)

    + +
      +
    • If function does not perform bound-check when writing to a local array … +
        +
      • Here is a an example of a bound-check: if input size <= array size +write input into array … then it may write more data that the allocated +space (to array) can hold, hence overflowing the array -> buffer overflow
      • +
      +
    • +
    • Effect: the function may end up writing over, i.e., %rsp corrupting, data kept on the stack such as: +
        +
      • Value of local variables and registers
      • +
      • Return address
      • +
      +
    • +
    • Stack smashing
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + +
    M[]
    Stack
    ...
    return address
    Unused stack space
    local var
    buf[ ]
    %rsp -> Top
    + +

    Demo the trouble -> buffer overflow

    + +

    (Transcriber’s note: no content on slide)

    + +

    Why is buffer overflow a problem

    + +
      +
    • Corrupted data
    • +
    • Corrupted return address +
        +
      • Which may lead to segmentation fault +
          +
        • How?
        • +
        +
      • +
      • Which also makes a system vulnerable to attacks +
          +
        • How?
        • +
        +
      • +
      +
    • +
    + +

    Code injection attack

    + +
    void func1(){
    +  func2();
    +  // C statement
    +     at return
    +     address A
    +  ...
    +}
    +
    + +
    int func2() {
    +  char buf[64];
    +  gets(buf);
    +  ...
    +  return ...;
    +}
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    M[] StackStack Frame/Note
     
     func1 stack frame
    return addressfunc1 stack frame
    func2 stack frame
    buf[64]func2 stack frame
    same buf[64] section labled Bfunc2 stack frame
    same buf[64]func2 stack frame
    %rsptop
    + +
      +
    • An “attacker” could overflow the +buffer … array of char’s +
        +
      • … by inputting a string that contains byte representation of malicious executable code (exploit code) instead of legitimate characters
      • +
      • The string is written to array buf on stack and overwrites return address A with a return address that points to exploit code
      • +
      • When func2 executes ret instruction, it pops this erroneous return address onto PC (%rip) and jumps to exploit code
      • +
      • Microprocessor starts executing the exploit code at this location
      • +
      +
    • +
    + +

    How to protection against such attack

    + +
      +
    1. Avoid creating overflow vulnerabilities in the code that we write by always checking bounds +
        +
      • For example, by calling library functions that limit string lengths
      • +
      +
        +
      • “Unsafe” : gets(), strcpy(), strcat(), sprintf(), … +
          +
        • These functions can generate a byte sequence without being given any indication of the size of the destination buffer (see next slide) + * “Safe”: fgets()
        • +
        +
      • +
      +
    2. +
    + +

    From our Lab 4

    + +
    void proc1(char *s, int *a, int *b) {
    +  int y;
    +  int t;
    +
    +  t = *a;
    +  v = proc2(*a, *b);
    +
    +  sprintf(s, "The result of proc2(%d,%d) is %d.", *a, *b, v);
    +
    +  *a = *b - 2;
    +  *b = t;
    +
    +  return;
    +
    + +

    Suggestion from developer.apple.com

    + +
    char destination[5];
    +char * source = “LARGER”;
    +
    + +
      +
    1. `strcpy(destination, source); +
        +
      • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
        ColorValue
        WhiteL
        WhiteA
        WhiteR
        WhiteG
        WhiteE
        BrownR
        Brown\0
        Brown 
        +
      • +
      • Copies the string pointed +to by source (including +the null character) to the +destination and returns it.
      • +
      +
    2. +
    3. strncpy(destination, source, sizeof(destination)) +
        +
      • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
        ColorValue
        WhiteL
        WhiteA
        WhiteR
        WhiteG
        WhiteE
        Brown 
        Brown 
        Brown 
        +
      • +
      • Copies up to +sizeof(destination) -> n +characters from the +string pointed to by +source to destination. In +a case where the length +of source is less than n, +the remainder of +destination will be +padded with null bytes. +In a case where the +length of source is +greater than n, the +destination will contain +a truncated version of +source.
      • +
      +
    4. +
    5. strlcpy(destination, source, sizeof(destination)) +
        +
      • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
        ColorValue
        WhiteL
        WhiteA
        WhiteR
        WhiteG
        White\0
        Brown 
        Brown 
        Brown 
        +
      • +
      • Copies up to +sizeof(destination) - 1 +-> n - 1 characters +from null-terminated +source to destination, +it then “null” terminates +destination and returns +the length of source.
      • +
      +
    6. +
    + +

    https://linux.die.net/man/3/strlcpy

    + +

    How to protection against such attack

    + +

    2) Employ system-level protections -> Randomized stack offsets

    + +
      +
    • At start of program, system allocates +random amount of space on stack
    • +
    • Effect: Shifts stack addresses (%rsp) for +entire program +
        +
      • Shifts the memory address of all the stack +frames allocated to program’s functions +when they are called
      • +
      +
    • +
    • Hence, makes it difficult for hackers to +predict start of each stack frame (hence +where exploit code may have been +inserted) since stack is repositioned each +time program executes
    • +
    + + + + + + + + + + + + + + + + + + + + + + +
    M[]Note
     (crossed off) %rsp
    brown shaded box, no value 
    top%rsp
    + +

    How to protection against such attack

    + +

    2) Employ system-level protections -> Non-executable code segments

    + +
      +
    • In the old days of x86, memory +segments marked as either read-only +or writeable (both implied readable) +=> 2 types of permissions +
        +
      • Could execute anything readable
      • +
      +
    • +
    • x86-64 has added an explicit +executable permission
    • +
    • Stack segment now marked as nonexecutable +M[] Stack|Note +—|— +…| +|func1 stack frame +“return address A” (crossed out) B|func1 stack frame +padding (crossed out)|func2 stack frame +exploit code|func2 stack frame +B|func2 stack framme +Top|%rsp
    • +
    + +

    Any attempt to execute the bottom “B” set of code, will fail.

    + +

    How to protection against such attack

    + +

    3) Compiler (like gcc) uses a stack canary value

    + +
      +
    • History: Starting early 1900’s, +canaries used in the coal mines to +detect gas leaks
    • +
    • Push a randomized canary value +between an array and return +address on stack (remember our +Lab 4)
    • +
    • Before executing a ret instruction, +canary value is checked to see if it +has been corrupted +
        +
      • If so, failure reported
      • +
      +
    • +
    + +
    main: # main.c from our Lab 4
    +  endbr64
    +  pushq %rbp
    +  ...
    +  subq $64, %rsp
    +  movq %fs:40, %rax
    +  movq %rax, 56(%rsp)
    +  ...
    +  leaq 16(%rsp), %rbp
    +  ...
    +  movq 56(%rsp), %rax
    +  xorq %fs:40, %rax
    +  jne .L5
    +  addq $64, %rsp
    +  popq %rbp
    +  ret
    +.L5:
    +  call __stack_chk_fail@PLT
    +
    + +

    How to protection against such attack

    + +

    3) Newest version of our gcc compiler +(version 8 and up) uses Control-Flow +Enforcement Technology (CET) From stackoverflow

    + +
      +
    • Instruction endbr64 (End Branch 64 bit) -> Terminate Indirect Branch in 64 bit
    • +
    • Microprocessor tracks indirect branching +and ensures that all indirect calls lead to +(legal) functions starting with endbr64 +
        +
      • If function does -> microprocessor infers +that function is safe to execute
      • +
      • If function does not -> microprocessor +infers that control flow may have been +manipulated by some exploit code, i.e., +function is unsafe to execute and aborts!
      • +
      +
    • +
    + + + +

    Brief overview of floating-point data and operations

    + +

    (Transcriber’s node: no content on slide)

    + +

    Background

    + +
      +
    • Once upon a time in the ’90’s … +
        +
      • Use of computer graphics and image processing (multimedia) +applications were on the rise +
          +
        • Microprocessors (i.e., machine instruction sets) designed to +support such applications
        • +
        • Idea: speed up microprocessors by executing single +instruction on multiple data -> SIMD
        • +
        +
      • +
      +
    • +
    • Since then, microprocessors and their machine instruction sets +have evolved … +
        +
      • SSE (Streaming SIMD Extensions)
      • +
      • AVX (Advanced Vector EXtensions) -> textbook
      • +
      +
    • +
    + +

    XMM Registers

    + +

    x86-64 registers and instructions seen so far are referred to as integer registers and integer instructions +Now we introduce a new set of registers for floating point numbers:

    + +
      +
    • 16 in total, each 16-byte wide (128 bits), named: %xmm0, %xmm1, …, %xmm15
    • +
    • Scalar mode: +
        +
      • 1 single-precision float (32 bits). Diagram of memory showing 32128=14\frac{32}{128} = \frac{1}{4} utilization
      • +
      • 1 double-precision double (64 bits) 63. Diagram of memory showing 64128=12\frac{64}{128} = \frac{1}{2} utilization
      • +
      +
    • +
    • Vector mode (packed data) +
        +
      • 16 single-byte integers
      • +
      • 8 16-bit integers
      • +
      • 4 32-bit integers
      • +
      • 4 single-precision float’s
      • +
      • 2 double-precision double’s
      • +
      +
    • +
    + +

    Scalar versus Vector (SIMD) instructions

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Assembly InstructionOperation TypePercisionNote
    addss %xmm0,%xmm1scalarsingleAdd single precision at the last 32 bits of %xmm0 to the last 32 bit of %xmm1. Save in the last 32-bits of %xmm1.
    addps %xmm0,%xmm1SMID (packed)singleAdd 4 sets of single percision numbers. Each 32 bit section of %xmm0 is added to each 32 bit section of %xmm1.
    addsd %xmm0,%xmm1scalardoubleAdd two double-precision numbers. Add the last 64 bits of %xmm0 to the last 64 bits of %xmm1.
    addpd %xmm0,%xmm1SMID (packed)doubleAdd a pair of double-precision numbers. Add each 64 bit sections of %xmm0 to each 64 bit section of %xmm1. Store results in %xmm1.
    + +

    Data movement instructions

    + +

    Assembly:

    + +
    float_mov:
    +# --------# float float_mov(float f1,
    +#
    +float *src,
    +#
    +float *dst) {
    +# float f2 = *src;
    +# *dst = f1;
    +# return f2;
    +# }
    +# --------# f1 in %xmm0, src in %rdi, dst in %rsi
    +movss (%rdi), %xmm1 # f2 = *src
    +movss %xmm0, (%rsi) # *dst = f1
    +movaps %xmm1, %xmm0
    +# return value = f2
    +ret
    +
    + +
      +
    • The instructions we shall look at in this +lecture are different than the ones +presented in section 3.11 of our +textbook – we shall focus on the scalar +version of these instructions
    • +
    • movss – move single precision +
        +
      • Mem (32 bits) <–> %xmm
      • +
      +
    • +
    • movsd – move double precision +
        +
      • Mem (64 bits) <–> %xmm
      • +
      +
    • +
    • First 2 instructions of program: Memory +referencing operands (i.e., memory +addressing mode operands) specified +in the same way as for the integer mov* +instructions
    • +
    • movaps/movapd – move %xmm <–> %xmm +
        +
      • ap -> aligned packed
      • +
      +
    • +
    + +

    Function call and register saving conventions

    + +
      +
    • Function call convention +
        +
      • Integer (and pointer i.e., memory address) arguments passed in +integer registers
      • +
      • Floating point values passed in XMM registers
      • +
      • Argument 1 to argument 8 passed in %xmm0, %xmm1, …, %xmm7
      • +
      • Result returned in %xmm0
      • +
      +
    • +
    • Register saving convention +
        +
      • All XMM registers caller-saved
      • +
      • Can use register %xmm8 to %xmm15 for managing local data
      • +
      +
    • +
    + +

    Data conversion instructions

    + +

    Converting between data types: (“t” is for “truncate”)

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    fromintfloatlongdouble
    intN/Acvtsi2ssN/Acvtsi2sd
    floatcvttss2siN/Acvttss2siqcvtss2sd
    longN/Acvtsi2ssqN/Acvtsi2sdq
    doublecvtsi2sdcvtsd2sscvttsd2siqN/A
    + +

    Data manipulation instructions

    + +

    Arithmetic

    + +
      +
    • addss/addsd - floating point add
    • +
    • subss/subsd - … subtract
    • +
    • mulss/mulsd - … mul
    • +
    • divss/divsd - … div
    • +
    + +

    Logical

    + +
      +
    • andps/andpd
    • +
    • orps/d
    • +
    • xorps/d
    • +
    • xorpd %xmm0, %xmm0: effect %xmm0 <- 0
    • +
    + +

    Comparison: ucomiss/d

    + +
      +
    • Affects only condition codes: CF, ZF +
        +
      • use unsigned branches
      • +
      +
    • +
    • If NaN, set all of condition codes: +CF, ZF and PF +
        +
      • Use jp/jnp to branch on PF
      • +
      +
    • +
    + +

    Others

    + +
      +
    • maxss/maxsd - … max +
        +
      • For example: maxss %xmm3, %xmm5 +Effect: xmm5 <- max(xmm5, xmm3)
      • +
      +
    • +
    • minss/minsd - … min
    • +
    • sqrtss/sqrtsd - … square root
    • +
    + +

    Example

    + +
    fadd:
    +# --------
    +# float fadd(float x, float y){
    +#   return x + y;
    +# }
    +# --------
    +# x in %xmm0, y in %xmm1
    +  addss
    +  %xmm1, %xmm0
    +  ret
    +
    +dadd:
    +# --------
    +# double dadd(double x, double y){
    +#   return x + y;
    +# }
    +# --------
    +# x in %xmm0, y in %xmm1
    +  addsd
    +  %xmm1, %xmm0
    +  ret
    +
    + +

    Storing Data in Various Segments of Memory - Optional

    + +

    (Transcriber’s note: no content on slide)

    + +

    Storing Data in Memory

    + +

    This material is optional –> It is for your learning pleasure!

    + +

    We already +know about +data on stack +and on heap.

    + +
      +
    • Data on stack memory (on stack frame of function) +
        +
      • Temporarily use and recycle
      • +
      • Lasts through life of function call
      • +
      +
    • +
    • Data on heap +
        +
      • Temporarily use and recycle
      • +
      • Lasts until memory is “free’ed”
      • +
      +
    • +
    • Data in fixed memory, i.e., Data segment. What does this type of data look like? +
        +
      • Statically allocated data +
          +
        • e.g., global variables, static variables, string constants
        • +
        +
      • +
      • Lasts while program executes
      • +
      +
    • +
    + +

    Data stored in Data Segment

    + +

    This material is optional –> It is for your learning pleasure!

    + +
      +
    • Declared using a label & a directive for size +
        +
      • label is a memory address
      • +
      • size: .byte (1), .word (2), .long (4), .quad (8)
      • +
      • initial value
      • +
      +
    • +
    + +

    Example 1:

    + +

    C:

    + +
    long x = 6;
    +long y = 9;
    +void main {
    +  ...
    +}
    +
    + +

    x86-64:

    + +
    x: .quad 6 # 0x0000000000000006
    +y: .quad 9 # 0x0000000000000009
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    labelStack
    01234567
    y09 (LSB; remember little endian)00000000000000
    x06 (LSB)00000000000000
    + +

    Data stored in Data Segment

    + +

    This material is optional +–> It is for your +learning pleasure!

    + +

    C:

    + +
    #define N 6
    +
    +int A[N] = {12,34,56,78,-90,1};
    +
    +void main(){
    +  printf("The total is %d.\n", sum_arrau(A,N));
    +  return;
    +}
    +
    + +

    Assembly:

    + +
    main:
    +.LFB38:
    +  .cfi_startproc
    +  subq $0,%rsp
    +  .cfi_def_cfa_offset 16
    +  movl $6,%esi
    +  movl $A,%edi
    +  call sum_array
    +  movl %.LC0,%esi
    +  movl %eax,%eax
    +  movl $1,%edi
    +  xorl %eax,%eax
    +  addq $8,%rsp
    +  .cfi_def_cfa_offset 8
    +  jmp __printf_chk
    +...
    +A:
    +  .long 12 # or .long 12,34,56,78,-90,1
    +  .long 34
    +  .long 56
    +  .long 78
    +  .long -90
    +  .long 1
    +  .ident "GCC: (Ubuntu 7.3.0-21ubuntu1-16.04) 7.3.0"
    +  .section .note.GNU-stack,"",@progbits
    +
    + +

    Data stored on Stack – Example 1

    + +

    This material is optional –> It is for your learning pleasure!

    + +

    C:

    + +
    void main(int argc, char* argv){
    +  int A[] = {12,34,56,78,-90,1}; // 12 and 34 are highlighted.
    +  printf("The total is %d.\n", sum_array(A,N));
    +  return;
    +}
    +
    + +

    Assembly:

    + +
    main:
    +.LFB38:
    +  .cfi_startproc
    +  subq $40,%rsp
    +  .cfi_def_cfa_offset 48
    +  movl $6,%esi
    +  movq %fs:40,%rax
    +  movq %rax,24(%rsp)
    +  xorl %eax,%eax
    +  movabsq $146028888076,%rax # highloghted
    +  movq %rsp,%rdi
    +  movq %rax,(%rsp)
    +  movabsq $335007449144,%rax # highlighted
    +  movq %rax,8(%rsp)
    +  movabsq $8589934502,%rax # highlighted
    +  movq %rax,16(%rsp)
    +  call sum_array
    +  movl $.LC0,%esi
    +  movl %eax,%edx
    +  movl $1,%edi
    +  xorl %eax,%eax
    +  call __printf_chk
    +  movq 24(%rsp), %rax
    +  xorq %fs:40,%rax
    +  jne .L5
    +  addq $40,%rsp
    +  .cfi_remember_state
    +  .cfi_def_cfa_offset 8
    +  ret
    +
    + +

    How does this large # end up representing 12 and 34:

    + +
      +
    • Express $146028888076 in binary
    • +
    • Transform binary to hex => 0x000000220000000c
    • +
    • Read hex’s LSB (32 bits) (0000000c) as a decimal +=> 12
    • +
    • Read hex’s MSB (32 bits) (00000022) as a decimal +=> 34
    • +
    • Repeat for other 2 operands of movabsq +instructions
    • +
    + +

    Summary - 1

    + +
      +
    • What is a buffer overflow +
        +
      • When function writes more data in array than array can hold on stack
      • +
      • Effect: data kept on the stack (value of other local variables and registers, +return address) may be corrupted +-> Stack smashing
      • +
      +
    • +
    • Why buffer overflow spells trouble -> it creates vulnerability +
        +
      • Allowing hacker attacks
      • +
      +
    • +
    • How to protect system against such attacks +
        +
      1. Avoid creating overflow vulnerabilities in the code that we write +
          +
        • By always checking bounds and calling “safe” library functions that +consider size of array
        • +
        +
      2. +
      3. Employ system-level protections +
          +
        • Randomized initial stack pointer and non-executable code segments
        • +
        +
      4. +
      5. Use compiler (like gcc) security features: +
          +
        • Stack “canary” value and endbr64 instruction
        • +
        +
      6. +
      +
    • +
    + +

    Summary - 2

    + +
      +
    • Floating point data and operations +
        +
      • Data held and manipulated in XMM registers
      • +
      • Assembly language instructions similar to integer +assembly language instructions we have seen so far
      • +
      +
    • +
    + +

    Next Lecture

    + +

    Start a new unit …

    +
      +
    • Instruction Set Architecture (ISA)
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/22/22/index.html b/_site/melody/cmpt-295/22/22/index.html new file mode 100644 index 0000000..e0a05e3 --- /dev/null +++ b/_site/melody/cmpt-295/22/22/index.html @@ -0,0 +1,1149 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 - Unit - Machine-Level Programming

    + +

    Lecture 22:

    + +
      +
    • Buffer Overflow + Floating-point data & operations
    • +
    + +

    Last lecture

    + +
      +
    • Manipulation of 2D arrays – in x86-64 +
        +
      • From x86-64’s perspective, a 2D array is a contiguously +allocated region of R * C * L bytes in memory where +L = sizeof( T ) and T -> data type of elements stored +in array
      • +
      • 2D Array layout in memory: Row-Major ordering
      • +
      • Memory address of each row A[i]: A + (i * C * L)
      • +
      • Memory address of each element A[i][j]: A + (i * C * L) + (j * L) => A + (i * C + j) * L
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Introduction +
        +
      • C program -> assembly code -> machine level code
      • +
      +
    • +
    • Assembly language basics: data, move operation +
        +
      • Memory addressing modes
      • +
      +
    • +
    • Operation leaq and Arithmetic & logical operations
    • +
    • Conditional Statement – Condition Code + cmovX
    • +
    • Loops
    • +
    • Function call – Stack +
        +
      • Overview of Function Call
      • +
      • Memory Layout and Stack - x86-64 instructions and registers
      • +
      • Passing control
      • +
      • Passing data – Calling Conventions
      • +
      • Managing local data
      • +
      • Recursion
      • +
      +
    • +
    • Array
    • +
    • (highlighted) Buffer Overflow
    • +
    • (highlighted) Floating-point data & operations
    • +
    + +

    Buffer Overflow

    + +

    C and Stack … so far

    + +
      +
    • C does not perform any bound checks on arrays
    • +
    • stored on the stack +
        +
      • Local variables in C programs
      • +
      • Callee and caller saved registers
      • +
      • Return addresses
      • +
      +
    • +
    • As we saw in Lab 2 and Lab 4, this may lead to trouble
    • +
    + +

    What kind of trouble? -> buffer overflow (overrun)

    + +
      +
    • If function does not perform bound-check when writing to a local array … +
        +
      • Here is a an example of a bound-check: if input size <= array size +write input into array … then it may write more data that the allocated +space (to array) can hold, hence overflowing the array -> buffer overflow
      • +
      +
    • +
    • Effect: the function may end up writing over, i.e., %rsp corrupting, data kept on the stack such as: +
        +
      • Value of local variables and registers
      • +
      • Return address
      • +
      +
    • +
    • Stack smashing
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + +
    M[]
    Stack
    ...
    return address
    Unused stack space
    local var
    buf[ ]
    %rsp -> Top
    + +

    Demo the trouble -> buffer overflow

    + +

    (Transcriber’s note: no content on slide)

    + +

    Why is buffer overflow a problem

    + +
      +
    • Corrupted data
    • +
    • Corrupted return address +
        +
      • Which may lead to segmentation fault +
          +
        • How?
        • +
        +
      • +
      • Which also makes a system vulnerable to attacks +
          +
        • How?
        • +
        +
      • +
      +
    • +
    + +

    Code injection attack

    + +
    void func1(){
    +  func2();
    +  // C statement
    +     at return
    +     address A
    +  ...
    +}
    +
    + +
    int func2() {
    +  char buf[64];
    +  gets(buf);
    +  ...
    +  return ...;
    +}
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    M[] StackStack Frame/Note
     
     func1 stack frame
    return addressfunc1 stack frame
    func2 stack frame
    buf[64]func2 stack frame
    same buf[64] section labled Bfunc2 stack frame
    same buf[64]func2 stack frame
    %rsptop
    + +
      +
    • An “attacker” could overflow the +buffer … array of char’s +
        +
      • … by inputting a string that contains byte representation of malicious executable code (exploit code) instead of legitimate characters
      • +
      • The string is written to array buf on stack and overwrites return address A with a return address that points to exploit code
      • +
      • When func2 executes ret instruction, it pops this erroneous return address onto PC (%rip) and jumps to exploit code
      • +
      • Microprocessor starts executing the exploit code at this location
      • +
      +
    • +
    + +

    How to protection against such attack

    + +
      +
    1. Avoid creating overflow vulnerabilities in the code that we write by always checking bounds +
        +
      • For example, by calling library functions that limit string lengths
      • +
      +
        +
      • “Unsafe” : gets(), strcpy(), strcat(), sprintf(), … +
          +
        • These functions can generate a byte sequence without being given any indication of the size of the destination buffer (see next slide) + * “Safe”: fgets()
        • +
        +
      • +
      +
    2. +
    + +

    From our Lab 4

    + +
    void proc1(char *s, int *a, int *b) {
    +  int y;
    +  int t;
    +
    +  t = *a;
    +  v = proc2(*a, *b);
    +
    +  sprintf(s, "The result of proc2(%d,%d) is %d.", *a, *b, v);
    +
    +  *a = *b - 2;
    +  *b = t;
    +
    +  return;
    +
    + +

    Suggestion from developer.apple.com

    + +
    char destination[5];
    +char * source = “LARGER”;
    +
    + +
      +
    1. `strcpy(destination, source); +
        +
      • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
        ColorValue
        WhiteL
        WhiteA
        WhiteR
        WhiteG
        WhiteE
        BrownR
        Brown\0
        Brown 
        +
      • +
      • Copies the string pointed +to by source (including +the null character) to the +destination and returns it.
      • +
      +
    2. +
    3. strncpy(destination, source, sizeof(destination)) +
        +
      • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
        ColorValue
        WhiteL
        WhiteA
        WhiteR
        WhiteG
        WhiteE
        Brown 
        Brown 
        Brown 
        +
      • +
      • Copies up to +sizeof(destination) -> n +characters from the +string pointed to by +source to destination. In +a case where the length +of source is less than n, +the remainder of +destination will be +padded with null bytes. +In a case where the +length of source is +greater than n, the +destination will contain +a truncated version of +source.
      • +
      +
    4. +
    5. strlcpy(destination, source, sizeof(destination)) +
        +
      • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
        ColorValue
        WhiteL
        WhiteA
        WhiteR
        WhiteG
        White\0
        Brown 
        Brown 
        Brown 
        +
      • +
      • Copies up to +sizeof(destination) - 1 +-> n - 1 characters +from null-terminated +source to destination, +it then “null” terminates +destination and returns +the length of source.
      • +
      +
    6. +
    + +

    https://linux.die.net/man/3/strlcpy

    + +

    How to protection against such attack

    + +

    2) Employ system-level protections -> Randomized stack offsets

    + +
      +
    • At start of program, system allocates +random amount of space on stack
    • +
    • Effect: Shifts stack addresses (%rsp) for +entire program +
        +
      • Shifts the memory address of all the stack +frames allocated to program’s functions +when they are called
      • +
      +
    • +
    • Hence, makes it difficult for hackers to +predict start of each stack frame (hence +where exploit code may have been +inserted) since stack is repositioned each +time program executes
    • +
    + + + + + + + + + + + + + + + + + + + + + + +
    M[]Note
     (crossed off) %rsp
    brown shaded box, no value 
    top%rsp
    + +

    How to protection against such attack

    + +

    2) Employ system-level protections -> Non-executable code segments

    + +
      +
    • In the old days of x86, memory +segments marked as either read-only +or writeable (both implied readable) +=> 2 types of permissions +
        +
      • Could execute anything readable
      • +
      +
    • +
    • x86-64 has added an explicit +executable permission
    • +
    • Stack segment now marked as nonexecutable +M[] Stack|Note +—|— +…| +|func1 stack frame +“return address A” (crossed out) B|func1 stack frame +padding (crossed out)|func2 stack frame +exploit code|func2 stack frame +B|func2 stack framme +Top|%rsp
    • +
    + +

    Any attempt to execute the bottom “B” set of code, will fail.

    + +

    How to protection against such attack

    + +

    3) Compiler (like gcc) uses a stack canary value

    + +
      +
    • History: Starting early 1900’s, +canaries used in the coal mines to +detect gas leaks
    • +
    • Push a randomized canary value +between an array and return +address on stack (remember our +Lab 4)
    • +
    • Before executing a ret instruction, +canary value is checked to see if it +has been corrupted +
        +
      • If so, failure reported
      • +
      +
    • +
    + +
    main: # main.c from our Lab 4
    +  endbr64
    +  pushq %rbp
    +  ...
    +  subq $64, %rsp
    +  movq %fs:40, %rax
    +  movq %rax, 56(%rsp)
    +  ...
    +  leaq 16(%rsp), %rbp
    +  ...
    +  movq 56(%rsp), %rax
    +  xorq %fs:40, %rax
    +  jne .L5
    +  addq $64, %rsp
    +  popq %rbp
    +  ret
    +.L5:
    +  call __stack_chk_fail@PLT
    +
    + +

    How to protection against such attack

    + +

    3) Newest version of our gcc compiler +(version 8 and up) uses Control-Flow +Enforcement Technology (CET) From stackoverflow

    + +
      +
    • Instruction endbr64 (End Branch 64 bit) -> Terminate Indirect Branch in 64 bit
    • +
    • Microprocessor tracks indirect branching +and ensures that all indirect calls lead to +(legal) functions starting with endbr64 +
        +
      • If function does -> microprocessor infers +that function is safe to execute
      • +
      • If function does not -> microprocessor +infers that control flow may have been +manipulated by some exploit code, i.e., +function is unsafe to execute and aborts!
      • +
      +
    • +
    + + + +

    Brief overview of floating-point data and operations

    + +

    (Transcriber’s node: no content on slide)

    + +

    Background

    + +
      +
    • Once upon a time in the ’90’s … +
        +
      • Use of computer graphics and image processing (multimedia) +applications were on the rise +
          +
        • Microprocessors (i.e., machine instruction sets) designed to +support such applications
        • +
        • Idea: speed up microprocessors by executing single +instruction on multiple data -> SIMD
        • +
        +
      • +
      +
    • +
    • Since then, microprocessors and their machine instruction sets +have evolved … +
        +
      • SSE (Streaming SIMD Extensions)
      • +
      • AVX (Advanced Vector EXtensions) -> textbook
      • +
      +
    • +
    + +

    XMM Registers

    + +

    x86-64 registers and instructions seen so far are referred to as integer registers and integer instructions +Now we introduce a new set of registers for floating point numbers:

    + +
      +
    • 16 in total, each 16-byte wide (128 bits), named: %xmm0, %xmm1, …, %xmm15
    • +
    • Scalar mode: +
        +
      • 1 single-precision float (32 bits). Diagram of memory showing 32128=14\frac{32}{128} = \frac{1}{4} utilization
      • +
      • 1 double-precision double (64 bits) 63. Diagram of memory showing 64128=12\frac{64}{128} = \frac{1}{2} utilization
      • +
      +
    • +
    • Vector mode (packed data) +
        +
      • 16 single-byte integers
      • +
      • 8 16-bit integers
      • +
      • 4 32-bit integers
      • +
      • 4 single-precision float’s
      • +
      • 2 double-precision double’s
      • +
      +
    • +
    + +

    Scalar versus Vector (SIMD) instructions

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Assembly InstructionOperation TypePercisionNote
    addss %xmm0,%xmm1scalarsingleAdd single precision at the last 32 bits of %xmm0 to the last 32 bit of %xmm1. Save in the last 32-bits of %xmm1.
    addps %xmm0,%xmm1SMID (packed)singleAdd 4 sets of single percision numbers. Each 32 bit section of %xmm0 is added to each 32 bit section of %xmm1.
    addsd %xmm0,%xmm1scalardoubleAdd two double-precision numbers. Add the last 64 bits of %xmm0 to the last 64 bits of %xmm1.
    addpd %xmm0,%xmm1SMID (packed)doubleAdd a pair of double-precision numbers. Add each 64 bit sections of %xmm0 to each 64 bit section of %xmm1. Store results in %xmm1.
    + +

    Data movement instructions

    + +

    Assembly:

    + +
    float_mov:
    +# --------# float float_mov(float f1,
    +#
    +float *src,
    +#
    +float *dst) {
    +# float f2 = *src;
    +# *dst = f1;
    +# return f2;
    +# }
    +# --------# f1 in %xmm0, src in %rdi, dst in %rsi
    +movss (%rdi), %xmm1 # f2 = *src
    +movss %xmm0, (%rsi) # *dst = f1
    +movaps %xmm1, %xmm0
    +# return value = f2
    +ret
    +
    + +
      +
    • The instructions we shall look at in this +lecture are different than the ones +presented in section 3.11 of our +textbook – we shall focus on the scalar +version of these instructions
    • +
    • movss – move single precision +
        +
      • Mem (32 bits) <–> %xmm
      • +
      +
    • +
    • movsd – move double precision +
        +
      • Mem (64 bits) <–> %xmm
      • +
      +
    • +
    • First 2 instructions of program: Memory +referencing operands (i.e., memory +addressing mode operands) specified +in the same way as for the integer mov* +instructions
    • +
    • movaps/movapd – move %xmm <–> %xmm +
        +
      • ap -> aligned packed
      • +
      +
    • +
    + +

    Function call and register saving conventions

    + +
      +
    • Function call convention +
        +
      • Integer (and pointer i.e., memory address) arguments passed in +integer registers
      • +
      • Floating point values passed in XMM registers
      • +
      • Argument 1 to argument 8 passed in %xmm0, %xmm1, …, %xmm7
      • +
      • Result returned in %xmm0
      • +
      +
    • +
    • Register saving convention +
        +
      • All XMM registers caller-saved
      • +
      • Can use register %xmm8 to %xmm15 for managing local data
      • +
      +
    • +
    + +

    Data conversion instructions

    + +

    Converting between data types: (“t” is for “truncate”)

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    fromintfloatlongdouble
    intN/Acvtsi2ssN/Acvtsi2sd
    floatcvttss2siN/Acvttss2siqcvtss2sd
    longN/Acvtsi2ssqN/Acvtsi2sdq
    doublecvtsi2sdcvtsd2sscvttsd2siqN/A
    + +

    Data manipulation instructions

    + +

    Arithmetic

    + +
      +
    • addss/addsd - floating point add
    • +
    • subss/subsd - … subtract
    • +
    • mulss/mulsd - … mul
    • +
    • divss/divsd - … div
    • +
    + +

    Logical

    + +
      +
    • andps/andpd
    • +
    • orps/d
    • +
    • xorps/d
    • +
    • xorpd %xmm0, %xmm0: effect %xmm0 <- 0
    • +
    + +

    Comparison: ucomiss/d

    + +
      +
    • Affects only condition codes: CF, ZF +
        +
      • use unsigned branches
      • +
      +
    • +
    • If NaN, set all of condition codes: +CF, ZF and PF +
        +
      • Use jp/jnp to branch on PF
      • +
      +
    • +
    + +

    Others

    + +
      +
    • maxss/maxsd - … max +
        +
      • For example: maxss %xmm3, %xmm5 +Effect: xmm5 <- max(xmm5, xmm3)
      • +
      +
    • +
    • minss/minsd - … min
    • +
    • sqrtss/sqrtsd - … square root
    • +
    + +

    Example

    + +
    fadd:
    +# --------
    +# float fadd(float x, float y){
    +#   return x + y;
    +# }
    +# --------
    +# x in %xmm0, y in %xmm1
    +  addss
    +  %xmm1, %xmm0
    +  ret
    +
    +dadd:
    +# --------
    +# double dadd(double x, double y){
    +#   return x + y;
    +# }
    +# --------
    +# x in %xmm0, y in %xmm1
    +  addsd
    +  %xmm1, %xmm0
    +  ret
    +
    + +

    Storing Data in Various Segments of Memory - Optional

    + +

    (Transcriber’s note: no content on slide)

    + +

    Storing Data in Memory

    + +

    This material is optional –> It is for your learning pleasure!

    + +

    We already +know about +data on stack +and on heap.

    + +
      +
    • Data on stack memory (on stack frame of function) +
        +
      • Temporarily use and recycle
      • +
      • Lasts through life of function call
      • +
      +
    • +
    • Data on heap +
        +
      • Temporarily use and recycle
      • +
      • Lasts until memory is “free’ed”
      • +
      +
    • +
    • Data in fixed memory, i.e., Data segment. What does this type of data look like? +
        +
      • Statically allocated data +
          +
        • e.g., global variables, static variables, string constants
        • +
        +
      • +
      • Lasts while program executes
      • +
      +
    • +
    + +

    Data stored in Data Segment

    + +

    This material is optional –> It is for your learning pleasure!

    + +
      +
    • Declared using a label & a directive for size +
        +
      • label is a memory address
      • +
      • size: .byte (1), .word (2), .long (4), .quad (8)
      • +
      • initial value
      • +
      +
    • +
    + +

    Example 1:

    + +

    C:

    + +
    long x = 6;
    +long y = 9;
    +void main {
    +  ...
    +}
    +
    + +

    x86-64:

    + +
    x: .quad 6 # 0x0000000000000006
    +y: .quad 9 # 0x0000000000000009
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    labelStack
    01234567
    y09 (LSB; remember little endian)00000000000000
    x06 (LSB)00000000000000
    + +

    Data stored in Data Segment

    + +

    This material is optional +–> It is for your +learning pleasure!

    + +

    C:

    + +
    #define N 6
    +
    +int A[N] = {12,34,56,78,-90,1};
    +
    +void main(){
    +  printf("The total is %d.\n", sum_arrau(A,N));
    +  return;
    +}
    +
    + +

    Assembly:

    + +
    main:
    +.LFB38:
    +  .cfi_startproc
    +  subq $0,%rsp
    +  .cfi_def_cfa_offset 16
    +  movl $6,%esi
    +  movl $A,%edi
    +  call sum_array
    +  movl %.LC0,%esi
    +  movl %eax,%eax
    +  movl $1,%edi
    +  xorl %eax,%eax
    +  addq $8,%rsp
    +  .cfi_def_cfa_offset 8
    +  jmp __printf_chk
    +...
    +A:
    +  .long 12 # or .long 12,34,56,78,-90,1
    +  .long 34
    +  .long 56
    +  .long 78
    +  .long -90
    +  .long 1
    +  .ident "GCC: (Ubuntu 7.3.0-21ubuntu1-16.04) 7.3.0"
    +  .section .note.GNU-stack,"",@progbits
    +
    + +

    Data stored on Stack – Example 1

    + +

    This material is optional –> It is for your learning pleasure!

    + +

    C:

    + +
    void main(int argc, char* argv){
    +  int A[] = {12,34,56,78,-90,1}; // 12 and 34 are highlighted.
    +  printf("The total is %d.\n", sum_array(A,N));
    +  return;
    +}
    +
    + +

    Assembly:

    + +
    main:
    +.LFB38:
    +  .cfi_startproc
    +  subq $40,%rsp
    +  .cfi_def_cfa_offset 48
    +  movl $6,%esi
    +  movq %fs:40,%rax
    +  movq %rax,24(%rsp)
    +  xorl %eax,%eax
    +  movabsq $146028888076,%rax # highloghted
    +  movq %rsp,%rdi
    +  movq %rax,(%rsp)
    +  movabsq $335007449144,%rax # highlighted
    +  movq %rax,8(%rsp)
    +  movabsq $8589934502,%rax # highlighted
    +  movq %rax,16(%rsp)
    +  call sum_array
    +  movl $.LC0,%esi
    +  movl %eax,%edx
    +  movl $1,%edi
    +  xorl %eax,%eax
    +  call __printf_chk
    +  movq 24(%rsp), %rax
    +  xorq %fs:40,%rax
    +  jne .L5
    +  addq $40,%rsp
    +  .cfi_remember_state
    +  .cfi_def_cfa_offset 8
    +  ret
    +
    + +

    How does this large # end up representing 12 and 34:

    + +
      +
    • Express $146028888076 in binary
    • +
    • Transform binary to hex => 0x000000220000000c
    • +
    • Read hex’s LSB (32 bits) (0000000c) as a decimal +=> 12
    • +
    • Read hex’s MSB (32 bits) (00000022) as a decimal +=> 34
    • +
    • Repeat for other 2 operands of movabsq +instructions
    • +
    + +

    Summary - 1

    + +
      +
    • What is a buffer overflow +
        +
      • When function writes more data in array than array can hold on stack
      • +
      • Effect: data kept on the stack (value of other local variables and registers, +return address) may be corrupted +-> Stack smashing
      • +
      +
    • +
    • Why buffer overflow spells trouble -> it creates vulnerability +
        +
      • Allowing hacker attacks
      • +
      +
    • +
    • How to protect system against such attacks +
        +
      1. Avoid creating overflow vulnerabilities in the code that we write +
          +
        • By always checking bounds and calling “safe” library functions that +consider size of array
        • +
        +
      2. +
      3. Employ system-level protections +
          +
        • Randomized initial stack pointer and non-executable code segments
        • +
        +
      4. +
      5. Use compiler (like gcc) security features: +
          +
        • Stack “canary” value and endbr64 instruction
        • +
        +
      6. +
      +
    • +
    + +

    Summary - 2

    + +
      +
    • Floating point data and operations +
        +
      • Data held and manipulated in XMM registers
      • +
      • Assembly language instructions similar to integer +assembly language instructions we have seen so far
      • +
      +
    • +
    + +

    Next Lecture

    + +

    Start a new unit …

    +
      +
    • Instruction Set Architecture (ISA)
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/22/Lecture_22_Machine_Level_Prog_Buffer_Overflow_and_Fl_Pt_Annotated.pdf b/_site/melody/cmpt-295/22/Lecture_22_Machine_Level_Prog_Buffer_Overflow_and_Fl_Pt_Annotated.pdf new file mode 100644 index 0000000..1cbaae9 Binary files /dev/null and b/_site/melody/cmpt-295/22/Lecture_22_Machine_Level_Prog_Buffer_Overflow_and_Fl_Pt_Annotated.pdf differ diff --git a/_site/melody/cmpt-295/23/23.html b/_site/melody/cmpt-295/23/23.html new file mode 100644 index 0000000..19087fe --- /dev/null +++ b/_site/melody/cmpt-295/23/23.html @@ -0,0 +1,727 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 - Unit - Instruction Set Architecture

    + +

    Lecture 23

    + +
      +
    • Introduction to Instruction Set Architecture (ISA)
    • +
    • ISA Design + MIPS (MIPS is crossed out)
    • +
    + +

    Last Lecture - 1

    + +
      +
    • What is a buffer overflow +
        +
      • When function writes more data in array than array can hold on stack
      • +
      • Effect: data kept on the stack (value of other local variables and registers, +return address) may be corrupted +-> Stack smashing
      • +
      +
    • +
    • Why buffer overflow spells trouble -> it creates vulnerability +
        +
      • Allowing hacker attacks
      • +
      +
    • +
    • How to protect system against such attacks +
        +
      1. (s/w developer) Avoid creating overflow vulnerabilities in the code that we write +
          +
        • By always checking bounds and calling “safe” library functions that +consider size of array
        • +
        +
      2. +
      3. (system) Employ system-level protections +
          +
        • Randomized initial stack pointer and non-executable code segments
        • +
        +
      4. +
      5. (compiler) Use compiler (like gcc) security features: +
          +
        • Stack “canary” value and endbr64 instruction
        • +
        +
      6. +
      +
    • +
    + +

    Last Lecture - 2

    + +

    Brief look at:

    + +
      +
    • Floating point data and operations +
        +
      • Data held and manipulated in XMM registers
      • +
      • Assembly language instructions similar to integer assembly +language instructions we have seen so far
      • +
      +
    • +
    • Optional: Storing Data in Various Segments of Memory +
        +
      • Global variables => data segment
      • +
      • Local variables => stack segment
      • +
      • How their values are represented in an assembly program
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • (highlighted) Instruction Set Architecture (ISA) +
        +
      • (highlighted) Definition of ISA
      • +
      +
    • +
    • (highlighted) Instruction Set design +
        +
      • (highlighted) Design guidelines
      • +
      • Example of an instruction set: MIPS
      • +
      • Create our own instruction sets
      • +
      • ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +

    Reference

    + +
      +
    • Computer Organization and Design, 5th Edition, +by David A. Patterson and John L. Hennessy +
        +
      • See Resources for a link to an online version
      • +
      • Chapter 2 – Instructions: Language of the Computer
      • +
      • Chapter 4 – The processor
      • +
      +
    • +
    • Chapter 4 of our textbook ? +
        +
      • will not make use of this chapter very much!
      • +
      +
    • +
    + +

    The Big Picture – Above the hood

    + +
      +
    • C code: +
        +
      • C Program (.c) -> sum_store.c
      • +
      • C Preprocessor creates: Preprocessed Source -> sum_store.i
      • +
      +
    • +
    • Assembly code: +
        +
      • C compiler creates: Assembly program (.s) -> sum_store.s
      • +
      +
    • +
    • Machine code: +
        +
      • Linker grates: Object (.o) -> sum_store.o
      • +
      +
    • +
    • ISA - Instruction Set Architecture: An agreement establishing how software communicates with CPU. +
        +
      • Loader creates: Executable -> ss
      • +
      • Computer executed it +
          +
        • CPU
        • +
        • Memory
        • +
        +
      • +
      +
    • +
    + +

    C code:

    + +
    short abs(short aNumber){
    +  short result=0;
    +  if(aNumber>0) result=aNumber;
    +  else result=-aNumber;
    +  return result;
    +}
    +
    + +

    Assembly code:

    + +
      .global abs
    +abs:
    +  movl %edi,%eax
    +  testw %di,%di
    +  jle .L3
    +.L2:
    +  ret
    +.L3:
    +  negl %eax
    +  jmp .L2
    +
    + +

    Machine code:

    + +
    1111100010001001
    +111111111000010101100110
    +0000001001111110
    +1100001111110011
    +1101100011110111
    +1111101011101011
    +
    + +

    The Big Picture - Under the hood!

    + +

    Wikipedia says: A datapath is a collection +of functional units +such as:

    + +
      +
    • ALU (perform data +processing operations),
    • +
    • registers, and
    • +
    • buses (allow data +to flow between them).
    • +
    + +

    Along with the control +unit, the datapath +composes the +central processing unit +(CPU/microprocessor).

    + +

    Microprocessor datapath

    + +

    Machine code below is stored in Instruction Memory:

    + +
    1111100010001001
    +111111111000010101100110
    +0000001001111110
    +1100001111110011
    +1101100011110111
    +1111101011101011
    +
    + +

    Instruction Set Architecture (ISA)

    + +
      +
    • Instruction set architecture (ISA): defines the machine +code (i.e., instruction set) that a microprocessor reads +and acts upon as well as the memory model. (Adapted from https://en.wikipedia.org/wiki/Computer_architecture#History)
    • +
    • Instruction Set: it is all the commands understood by a +given computer architecture. Source: Computer Organization and Design, 5th Edition, by David A. +Patterson and John L. Hennessy
    • +
    • We say that a microprocessor implements an ISA.
    • +
    + +

    Instruction Set Architecture (ISA)

    + +

    An ISA is a formal specification of …

    + +
      +
    • Memory and Registers +
        +
      • Memory +
          +
        • Word size
        • +
        • Memory size -> 2m x n +
            +
          • 2m distinct addressable locations in memory
          • +
          • each of these addressable locations has n bits
          • +
          +
        • +
        +
      • +
      +
    • +
    • Registers +
        +
      • Number
      • +
      • Size
      • +
      • Data type
      • +
      • Purpose
      • +
      +
    • +
    • Instruction Set +(First three sub-items have a label: of assembly instructions +and their corresponding +machine instructions) +
        +
      • Format
      • +
      • Syntax
      • +
      • Description (semantic)
      • +
      • Operand model: number, order and meaning of operands
      • +
      • Memory addressing modes
      • +
      +
    • +
    + +

    Instruction Set Architecture (ISA) cont’d

    + +

    An ISA is a formal specification of … (cont’d)

    + +
      +
    • Conventions +
        +
      • How control flow and data are passed during function calls
      • +
      • How registers are preserved during function calls +
          +
        • Any callee and caller saved registers?
        • +
        +
      • +
      +
    • +
    • Model of computation - sequential +
        +
      • Microprocessor executes our C program in such a way that it +produces the expected result +
          +
        • We get the illusion that the microprocessor executes each C +statement sequentially. +Annotation 1: Through the fetch-decode execvute bop & PC++ to next instructions. +Annotation 2: but as we shall see in our next unit (Chapter 5) this is not what actually happens at the CPU level.
        • +
        +
      • +
      +
    • +
    + +

    Memory Models:

    + +

    Address resolution is the smallest addressable memory “chunk”.

    + +

    Model 1 – word-addressable computer

    + +

    Memory layout with 232×162^{32} \times 16 bits; 16 is the “address resolution”:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressValue (width = address resolution)
    23212^{32}-1 
     
    2 
    1 
    0 
    + +

    Example of word-addressable ISAs:

    + +
      +
    • Data General Nova mini-computer
    • +
    • Texas Instruments TMS9900
    • +
    • National Semiconductors IMP-16
    • +
    + +

    In this model, n = wordsize; wordsize is 16 bits.

    + +

    Model 2 – byte-addressable computer

    + +

    Memory layout with 232×82^{32} \times 8 bits. 8 is the address resolution.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressValue (8 bits in width)
    23212^{32}-1 a.k.a 4294967232 
     
    2 
    1 
    0 
    + +

    Compressed View of Memory:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Address+1+2+3+4+5+6+7
    23212^{32-1} a.k.a. 4294967295       
           
    16       
    8       
    0       
    + +

    In this model nwordsizen \neq \text{wordsize}; wordsize = 64 bits.

    + +

    Examples of byte-addressable ISA:

    + +
      +
    • Intel Core i7 and i9 (Transcriber’s note: this is not an ISA, these are processor lines that implement the x86-64 ISA)
    • +
    • AMD64 Epyc (Transcriber’s note: AMD64 is an alternate name for x86-64, Epyc is a lineup of server processors from AMD)
    • +
    • MIPS64
    • +
    • RISC-V
    • +
    + +

    Model 3

    + +

    Target machine:

    + +
      +
    • m=64m=64 (however only 48 bits are used)
    • +
    • + n=8 bitsn=8 \text{ bits} +
    • +
    + +

    Example of an ISA: x86

    + +
      +
    • Memory model +
        +
      • Word size: 64 bits
      • +
      • Memory size -> 2m x n +
          +
        • m = 64 bits even though only 48 bits are used
        • +
        • n = 8 bits (byte-addressable)
        • +
        +
      • +
      +
    • +
    • Registers +
        +
      • 16 integers registers of 64 bits (8/16/32/64 bits can be accessed) +
          +
        • Purpose: stack pointer, return value, callee-saved, caller-saved, +arguments
        • +
        +
      • +
      • 16 floating point registers of 128 bits
      • +
      +
    • +
    • Instruction set +
        +
      • Lots of them: https://en.wikipedia.org/wiki/X86_instruction_listings
      • +
      • Operand model: two operands (of different sizes)
      • +
      • Memory addressing modes: Supports various addressing modes including +immediate (direct), indirect, base+displacement, indexed, and scaled
      • +
      +
    • +
    + +

    Instruction set (IS) design guidelines

    + +
      +
    1. Each instruction of IS must have an unambiguous binary +encoding, so CPU can unambiguously decode and +execute it -> let’s assign a unique opcode to each instruction
    2. +
    3. IS is functionally complete -> i.e., it is “Turing complete”; it will have 3 classes of instructions: +
        +
      1. Data transfer instructions – Memory reference
      2. +
      3. Data manipulation instructions – Arithmetic and logical
      4. +
      5. Program control instructions – Branch and jump
      6. +
      +
    4. +
    5. In terms of machine instruction format: +
        +
      1. Create as few of them as possible
      2. +
      3. Have them all of the same length and same format!
      4. +
      5. If we have different machine instruction formats, then position the +fields that have the same purpose in the same location in the format
      6. +
      +
    6. +
    + +

    1. “Each instruction of IS must have an unambiguous binary encoding …”

    + +

    Assembly instruction:

    + +
      +
    • Symbolic representation of a +machine instruction +
        +
      • Mnemonics: abbreviation of +operation name +
          +
        • Example: movq, addw, ret
        • +
        +
      • +
      • Labels to represent addresses +
          +
        • Example: call sum, jmp loop
        • +
        +
      • +
      +
    • +
    • Advantage: human readable, +i.e., program easier to read and +write than a series of 0’s and 1’s, Example
    • +
    • Made easier through the use of of format opcode +mnemonics and labels
    • +
    + +

    An assembly instruction will compile (more specifically, “assemble”) into a:

    + +

    Machine Instrucction:

    + +
      +
    • Each assembly instruction has a +corresponding machine instruction
    • +
    • Machine instruction expressed as +bit pattern (binary encoding) +–> 0’s and 1’s
    • +
    • unique +bit pattern +representing +each opcode
    • +
    • unique +bit pattern +representing +each operand
    • +
    • This is an example of formal binary encoding
    • +
    + +

    What is an opcode? What is an operand?

    + +
      +
    • Opcode: Operation Code
    • +
    • Opcode: operation that can be executed by the CPU +
        +
      • Expressed as bit pattern (binary encoding) –> 0’s and 1’s
      • +
      +
    • +
    • Operand(s): required by the opcode in order for CPU to +successfully carry out the instruction +
        +
      • They are also expressed as bit patterns –> 0’s and 1’s
      • +
      +
    • +
    • In the output of the objdump tool (disassembler), we can see +opcodes and operands expressed as hexadecimal values
    • +
    + +

    Example using x86-64:

    + +

    Mixed assembly and hex machine instructions:

    + +
    00000000004004b7 <someFcn>:
    +  4004b7:   4c 03 00  add (%rax),%r8
    +  4004ba:   7e fb     jle 4004b7 <someFcn>
    +  4004bc:   c3        retq
    +
    + +

    Binary machine instructions:

    + +
    000000000000001101001100
    +1111101101111110
    +11000011
    +
    + + diff. length\therefore \text{ diff. length} + +

    Types of instruction sets

    + +

    CISC:

    + +
      +
    • Stands for: Complex Instruction Set Computing
    • +
    • Large # of instructions +including special purpose +instructions
    • +
    • Usually “register-memory” +architecture +
        +
      • This means any instruction may address memory e.g. addq 8(%rsp) %rax
      • +
      +
    • +
    • Examples: VAX, x86, MC68000
    • +
    + +

    RISC:

    + +
      +
    • Reduced Instruction Set +Computing
    • +
    • Small # of general purpose +instructions +
        +
      • smaller machine instruction set
      • +
      • simpler microprocessor design
      • +
      +
    • +
    • “load/store” architecture +
        +
      • This means load & store are the only instructions to access memory
      • +
      +
    • +
    • Examples: SPARC, MIPS, Alpha, AXP, PowerPC (Transcriber’s note: Also, ARM, like the Raspberry Pi, PinebookPro and M1 chips)
    • +
    + +

    Summary

    + +
      +
    • Assembler (part of the compilation process): +
        +
      • Transforms assembly code (movl %edi,%eax) into machine code +(0xf889 -> 1111100010001001)
      • +
      +
    • +
    • Instruction Set Architecture (ISA) +
        +
      • A formal specification (or agreement) of:
      • +
      • Registers and memory model, set of instructions (assembly-machine)
      • +
      • Conventions, model of computation
      • +
      • etc…
      • +
      +
    • +
    • Design principles when creating instruction set (IS) +
        +
      1. Each instruction must have an unambiguous encoding
      2. +
      3. Functionally complete (Turing complete)
      4. +
      5. Machine instruction format: 1) as few of them as possible 2) of the +same length 3) fields that have the same purpose positioned in the +same location in the format
      6. +
      +
    • +
    • Types of instruction sets: CISC and RISC
    • +
    + +

    Next lecture

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design guidelines
      • +
      • (highlighted) Example of an instruction set: MIPS
      • +
      • Create our own instruction sets
      • +
      • ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/23/23/index.html b/_site/melody/cmpt-295/23/23/index.html new file mode 100644 index 0000000..19087fe --- /dev/null +++ b/_site/melody/cmpt-295/23/23/index.html @@ -0,0 +1,727 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 - Unit - Instruction Set Architecture

    + +

    Lecture 23

    + +
      +
    • Introduction to Instruction Set Architecture (ISA)
    • +
    • ISA Design + MIPS (MIPS is crossed out)
    • +
    + +

    Last Lecture - 1

    + +
      +
    • What is a buffer overflow +
        +
      • When function writes more data in array than array can hold on stack
      • +
      • Effect: data kept on the stack (value of other local variables and registers, +return address) may be corrupted +-> Stack smashing
      • +
      +
    • +
    • Why buffer overflow spells trouble -> it creates vulnerability +
        +
      • Allowing hacker attacks
      • +
      +
    • +
    • How to protect system against such attacks +
        +
      1. (s/w developer) Avoid creating overflow vulnerabilities in the code that we write +
          +
        • By always checking bounds and calling “safe” library functions that +consider size of array
        • +
        +
      2. +
      3. (system) Employ system-level protections +
          +
        • Randomized initial stack pointer and non-executable code segments
        • +
        +
      4. +
      5. (compiler) Use compiler (like gcc) security features: +
          +
        • Stack “canary” value and endbr64 instruction
        • +
        +
      6. +
      +
    • +
    + +

    Last Lecture - 2

    + +

    Brief look at:

    + +
      +
    • Floating point data and operations +
        +
      • Data held and manipulated in XMM registers
      • +
      • Assembly language instructions similar to integer assembly +language instructions we have seen so far
      • +
      +
    • +
    • Optional: Storing Data in Various Segments of Memory +
        +
      • Global variables => data segment
      • +
      • Local variables => stack segment
      • +
      • How their values are represented in an assembly program
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • (highlighted) Instruction Set Architecture (ISA) +
        +
      • (highlighted) Definition of ISA
      • +
      +
    • +
    • (highlighted) Instruction Set design +
        +
      • (highlighted) Design guidelines
      • +
      • Example of an instruction set: MIPS
      • +
      • Create our own instruction sets
      • +
      • ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +

    Reference

    + +
      +
    • Computer Organization and Design, 5th Edition, +by David A. Patterson and John L. Hennessy +
        +
      • See Resources for a link to an online version
      • +
      • Chapter 2 – Instructions: Language of the Computer
      • +
      • Chapter 4 – The processor
      • +
      +
    • +
    • Chapter 4 of our textbook ? +
        +
      • will not make use of this chapter very much!
      • +
      +
    • +
    + +

    The Big Picture – Above the hood

    + +
      +
    • C code: +
        +
      • C Program (.c) -> sum_store.c
      • +
      • C Preprocessor creates: Preprocessed Source -> sum_store.i
      • +
      +
    • +
    • Assembly code: +
        +
      • C compiler creates: Assembly program (.s) -> sum_store.s
      • +
      +
    • +
    • Machine code: +
        +
      • Linker grates: Object (.o) -> sum_store.o
      • +
      +
    • +
    • ISA - Instruction Set Architecture: An agreement establishing how software communicates with CPU. +
        +
      • Loader creates: Executable -> ss
      • +
      • Computer executed it +
          +
        • CPU
        • +
        • Memory
        • +
        +
      • +
      +
    • +
    + +

    C code:

    + +
    short abs(short aNumber){
    +  short result=0;
    +  if(aNumber>0) result=aNumber;
    +  else result=-aNumber;
    +  return result;
    +}
    +
    + +

    Assembly code:

    + +
      .global abs
    +abs:
    +  movl %edi,%eax
    +  testw %di,%di
    +  jle .L3
    +.L2:
    +  ret
    +.L3:
    +  negl %eax
    +  jmp .L2
    +
    + +

    Machine code:

    + +
    1111100010001001
    +111111111000010101100110
    +0000001001111110
    +1100001111110011
    +1101100011110111
    +1111101011101011
    +
    + +

    The Big Picture - Under the hood!

    + +

    Wikipedia says: A datapath is a collection +of functional units +such as:

    + +
      +
    • ALU (perform data +processing operations),
    • +
    • registers, and
    • +
    • buses (allow data +to flow between them).
    • +
    + +

    Along with the control +unit, the datapath +composes the +central processing unit +(CPU/microprocessor).

    + +

    Microprocessor datapath

    + +

    Machine code below is stored in Instruction Memory:

    + +
    1111100010001001
    +111111111000010101100110
    +0000001001111110
    +1100001111110011
    +1101100011110111
    +1111101011101011
    +
    + +

    Instruction Set Architecture (ISA)

    + +
      +
    • Instruction set architecture (ISA): defines the machine +code (i.e., instruction set) that a microprocessor reads +and acts upon as well as the memory model. (Adapted from https://en.wikipedia.org/wiki/Computer_architecture#History)
    • +
    • Instruction Set: it is all the commands understood by a +given computer architecture. Source: Computer Organization and Design, 5th Edition, by David A. +Patterson and John L. Hennessy
    • +
    • We say that a microprocessor implements an ISA.
    • +
    + +

    Instruction Set Architecture (ISA)

    + +

    An ISA is a formal specification of …

    + +
      +
    • Memory and Registers +
        +
      • Memory +
          +
        • Word size
        • +
        • Memory size -> 2m x n +
            +
          • 2m distinct addressable locations in memory
          • +
          • each of these addressable locations has n bits
          • +
          +
        • +
        +
      • +
      +
    • +
    • Registers +
        +
      • Number
      • +
      • Size
      • +
      • Data type
      • +
      • Purpose
      • +
      +
    • +
    • Instruction Set +(First three sub-items have a label: of assembly instructions +and their corresponding +machine instructions) +
        +
      • Format
      • +
      • Syntax
      • +
      • Description (semantic)
      • +
      • Operand model: number, order and meaning of operands
      • +
      • Memory addressing modes
      • +
      +
    • +
    + +

    Instruction Set Architecture (ISA) cont’d

    + +

    An ISA is a formal specification of … (cont’d)

    + +
      +
    • Conventions +
        +
      • How control flow and data are passed during function calls
      • +
      • How registers are preserved during function calls +
          +
        • Any callee and caller saved registers?
        • +
        +
      • +
      +
    • +
    • Model of computation - sequential +
        +
      • Microprocessor executes our C program in such a way that it +produces the expected result +
          +
        • We get the illusion that the microprocessor executes each C +statement sequentially. +Annotation 1: Through the fetch-decode execvute bop & PC++ to next instructions. +Annotation 2: but as we shall see in our next unit (Chapter 5) this is not what actually happens at the CPU level.
        • +
        +
      • +
      +
    • +
    + +

    Memory Models:

    + +

    Address resolution is the smallest addressable memory “chunk”.

    + +

    Model 1 – word-addressable computer

    + +

    Memory layout with 232×162^{32} \times 16 bits; 16 is the “address resolution”:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressValue (width = address resolution)
    23212^{32}-1 
     
    2 
    1 
    0 
    + +

    Example of word-addressable ISAs:

    + +
      +
    • Data General Nova mini-computer
    • +
    • Texas Instruments TMS9900
    • +
    • National Semiconductors IMP-16
    • +
    + +

    In this model, n = wordsize; wordsize is 16 bits.

    + +

    Model 2 – byte-addressable computer

    + +

    Memory layout with 232×82^{32} \times 8 bits. 8 is the address resolution.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressValue (8 bits in width)
    23212^{32}-1 a.k.a 4294967232 
     
    2 
    1 
    0 
    + +

    Compressed View of Memory:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Address+1+2+3+4+5+6+7
    23212^{32-1} a.k.a. 4294967295       
           
    16       
    8       
    0       
    + +

    In this model nwordsizen \neq \text{wordsize}; wordsize = 64 bits.

    + +

    Examples of byte-addressable ISA:

    + +
      +
    • Intel Core i7 and i9 (Transcriber’s note: this is not an ISA, these are processor lines that implement the x86-64 ISA)
    • +
    • AMD64 Epyc (Transcriber’s note: AMD64 is an alternate name for x86-64, Epyc is a lineup of server processors from AMD)
    • +
    • MIPS64
    • +
    • RISC-V
    • +
    + +

    Model 3

    + +

    Target machine:

    + +
      +
    • m=64m=64 (however only 48 bits are used)
    • +
    • + n=8 bitsn=8 \text{ bits} +
    • +
    + +

    Example of an ISA: x86

    + +
      +
    • Memory model +
        +
      • Word size: 64 bits
      • +
      • Memory size -> 2m x n +
          +
        • m = 64 bits even though only 48 bits are used
        • +
        • n = 8 bits (byte-addressable)
        • +
        +
      • +
      +
    • +
    • Registers +
        +
      • 16 integers registers of 64 bits (8/16/32/64 bits can be accessed) +
          +
        • Purpose: stack pointer, return value, callee-saved, caller-saved, +arguments
        • +
        +
      • +
      • 16 floating point registers of 128 bits
      • +
      +
    • +
    • Instruction set +
        +
      • Lots of them: https://en.wikipedia.org/wiki/X86_instruction_listings
      • +
      • Operand model: two operands (of different sizes)
      • +
      • Memory addressing modes: Supports various addressing modes including +immediate (direct), indirect, base+displacement, indexed, and scaled
      • +
      +
    • +
    + +

    Instruction set (IS) design guidelines

    + +
      +
    1. Each instruction of IS must have an unambiguous binary +encoding, so CPU can unambiguously decode and +execute it -> let’s assign a unique opcode to each instruction
    2. +
    3. IS is functionally complete -> i.e., it is “Turing complete”; it will have 3 classes of instructions: +
        +
      1. Data transfer instructions – Memory reference
      2. +
      3. Data manipulation instructions – Arithmetic and logical
      4. +
      5. Program control instructions – Branch and jump
      6. +
      +
    4. +
    5. In terms of machine instruction format: +
        +
      1. Create as few of them as possible
      2. +
      3. Have them all of the same length and same format!
      4. +
      5. If we have different machine instruction formats, then position the +fields that have the same purpose in the same location in the format
      6. +
      +
    6. +
    + +

    1. “Each instruction of IS must have an unambiguous binary encoding …”

    + +

    Assembly instruction:

    + +
      +
    • Symbolic representation of a +machine instruction +
        +
      • Mnemonics: abbreviation of +operation name +
          +
        • Example: movq, addw, ret
        • +
        +
      • +
      • Labels to represent addresses +
          +
        • Example: call sum, jmp loop
        • +
        +
      • +
      +
    • +
    • Advantage: human readable, +i.e., program easier to read and +write than a series of 0’s and 1’s, Example
    • +
    • Made easier through the use of of format opcode +mnemonics and labels
    • +
    + +

    An assembly instruction will compile (more specifically, “assemble”) into a:

    + +

    Machine Instrucction:

    + +
      +
    • Each assembly instruction has a +corresponding machine instruction
    • +
    • Machine instruction expressed as +bit pattern (binary encoding) +–> 0’s and 1’s
    • +
    • unique +bit pattern +representing +each opcode
    • +
    • unique +bit pattern +representing +each operand
    • +
    • This is an example of formal binary encoding
    • +
    + +

    What is an opcode? What is an operand?

    + +
      +
    • Opcode: Operation Code
    • +
    • Opcode: operation that can be executed by the CPU +
        +
      • Expressed as bit pattern (binary encoding) –> 0’s and 1’s
      • +
      +
    • +
    • Operand(s): required by the opcode in order for CPU to +successfully carry out the instruction +
        +
      • They are also expressed as bit patterns –> 0’s and 1’s
      • +
      +
    • +
    • In the output of the objdump tool (disassembler), we can see +opcodes and operands expressed as hexadecimal values
    • +
    + +

    Example using x86-64:

    + +

    Mixed assembly and hex machine instructions:

    + +
    00000000004004b7 <someFcn>:
    +  4004b7:   4c 03 00  add (%rax),%r8
    +  4004ba:   7e fb     jle 4004b7 <someFcn>
    +  4004bc:   c3        retq
    +
    + +

    Binary machine instructions:

    + +
    000000000000001101001100
    +1111101101111110
    +11000011
    +
    + + diff. length\therefore \text{ diff. length} + +

    Types of instruction sets

    + +

    CISC:

    + +
      +
    • Stands for: Complex Instruction Set Computing
    • +
    • Large # of instructions +including special purpose +instructions
    • +
    • Usually “register-memory” +architecture +
        +
      • This means any instruction may address memory e.g. addq 8(%rsp) %rax
      • +
      +
    • +
    • Examples: VAX, x86, MC68000
    • +
    + +

    RISC:

    + +
      +
    • Reduced Instruction Set +Computing
    • +
    • Small # of general purpose +instructions +
        +
      • smaller machine instruction set
      • +
      • simpler microprocessor design
      • +
      +
    • +
    • “load/store” architecture +
        +
      • This means load & store are the only instructions to access memory
      • +
      +
    • +
    • Examples: SPARC, MIPS, Alpha, AXP, PowerPC (Transcriber’s note: Also, ARM, like the Raspberry Pi, PinebookPro and M1 chips)
    • +
    + +

    Summary

    + +
      +
    • Assembler (part of the compilation process): +
        +
      • Transforms assembly code (movl %edi,%eax) into machine code +(0xf889 -> 1111100010001001)
      • +
      +
    • +
    • Instruction Set Architecture (ISA) +
        +
      • A formal specification (or agreement) of:
      • +
      • Registers and memory model, set of instructions (assembly-machine)
      • +
      • Conventions, model of computation
      • +
      • etc…
      • +
      +
    • +
    • Design principles when creating instruction set (IS) +
        +
      1. Each instruction must have an unambiguous encoding
      2. +
      3. Functionally complete (Turing complete)
      4. +
      5. Machine instruction format: 1) as few of them as possible 2) of the +same length 3) fields that have the same purpose positioned in the +same location in the format
      6. +
      +
    • +
    • Types of instruction sets: CISC and RISC
    • +
    + +

    Next lecture

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design guidelines
      • +
      • (highlighted) Example of an instruction set: MIPS
      • +
      • Create our own instruction sets
      • +
      • ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/23/Lecture_23_ISA_Intro_and_Design_Annotated.pdf b/_site/melody/cmpt-295/23/Lecture_23_ISA_Intro_and_Design_Annotated.pdf new file mode 100644 index 0000000..39ccea8 Binary files /dev/null and b/_site/melody/cmpt-295/23/Lecture_23_ISA_Intro_and_Design_Annotated.pdf differ diff --git a/_site/melody/cmpt-295/23/micro_graph.html b/_site/melody/cmpt-295/23/micro_graph.html new file mode 100644 index 0000000..3696a3b --- /dev/null +++ b/_site/melody/cmpt-295/23/micro_graph.html @@ -0,0 +1,551 @@ + + + + + + +

    + Graph +

    +

    + The ideal solution to this would actually be to turn the graph into a 3D braille model so it can be printed. Due to conditions of remoteness and the time required to do such a task though, this is not possible right now. +

    +

    + Please do not navigate to connections manually. Use the links. +

    +

    + Nodes of Graph +

    + +

    + Graph Connections (to) +

    + +

    + Graph Connections (form) +

    + + + diff --git a/_site/melody/cmpt-295/24/24-annotated.html b/_site/melody/cmpt-295/24/24-annotated.html new file mode 100644 index 0000000..c81b172 --- /dev/null +++ b/_site/melody/cmpt-295/24/24-annotated.html @@ -0,0 +1,885 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 - Unit - Instruction Set Architecture

    + +

    I do not like computer jokes…not one bit!

    + +

    Lecture 24:

    + +
      +
    • MIPS
    • +
    + +

    Last Lecture

    + +
      +
    • Assembler (part of the compilation process): +
        +
      • Transforms assembly code (movl %edi,%eax) into machine code +(0xf889 -> 1111100010001001)
      • +
      +
    • +
    • Instruction Set Architecture (ISA) [Annotation: …is an abstract model of a computer …is an interface between software (and s/w developers) and hardware (hardware designers) +A formal specification (or agreement) of: +
        +
      • Registers and memory model, set of instructions (assembly-machine) [Annotation: main memory]
      • +
      • Conventions, model of computation [Annotation: data types, memory addressing modes]
      • +
      • etc…
      • +
      +
    • +
    • Design principles when creating instruction set (IS) +
        +
      1. Each instruction must have an unambiguous encoding
      2. +
      3. Functionally complete (Turing complete)
      4. +
      5. Machine instruction format: 1) as few of them as possible 2) of the +same length 3) fields that have the same purpose positioned in the +same location in the format
      6. +
      +
    • +
    • Types of instruction sets: CISC and RISC
    • +
    + +

    [Annotation: A realization of an ISA is called an implementation. An ISA permits multiple implementations.]

    + +

    Today’s Menu

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design guidelines
      • +
      • (highlighted) Example of an instruction set: MIPS
      • +
      • Create our own instruction sets
      • +
      • ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +

    Example of another ISA: MIPS

    + +

    [Annotation: MIPS = microprocessor without interlocked pipelined stages.]

    + +

    Example of an ISA: MIPS

    + +
      +
    • Registers and Memory model +
        +
      • # of registers -> 32 registers (each register is 32 bit wide) See Figure on next Slide [Annotation: 1]
      • +
      • Word size -> 32 bits [Annotation: 3]
      • +
      • Memory size -> 2m×n=232×8 bits2^{m} \times n = 2^{32} \times 8 \text{ bits} [Annotation: 5]
      • +
      • Byte-addressable memory +so address resolution -> n = 8 bits [Annotation: 4]
      • +
      • Size of memory address (# of bits) -> 32 bits; m = 32 [Annotation: 2]
      • +
      +
    • +
    • So, there are 2322^{32} distinct addressable memory “chunks” +(or “locations”) and each of these addressable memory +“chunks” (or “locations”) has 8 bits See Figure on next next Slide [Annotation: 6]
    • +
    + +

    FIGURE A.6.1 MIPS registers and usage conventions.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register nameNumberUsage
    $zero0constant 0
    $at1reserved for assembler
    $v02expression evaluation and results of a function
    $v13expression evaluation and results of a function
    $a04argument 1
    $a15argument 2
    $a26argument 3
    $a37argument 4
    $t08temporary (not preserved across call)
    $t19temporary (not preserved across call)
    $t210temporary (not preserved across call)
    $t311temporary (not preserved across call)
    $t512temporary (not preserved across call)
    $t613temporary (not preserved across call)
    $t714temporary (not preserved across call)
    $s015saved temporary (preserved across call)
    $s116saved temporary (preserved across call)
    $s217saved temporary (preserved across call)
    $s318saved temporary (preserved across call)
    $s419saved temporary (preserved across call)
    $s520saved temporary (preserved across call)
    $s621saved temporary (preserved across call)
    $s722saved temporary (preserved across call)
    $t824temporary (not preserved across call)
    $t925temporary (not preserved across call)
    $k026reserved for OS kernel
    $k127reserved for OS kernel
    $gp28pointer to global area
    $sp29stack pointer
    $fp30frame pointer
    $ra31return address (used by function call)
    + +

    Source: Page A-24 in Patterson and Hennessy

    + +

    MIPS Memory Model

    + +

    FIGURE 2.13 – The MIPS memory allocation for program and data

    + +

    These addresses are only a software convention, +and not part of the MIPS architecture. +The stack pointer is initialized to 7fff fffchex\text{7fff fffc}_{\text{hex}} and grows down toward the segment. +At the other end, the program code (“text”) starts at 0040 0000hex\text{0040 0000}_{\text{hex}}. +The static data starts at 1000 0000hex\text{1000 0000}_{\text{hex}}. +Dynamic data, allocated by malloc in C and by new in Java, is next. +It grows up toward the stack and in an area called the heap. +The global pointer: $gp, is set to an address to make it easy to access data. +It is initialized to 1000 8000hex\text{1000 8000}_{\text{hex}} +so it can access from 1000 000hex\text{1000 000}_{\text{hex}} to 1000 ffffhex\text{1000 ffff}_{\text{hex}} using the positive and negative 16-bit offsets from $gp. +The information is also found in Column 4 of the MIPS Reference Data Card at the front of the book.

    + +

    Source: Page 104 in Patterson and Hennessy

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressLabel
    (top) $sp -> 7fff ffffhex\text{7fff ffff}_{\text{hex}}Stack (arrow pointing down)
     Dynamic data (arrow pointing up) [Annotation: heap]
    (between Static Data and Text) $gp -> 1000 8000hex\text{1000 8000}_{\text{hex}}, 1000 0000hex\text{1000 0000}_{\text{hex}}Static Data
    (bottom) $pc -> 0040 0000hex\text{0040 0000}_{\text{hex}}Text
    (bottom) 0Reserved
    + +

    Example of an ISA: MIPS

    + +
      +
    • Instruction set +
        +
      • MIPS assembly language notation +
          +
        • add a, b, c, Meaning: a = b+c
        • +
        +
      • +
      • 3 operand assembly language +
          +
        • Requiring all instructions to have 3 operands would +keep the design of the microprocessor hardware +simple
        • +
        • Hardware for a variable number of operands is +more complicated
        • +
        +
      • +
      +
    • +
    + +

    Activity

    + +
      +
    • +

      If we want to write an assembly program that sums +four variables b, c , d and e, how many MIPS add +instructions would we need?

      +
    • +
    • +

      Solution:

      +
    • +
    + +
    add a,b,c # a = b+c
    +add a,a,d # a = b+c+d
    +add a,a,e # a = b+c+d+e
    +
    + +

    \therefore make your comments as “self contained” as possible, to be self contained, the reader does not have to refer back to other commands to understand current comment!

    + +

    Example of an ISA: MIPS

    + +

    (Sub)set of instructions:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    MIPS assembly language instructionsSemantic (i.e., Meaning)Corresponding MIPS machine instructions
    lw $s1, 20($s2) A$s1 = M[$s2 + 20]?
    sw $s1, 20($s2) AM[$s2 + 20] = $s1?
    add $s1, $s2, $s3$s1 = $s2 + $s3?
    sub $s1, $s2, $s3$s1 = $s2 - $s3?
    beq $s1, $s2, 25if ($s1 == $s2) go to PC + 4 + 100 [Annotation: PC+4 done as part of loop fetch-decode-execute]?
    j 2500go to 10000 (2500 * 4 bytes)?
    jal 2500$ra = PC + 4; go to 10000?
    + +
      +
    • Memory addressing modes -> Direct (absolute), +base + displacement and Indirect
    • +
    • Operand model ->
    • +
    • Format/syntax of corresponding MIPS machine instructions? +
        +
      • Length of machine instruction -> 32 bits (4 bytes)
      • +
      • Size of opcode? Size of other fields? Order of operands?
      • +
      +
    • +
    + +

    Format/syntax of these bit patterns are all the question marks.

    + +

    A closer look at MIPS’ add instruction

    + +
      +
    • MIPS assembly language instruction: +
        +
      • add $s0, $al, $t7
      • +
      +
    • +
    • Corresponding MIPS machine instruction: +
        +
      • 0x00af8020
      • +
      • Binary: 0000 0000 1010 1111 1000 0000 0010 0000
      • +
      • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
        Breakdown: LabelBinaryMeaningBits
        opcode000000operation of the instruction6 bits
        rs ($al in add $s0,$al,$t7)00101first register source operand5 bits
        rt ($t7 in add $s0,$al,$t7)01111second register source operand5 bits
        rd ($s0 in add $s0,$al,$t7)10000register destination operand (contains result of operation)5 bits
        shamt00000shift amount5 bits
        func100000function – often called function code, which indicates the specific variant of the operation in the opcode field6 bits
        +
      • +
      +
    • +
    + +

    [Annotation: Why 5 bits for rs, rt and rd? +25=322^{5} =32 registers -> need 5 bits to uniquely identify 32 registers]

    + +

    Total: 32 bits

    + +

    Let’s examine an ISA: MIPS (3 of 3)

    + +
      +
    • Function call conventions +
        +
      • caller saved registers (4-15, 24, 25)
      • +
      • callee saved registers (16-23, 30, 31)
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register nameNumberUsage
    $zero0constant 0
    $at1reserved for assembler
    $v02expression evaluation and results of a function
    $v13expression evaluation and results of a function
    $a04argument 1
    $a15argument 2
    $a26argument 3
    $a37argument 4
    $t08temporary (not preserved across call)
    $t19temporary (not preserved across call)
    $t210temporary (not preserved across call)
    $t311temporary (not preserved across call)
    $t512temporary (not preserved across call)
    $t613temporary (not preserved across call)
    $t714temporary (not preserved across call)
    $s015saved temporary (preserved across call)
    $s116saved temporary (preserved across call)
    $s217saved temporary (preserved across call)
    $s318saved temporary (preserved across call)
    $s419saved temporary (preserved across call)
    $s520saved temporary (preserved across call)
    $s621saved temporary (preserved across call)
    $s722saved temporary (preserved across call)
    $t824temporary (not preserved across call)
    $t925temporary (not preserved across call)
    $k026reserved for OS kernel
    $k127reserved for OS kernel
    $gp28pointer to global area
    $sp29stack pointer
    $fp30frame pointer
    $ra31return address (used by function call)
    + +

    MIPS - Design guidelines

    + +
      +
    • opcode
    • +
    • rs
    • +
    • rt
    • +
    • rd
    • +
    • shamt
    • +
    • func
    • +
    + +

    3) In terms of machine instruction format: + 1. Create as few of them as possible + 2. Have them all of the same length and same format! + 3. If we have in different machine instruction formats, then position the +fields that have the same purpose in the same location in the format

    +
      +
    • Can all MIPS machine instructions have the same length and same +format? +
        +
      • For example: lw $s1, 20($s2)
      • +
      • When designing its corresponding machine instruction … +
          +
        • Must specify source register using 5 bits -> OK!
        • +
        • Must specify destination register using 5 bits -> OK!
        • +
        • Must specify a constant using 5 bits -> Hum… +
            +
          • Value of constant limited to [0..25-1]
          • +
          • Often use to access array elements +so needs to be >25=32> 2^{5} = 32
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    MIPS ISA designers compromise

    + +
      +
    • Keep all machine instructions format the same length
    • +
    • Consequence -> different formats for different kinds of MIPS +instructions +
        +
      • R-format for register +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          rs5 bits
          rt5 bits
          shamt5 bits
          func6 bits
          +
        • +
        +
      • +
      • I-format for immediate +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          rs5 bits
          rt5 bits
          Address/immediate16 bits
          +
        • +
        +
      • +
      • J-format for jump +
          +
        • + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          Target address26 bits
          +
        • +
        +
      • +
      +
    • +
    • opcode indicates the format of the instruction +
        +
      • This way, the hardware knows whether to treat the last half of the +instruction as 3 fields (R-format) or as 1 field (I-format)
      • +
      • Also, position of fields with same purpose are in the same location +in the 3 formats ☺
      • +
      +
    • +
    + +

    Summary

    + +
      +
    • Types of instruction sets: CISC and RISC
    • +
    • Looked at an example of a RISC instruction set: MIPS +
        +
      • Registers and Memory model
      • +
      • (Sub)set of instructions (assembly + machine instructions)
      • +
      • Function call conventions
      • +
      • Model of computation
      • +
      +
    • +
    • MIPS design principles +
        +
      1. Unambiguous binary encoding of instruction
      2. +
      3. (highlighted) IS functionally complete (“Turing complete”)
      4. +
      5. Machine instruction format -> only 3 of same length but of different format! + * R-format for register + * I-format for immediate + * J-format for jump
      6. +
      +
    • +
    • Also, position of fields with same purpose are in the same location in the 3 +formats ☺
    • +
    + +

    Next lecture

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design guidelines
      • +
      • Example of an instruction set: MIPS
      • +
      • (highlighted) Create our own instruction sets
      • +
      • (highlighted) ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/24/24-annotated/index.html b/_site/melody/cmpt-295/24/24-annotated/index.html new file mode 100644 index 0000000..c81b172 --- /dev/null +++ b/_site/melody/cmpt-295/24/24-annotated/index.html @@ -0,0 +1,885 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 - Unit - Instruction Set Architecture

    + +

    I do not like computer jokes…not one bit!

    + +

    Lecture 24:

    + +
      +
    • MIPS
    • +
    + +

    Last Lecture

    + +
      +
    • Assembler (part of the compilation process): +
        +
      • Transforms assembly code (movl %edi,%eax) into machine code +(0xf889 -> 1111100010001001)
      • +
      +
    • +
    • Instruction Set Architecture (ISA) [Annotation: …is an abstract model of a computer …is an interface between software (and s/w developers) and hardware (hardware designers) +A formal specification (or agreement) of: +
        +
      • Registers and memory model, set of instructions (assembly-machine) [Annotation: main memory]
      • +
      • Conventions, model of computation [Annotation: data types, memory addressing modes]
      • +
      • etc…
      • +
      +
    • +
    • Design principles when creating instruction set (IS) +
        +
      1. Each instruction must have an unambiguous encoding
      2. +
      3. Functionally complete (Turing complete)
      4. +
      5. Machine instruction format: 1) as few of them as possible 2) of the +same length 3) fields that have the same purpose positioned in the +same location in the format
      6. +
      +
    • +
    • Types of instruction sets: CISC and RISC
    • +
    + +

    [Annotation: A realization of an ISA is called an implementation. An ISA permits multiple implementations.]

    + +

    Today’s Menu

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design guidelines
      • +
      • (highlighted) Example of an instruction set: MIPS
      • +
      • Create our own instruction sets
      • +
      • ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +

    Example of another ISA: MIPS

    + +

    [Annotation: MIPS = microprocessor without interlocked pipelined stages.]

    + +

    Example of an ISA: MIPS

    + +
      +
    • Registers and Memory model +
        +
      • # of registers -> 32 registers (each register is 32 bit wide) See Figure on next Slide [Annotation: 1]
      • +
      • Word size -> 32 bits [Annotation: 3]
      • +
      • Memory size -> 2m×n=232×8 bits2^{m} \times n = 2^{32} \times 8 \text{ bits} [Annotation: 5]
      • +
      • Byte-addressable memory +so address resolution -> n = 8 bits [Annotation: 4]
      • +
      • Size of memory address (# of bits) -> 32 bits; m = 32 [Annotation: 2]
      • +
      +
    • +
    • So, there are 2322^{32} distinct addressable memory “chunks” +(or “locations”) and each of these addressable memory +“chunks” (or “locations”) has 8 bits See Figure on next next Slide [Annotation: 6]
    • +
    + +

    FIGURE A.6.1 MIPS registers and usage conventions.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register nameNumberUsage
    $zero0constant 0
    $at1reserved for assembler
    $v02expression evaluation and results of a function
    $v13expression evaluation and results of a function
    $a04argument 1
    $a15argument 2
    $a26argument 3
    $a37argument 4
    $t08temporary (not preserved across call)
    $t19temporary (not preserved across call)
    $t210temporary (not preserved across call)
    $t311temporary (not preserved across call)
    $t512temporary (not preserved across call)
    $t613temporary (not preserved across call)
    $t714temporary (not preserved across call)
    $s015saved temporary (preserved across call)
    $s116saved temporary (preserved across call)
    $s217saved temporary (preserved across call)
    $s318saved temporary (preserved across call)
    $s419saved temporary (preserved across call)
    $s520saved temporary (preserved across call)
    $s621saved temporary (preserved across call)
    $s722saved temporary (preserved across call)
    $t824temporary (not preserved across call)
    $t925temporary (not preserved across call)
    $k026reserved for OS kernel
    $k127reserved for OS kernel
    $gp28pointer to global area
    $sp29stack pointer
    $fp30frame pointer
    $ra31return address (used by function call)
    + +

    Source: Page A-24 in Patterson and Hennessy

    + +

    MIPS Memory Model

    + +

    FIGURE 2.13 – The MIPS memory allocation for program and data

    + +

    These addresses are only a software convention, +and not part of the MIPS architecture. +The stack pointer is initialized to 7fff fffchex\text{7fff fffc}_{\text{hex}} and grows down toward the segment. +At the other end, the program code (“text”) starts at 0040 0000hex\text{0040 0000}_{\text{hex}}. +The static data starts at 1000 0000hex\text{1000 0000}_{\text{hex}}. +Dynamic data, allocated by malloc in C and by new in Java, is next. +It grows up toward the stack and in an area called the heap. +The global pointer: $gp, is set to an address to make it easy to access data. +It is initialized to 1000 8000hex\text{1000 8000}_{\text{hex}} +so it can access from 1000 000hex\text{1000 000}_{\text{hex}} to 1000 ffffhex\text{1000 ffff}_{\text{hex}} using the positive and negative 16-bit offsets from $gp. +The information is also found in Column 4 of the MIPS Reference Data Card at the front of the book.

    + +

    Source: Page 104 in Patterson and Hennessy

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressLabel
    (top) $sp -> 7fff ffffhex\text{7fff ffff}_{\text{hex}}Stack (arrow pointing down)
     Dynamic data (arrow pointing up) [Annotation: heap]
    (between Static Data and Text) $gp -> 1000 8000hex\text{1000 8000}_{\text{hex}}, 1000 0000hex\text{1000 0000}_{\text{hex}}Static Data
    (bottom) $pc -> 0040 0000hex\text{0040 0000}_{\text{hex}}Text
    (bottom) 0Reserved
    + +

    Example of an ISA: MIPS

    + +
      +
    • Instruction set +
        +
      • MIPS assembly language notation +
          +
        • add a, b, c, Meaning: a = b+c
        • +
        +
      • +
      • 3 operand assembly language +
          +
        • Requiring all instructions to have 3 operands would +keep the design of the microprocessor hardware +simple
        • +
        • Hardware for a variable number of operands is +more complicated
        • +
        +
      • +
      +
    • +
    + +

    Activity

    + +
      +
    • +

      If we want to write an assembly program that sums +four variables b, c , d and e, how many MIPS add +instructions would we need?

      +
    • +
    • +

      Solution:

      +
    • +
    + +
    add a,b,c # a = b+c
    +add a,a,d # a = b+c+d
    +add a,a,e # a = b+c+d+e
    +
    + +

    \therefore make your comments as “self contained” as possible, to be self contained, the reader does not have to refer back to other commands to understand current comment!

    + +

    Example of an ISA: MIPS

    + +

    (Sub)set of instructions:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    MIPS assembly language instructionsSemantic (i.e., Meaning)Corresponding MIPS machine instructions
    lw $s1, 20($s2) A$s1 = M[$s2 + 20]?
    sw $s1, 20($s2) AM[$s2 + 20] = $s1?
    add $s1, $s2, $s3$s1 = $s2 + $s3?
    sub $s1, $s2, $s3$s1 = $s2 - $s3?
    beq $s1, $s2, 25if ($s1 == $s2) go to PC + 4 + 100 [Annotation: PC+4 done as part of loop fetch-decode-execute]?
    j 2500go to 10000 (2500 * 4 bytes)?
    jal 2500$ra = PC + 4; go to 10000?
    + +
      +
    • Memory addressing modes -> Direct (absolute), +base + displacement and Indirect
    • +
    • Operand model ->
    • +
    • Format/syntax of corresponding MIPS machine instructions? +
        +
      • Length of machine instruction -> 32 bits (4 bytes)
      • +
      • Size of opcode? Size of other fields? Order of operands?
      • +
      +
    • +
    + +

    Format/syntax of these bit patterns are all the question marks.

    + +

    A closer look at MIPS’ add instruction

    + +
      +
    • MIPS assembly language instruction: +
        +
      • add $s0, $al, $t7
      • +
      +
    • +
    • Corresponding MIPS machine instruction: +
        +
      • 0x00af8020
      • +
      • Binary: 0000 0000 1010 1111 1000 0000 0010 0000
      • +
      • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
        Breakdown: LabelBinaryMeaningBits
        opcode000000operation of the instruction6 bits
        rs ($al in add $s0,$al,$t7)00101first register source operand5 bits
        rt ($t7 in add $s0,$al,$t7)01111second register source operand5 bits
        rd ($s0 in add $s0,$al,$t7)10000register destination operand (contains result of operation)5 bits
        shamt00000shift amount5 bits
        func100000function – often called function code, which indicates the specific variant of the operation in the opcode field6 bits
        +
      • +
      +
    • +
    + +

    [Annotation: Why 5 bits for rs, rt and rd? +25=322^{5} =32 registers -> need 5 bits to uniquely identify 32 registers]

    + +

    Total: 32 bits

    + +

    Let’s examine an ISA: MIPS (3 of 3)

    + +
      +
    • Function call conventions +
        +
      • caller saved registers (4-15, 24, 25)
      • +
      • callee saved registers (16-23, 30, 31)
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register nameNumberUsage
    $zero0constant 0
    $at1reserved for assembler
    $v02expression evaluation and results of a function
    $v13expression evaluation and results of a function
    $a04argument 1
    $a15argument 2
    $a26argument 3
    $a37argument 4
    $t08temporary (not preserved across call)
    $t19temporary (not preserved across call)
    $t210temporary (not preserved across call)
    $t311temporary (not preserved across call)
    $t512temporary (not preserved across call)
    $t613temporary (not preserved across call)
    $t714temporary (not preserved across call)
    $s015saved temporary (preserved across call)
    $s116saved temporary (preserved across call)
    $s217saved temporary (preserved across call)
    $s318saved temporary (preserved across call)
    $s419saved temporary (preserved across call)
    $s520saved temporary (preserved across call)
    $s621saved temporary (preserved across call)
    $s722saved temporary (preserved across call)
    $t824temporary (not preserved across call)
    $t925temporary (not preserved across call)
    $k026reserved for OS kernel
    $k127reserved for OS kernel
    $gp28pointer to global area
    $sp29stack pointer
    $fp30frame pointer
    $ra31return address (used by function call)
    + +

    MIPS - Design guidelines

    + +
      +
    • opcode
    • +
    • rs
    • +
    • rt
    • +
    • rd
    • +
    • shamt
    • +
    • func
    • +
    + +

    3) In terms of machine instruction format: + 1. Create as few of them as possible + 2. Have them all of the same length and same format! + 3. If we have in different machine instruction formats, then position the +fields that have the same purpose in the same location in the format

    +
      +
    • Can all MIPS machine instructions have the same length and same +format? +
        +
      • For example: lw $s1, 20($s2)
      • +
      • When designing its corresponding machine instruction … +
          +
        • Must specify source register using 5 bits -> OK!
        • +
        • Must specify destination register using 5 bits -> OK!
        • +
        • Must specify a constant using 5 bits -> Hum… +
            +
          • Value of constant limited to [0..25-1]
          • +
          • Often use to access array elements +so needs to be >25=32> 2^{5} = 32
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    MIPS ISA designers compromise

    + +
      +
    • Keep all machine instructions format the same length
    • +
    • Consequence -> different formats for different kinds of MIPS +instructions +
        +
      • R-format for register +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          rs5 bits
          rt5 bits
          shamt5 bits
          func6 bits
          +
        • +
        +
      • +
      • I-format for immediate +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          rs5 bits
          rt5 bits
          Address/immediate16 bits
          +
        • +
        +
      • +
      • J-format for jump +
          +
        • + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          Target address26 bits
          +
        • +
        +
      • +
      +
    • +
    • opcode indicates the format of the instruction +
        +
      • This way, the hardware knows whether to treat the last half of the +instruction as 3 fields (R-format) or as 1 field (I-format)
      • +
      • Also, position of fields with same purpose are in the same location +in the 3 formats ☺
      • +
      +
    • +
    + +

    Summary

    + +
      +
    • Types of instruction sets: CISC and RISC
    • +
    • Looked at an example of a RISC instruction set: MIPS +
        +
      • Registers and Memory model
      • +
      • (Sub)set of instructions (assembly + machine instructions)
      • +
      • Function call conventions
      • +
      • Model of computation
      • +
      +
    • +
    • MIPS design principles +
        +
      1. Unambiguous binary encoding of instruction
      2. +
      3. (highlighted) IS functionally complete (“Turing complete”)
      4. +
      5. Machine instruction format -> only 3 of same length but of different format! + * R-format for register + * I-format for immediate + * J-format for jump
      6. +
      +
    • +
    • Also, position of fields with same purpose are in the same location in the 3 +formats ☺
    • +
    + +

    Next lecture

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design guidelines
      • +
      • Example of an instruction set: MIPS
      • +
      • (highlighted) Create our own instruction sets
      • +
      • (highlighted) ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/24/24.html b/_site/melody/cmpt-295/24/24.html new file mode 100644 index 0000000..42b88a2 --- /dev/null +++ b/_site/melody/cmpt-295/24/24.html @@ -0,0 +1,875 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 - Unit - Instruction Set Architecture

    + +

    I do not like computer jokes…not one bit!

    + +

    Lecture 24:

    + +
      +
    • MIPS
    • +
    + +

    Last Lecture

    + +
      +
    • Assembler (part of the compilation process): +
        +
      • Transforms assembly code (movl %edi,%eax) into machine code +(0xf889 -> 1111100010001001)
      • +
      +
    • +
    • Instruction Set Architecture (ISA) +A formal specification (or agreement) of: +
        +
      • Registers and memory model, set of instructions (assembly-machine)
      • +
      • Conventions, model of computation
      • +
      • etc…
      • +
      +
    • +
    • Design principles when creating instruction set (IS) +
        +
      1. Each instruction must have an unambiguous encoding
      2. +
      3. Functionally complete (Turing complete)
      4. +
      5. Machine instruction format: 1) as few of them as possible 2) of the +same length 3) fields that have the same purpose positioned in the +same location in the format
      6. +
      +
    • +
    • Types of instruction sets: CISC and RISC
    • +
    + +

    Today’s Menu

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design guidelines
      • +
      • (highlighted) Example of an instruction set: MIPS
      • +
      • Create our own instruction sets
      • +
      • ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +

    Example of another ISA: MIPS

    + +

    (Transcriber’s note: rest of slide is blank)

    + +

    Example of an ISA: MIPS

    + +
      +
    • Registers and Memory model +
        +
      • +

        of registers -> 32 registers (each register is 32 bit wide) See Figure on next Slide

        +
      • +
      • Word size -> 32 bits
      • +
      • Memory size -> 2m x n
      • +
      • Byte-addressable memory +so address resolution ->
      • +
      • Size of memory address (# of bits) ->
      • +
      +
    • +
    • So, there are _______ distinct addressable memory “chunks” +(or “locations”) and each of these addressable memory +“chunks” (or “locations”) has _____ bits See Figure on next next Slide
    • +
    + +

    FIGURE A.6.1 MIPS registers and usage conventions.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register nameNumberUsage
    $zero0constant 0
    $at1reserved for assembler
    $v02expression evaluation and results of a function
    $v13expression evaluation and results of a function
    $a04argument 1
    $a15argument 2
    $a26argument 3
    $a37argument 4
    $t08temporary (not preserved across call)
    $t19temporary (not preserved across call)
    $t210temporary (not preserved across call)
    $t311temporary (not preserved across call)
    $t512temporary (not preserved across call)
    $t613temporary (not preserved across call)
    $t714temporary (not preserved across call)
    $s015saved temporary (preserved across call)
    $s116saved temporary (preserved across call)
    $s217saved temporary (preserved across call)
    $s318saved temporary (preserved across call)
    $s419saved temporary (preserved across call)
    $s520saved temporary (preserved across call)
    $s621saved temporary (preserved across call)
    $s722saved temporary (preserved across call)
    $t824temporary (not preserved across call)
    $t925temporary (not preserved across call)
    $k026reserved for OS kernel
    $k127reserved for OS kernel
    $gp28pointer to global area
    $sp29stack pointer
    $fp30frame pointer
    $ra31return address (used by function call)
    + +

    Source: Page A-24 in Patterson and Hennessy

    + +

    MIPS Memory Model

    + +

    FIGURE 2.13 – The MIPS memory allocation for program and data

    + +

    These addresses are only a software convention, +and not part of the MIPS architecture. +The stack pointer is initialized to 7fff fffchex\text{7fff fffc}_{\text{hex}} and grows down toward the segment. +At the other end, the program code (“text”) starts at 0040 0000hex\text{0040 0000}_{\text{hex}}. +The static data starts at 1000 0000hex\text{1000 0000}_{\text{hex}}. +Dynamic data, allocated by malloc in C and by new in Java, is next. +It grows up toward the stack and in an area called the heap. +The global pointer: $gp, is set to an address to make it easy to access data. +It is initialized to 1000 8000hex\text{1000 8000}_{\text{hex}} +so it can access from 1000 000hex\text{1000 000}_{\text{hex}} to 1000 ffffhex\text{1000 ffff}_{\text{hex}} using the positive and negative 16-bit offsets from $gp. +The information is also found in Column 4 of the MIPS Reference Data Card at the front of the book.

    + +

    Source: Page 104 in Patterson and Hennessy

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressLabel
    (top) $sp -> 7fff ffffhex\text{7fff ffff}_{\text{hex}}Stack (arrow pointing down)
     Dynamic data (arrow pointing up)
    (between Static Data and Text) $gp -> 1000 8000hex\text{1000 8000}_{\text{hex}}, 1000 0000hex\text{1000 0000}_{\text{hex}}Static Data
    (bottom) $pc -> 0040 0000hex\text{0040 0000}_{\text{hex}}Text
    (bottom) 0Reserved
    + +

    Example of an ISA: MIPS

    + +
      +
    • Instruction set +
        +
      • MIPS assembly language notation +
          +
        • add a, b, c, Meaning: a = b+c
        • +
        +
      • +
      • 3 operand assembly language +
          +
        • Requiring all instructions to have 3 operands would +keep the design of the microprocessor hardware +simple
        • +
        • Hardware for a variable number of operands is +more complicated
        • +
        +
      • +
      +
    • +
    + +

    Activity

    + +
      +
    • +

      If we want to write an assembly program that sums +four variables b, c , d and e, how many MIPS add +instructions would we need?

      +
    • +
    • +

      Solution:

      +
    • +
    + +

    Example of an ISA: MIPS

    + +

    (Sub)set of instructions:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    MIPS assembly language instructionsSemantic (i.e., Meaning)Corresponding MIPS machine instructions
    lw $s1, 20($s2)$s1 = M[$s2 + 20]?
    sw $s1, 20($s2)M[$s2 + 20] = $s1?
    add $s1, $s2, $s3$s1 = $s2 + $s3?
    sub $s1, $s2, $s3$s1 = $s2 - $s3?
    beq $s1, $s2, 25if ($s1 == $s2) go to PC + 4 + 100?
    j 2500go to 10000 (2500 * 4 bytes)?
    jal 2500$ra = PC + 4; go to 10000?
    + +
      +
    • Memory addressing modes -> Direct (absolute), +base + displacement and Indirect
    • +
    • Operand model ->
    • +
    • Format/syntax of corresponding MIPS machine instructions? +
        +
      • Length of machine instruction -> 32 bits (4 bytes)
      • +
      • Size of opcode? Size of other fields? Order of operands?
      • +
      +
    • +
    + +

    Format/syntax of these bit patterns are all the question marks.

    + +

    A closer look at MIPS’ add instruction

    + +
      +
    • MIPS assembly language instruction: +
        +
      • add $s0, $al, $t7
      • +
      +
    • +
    • Corresponding MIPS machine instruction: +
        +
      • 0x00af8020
      • +
      • Binary: 0000 0000 1010 1111 1000 0000 0010 0000
      • +
      • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
        Breakdown: LabelBinaryMeaningBits
        opcode000000operation of the instruction6 bits
        rs00101first register source operand5 bits
        rt01111second register source operand5 bits
        rd10000register destination operand (contains result of operation)5 bits
        shamt00000shift amount5 bits
        func100000function – often called function code, which indicates the specific variant of the operation in the opcode field6 bits
        +
      • +
      +
    • +
    + +

    Total: 32 bits

    + +

    Let’s examine an ISA: MIPS (3 of 3)

    + +
      +
    • Function call conventions +
        +
      • caller saved registers (4-15, 24, 25)
      • +
      • callee saved registers (16-23, 30, 31)
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register nameNumberUsage
    $zero0constant 0
    $at1reserved for assembler
    $v02expression evaluation and results of a function
    $v13expression evaluation and results of a function
    $a04argument 1
    $a15argument 2
    $a26argument 3
    $a37argument 4
    $t08temporary (not preserved across call)
    $t19temporary (not preserved across call)
    $t210temporary (not preserved across call)
    $t311temporary (not preserved across call)
    $t512temporary (not preserved across call)
    $t613temporary (not preserved across call)
    $t714temporary (not preserved across call)
    $s015saved temporary (preserved across call)
    $s116saved temporary (preserved across call)
    $s217saved temporary (preserved across call)
    $s318saved temporary (preserved across call)
    $s419saved temporary (preserved across call)
    $s520saved temporary (preserved across call)
    $s621saved temporary (preserved across call)
    $s722saved temporary (preserved across call)
    $t824temporary (not preserved across call)
    $t925temporary (not preserved across call)
    $k026reserved for OS kernel
    $k127reserved for OS kernel
    $gp28pointer to global area
    $sp29stack pointer
    $fp30frame pointer
    $ra31return address (used by function call)
    + +

    MIPS - Design guidelines

    + +
      +
    • opcode
    • +
    • rs
    • +
    • rt
    • +
    • rd
    • +
    • shamt
    • +
    • func
    • +
    + +

    3) In terms of machine instruction format: + 1. Create as few of them as possible + 2. Have them all of the same length and same format! + 3. If we have in different machine instruction formats, then position the +fields that have the same purpose in the same location in the format

    +
      +
    • Can all MIPS machine instructions have the same length and same +format? +
        +
      • For example: lw $s1, 20($s2)
      • +
      • When designing its corresponding machine instruction … +
          +
        • Must specify source register using 5 bits -> OK!
        • +
        • Must specify destination register using 5 bits -> OK!
        • +
        • Must specify a constant using 5 bits -> Hum… +
            +
          • Value of constant limited to [0..25-1]
          • +
          • Often use to access array elements +so needs to be >25=32> 2^{5} = 32
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    MIPS ISA designers compromise

    + +
      +
    • Keep all machine instructions format the same length
    • +
    • Consequence -> different formats for different kinds of MIPS +instructions +
        +
      • R-format for register +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          rs5 bits
          rt5 bits
          shamt5 bits
          func6 bits
          +
        • +
        +
      • +
      • I-format for immediate +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          rs5 bits
          rt5 bits
          Address/immediate16 bits
          +
        • +
        +
      • +
      • J-format for jump +
          +
        • + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          Target address26 bits
          +
        • +
        +
      • +
      +
    • +
    • opcode indicates the format of the instruction +
        +
      • This way, the hardware knows whether to treat the last half of the +instruction as 3 fields (R-format) or as 1 field (I-format)
      • +
      • Also, position of fields with same purpose are in the same location +in the 3 formats ☺
      • +
      +
    • +
    + +

    Summary

    + +
      +
    • Types of instruction sets: CISC and RISC
    • +
    • Looked at an example of a RISC instruction set: MIPS +
        +
      • Registers and Memory model
      • +
      • (Sub)set of instructions (assembly + machine instructions)
      • +
      • Function call conventions
      • +
      • Model of computation
      • +
      +
    • +
    • MIPS design principles +
        +
      1. Unambiguous binary encoding of instruction
      2. +
      3. (highlighted) IS functionally complete (“Turing complete”)
      4. +
      5. Machine instruction format -> only 3 of same length but of different format! + * R-format for register + * I-format for immediate + * J-format for jump
      6. +
      +
    • +
    • Also, position of fields with same purpose are in the same location in the 3 +formats ☺
    • +
    + +

    Next lecture

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design guidelines
      • +
      • Example of an instruction set: MIPS
      • +
      • (highlighted) Create our own instruction sets
      • +
      • (highlighted) ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/24/24/index.html b/_site/melody/cmpt-295/24/24/index.html new file mode 100644 index 0000000..42b88a2 --- /dev/null +++ b/_site/melody/cmpt-295/24/24/index.html @@ -0,0 +1,875 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 - Unit - Instruction Set Architecture

    + +

    I do not like computer jokes…not one bit!

    + +

    Lecture 24:

    + +
      +
    • MIPS
    • +
    + +

    Last Lecture

    + +
      +
    • Assembler (part of the compilation process): +
        +
      • Transforms assembly code (movl %edi,%eax) into machine code +(0xf889 -> 1111100010001001)
      • +
      +
    • +
    • Instruction Set Architecture (ISA) +A formal specification (or agreement) of: +
        +
      • Registers and memory model, set of instructions (assembly-machine)
      • +
      • Conventions, model of computation
      • +
      • etc…
      • +
      +
    • +
    • Design principles when creating instruction set (IS) +
        +
      1. Each instruction must have an unambiguous encoding
      2. +
      3. Functionally complete (Turing complete)
      4. +
      5. Machine instruction format: 1) as few of them as possible 2) of the +same length 3) fields that have the same purpose positioned in the +same location in the format
      6. +
      +
    • +
    • Types of instruction sets: CISC and RISC
    • +
    + +

    Today’s Menu

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design guidelines
      • +
      • (highlighted) Example of an instruction set: MIPS
      • +
      • Create our own instruction sets
      • +
      • ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +

    Example of another ISA: MIPS

    + +

    (Transcriber’s note: rest of slide is blank)

    + +

    Example of an ISA: MIPS

    + +
      +
    • Registers and Memory model +
        +
      • +

        of registers -> 32 registers (each register is 32 bit wide) See Figure on next Slide

        +
      • +
      • Word size -> 32 bits
      • +
      • Memory size -> 2m x n
      • +
      • Byte-addressable memory +so address resolution ->
      • +
      • Size of memory address (# of bits) ->
      • +
      +
    • +
    • So, there are _______ distinct addressable memory “chunks” +(or “locations”) and each of these addressable memory +“chunks” (or “locations”) has _____ bits See Figure on next next Slide
    • +
    + +

    FIGURE A.6.1 MIPS registers and usage conventions.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register nameNumberUsage
    $zero0constant 0
    $at1reserved for assembler
    $v02expression evaluation and results of a function
    $v13expression evaluation and results of a function
    $a04argument 1
    $a15argument 2
    $a26argument 3
    $a37argument 4
    $t08temporary (not preserved across call)
    $t19temporary (not preserved across call)
    $t210temporary (not preserved across call)
    $t311temporary (not preserved across call)
    $t512temporary (not preserved across call)
    $t613temporary (not preserved across call)
    $t714temporary (not preserved across call)
    $s015saved temporary (preserved across call)
    $s116saved temporary (preserved across call)
    $s217saved temporary (preserved across call)
    $s318saved temporary (preserved across call)
    $s419saved temporary (preserved across call)
    $s520saved temporary (preserved across call)
    $s621saved temporary (preserved across call)
    $s722saved temporary (preserved across call)
    $t824temporary (not preserved across call)
    $t925temporary (not preserved across call)
    $k026reserved for OS kernel
    $k127reserved for OS kernel
    $gp28pointer to global area
    $sp29stack pointer
    $fp30frame pointer
    $ra31return address (used by function call)
    + +

    Source: Page A-24 in Patterson and Hennessy

    + +

    MIPS Memory Model

    + +

    FIGURE 2.13 – The MIPS memory allocation for program and data

    + +

    These addresses are only a software convention, +and not part of the MIPS architecture. +The stack pointer is initialized to 7fff fffchex\text{7fff fffc}_{\text{hex}} and grows down toward the segment. +At the other end, the program code (“text”) starts at 0040 0000hex\text{0040 0000}_{\text{hex}}. +The static data starts at 1000 0000hex\text{1000 0000}_{\text{hex}}. +Dynamic data, allocated by malloc in C and by new in Java, is next. +It grows up toward the stack and in an area called the heap. +The global pointer: $gp, is set to an address to make it easy to access data. +It is initialized to 1000 8000hex\text{1000 8000}_{\text{hex}} +so it can access from 1000 000hex\text{1000 000}_{\text{hex}} to 1000 ffffhex\text{1000 ffff}_{\text{hex}} using the positive and negative 16-bit offsets from $gp. +The information is also found in Column 4 of the MIPS Reference Data Card at the front of the book.

    + +

    Source: Page 104 in Patterson and Hennessy

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressLabel
    (top) $sp -> 7fff ffffhex\text{7fff ffff}_{\text{hex}}Stack (arrow pointing down)
     Dynamic data (arrow pointing up)
    (between Static Data and Text) $gp -> 1000 8000hex\text{1000 8000}_{\text{hex}}, 1000 0000hex\text{1000 0000}_{\text{hex}}Static Data
    (bottom) $pc -> 0040 0000hex\text{0040 0000}_{\text{hex}}Text
    (bottom) 0Reserved
    + +

    Example of an ISA: MIPS

    + +
      +
    • Instruction set +
        +
      • MIPS assembly language notation +
          +
        • add a, b, c, Meaning: a = b+c
        • +
        +
      • +
      • 3 operand assembly language +
          +
        • Requiring all instructions to have 3 operands would +keep the design of the microprocessor hardware +simple
        • +
        • Hardware for a variable number of operands is +more complicated
        • +
        +
      • +
      +
    • +
    + +

    Activity

    + +
      +
    • +

      If we want to write an assembly program that sums +four variables b, c , d and e, how many MIPS add +instructions would we need?

      +
    • +
    • +

      Solution:

      +
    • +
    + +

    Example of an ISA: MIPS

    + +

    (Sub)set of instructions:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    MIPS assembly language instructionsSemantic (i.e., Meaning)Corresponding MIPS machine instructions
    lw $s1, 20($s2)$s1 = M[$s2 + 20]?
    sw $s1, 20($s2)M[$s2 + 20] = $s1?
    add $s1, $s2, $s3$s1 = $s2 + $s3?
    sub $s1, $s2, $s3$s1 = $s2 - $s3?
    beq $s1, $s2, 25if ($s1 == $s2) go to PC + 4 + 100?
    j 2500go to 10000 (2500 * 4 bytes)?
    jal 2500$ra = PC + 4; go to 10000?
    + +
      +
    • Memory addressing modes -> Direct (absolute), +base + displacement and Indirect
    • +
    • Operand model ->
    • +
    • Format/syntax of corresponding MIPS machine instructions? +
        +
      • Length of machine instruction -> 32 bits (4 bytes)
      • +
      • Size of opcode? Size of other fields? Order of operands?
      • +
      +
    • +
    + +

    Format/syntax of these bit patterns are all the question marks.

    + +

    A closer look at MIPS’ add instruction

    + +
      +
    • MIPS assembly language instruction: +
        +
      • add $s0, $al, $t7
      • +
      +
    • +
    • Corresponding MIPS machine instruction: +
        +
      • 0x00af8020
      • +
      • Binary: 0000 0000 1010 1111 1000 0000 0010 0000
      • +
      • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
        Breakdown: LabelBinaryMeaningBits
        opcode000000operation of the instruction6 bits
        rs00101first register source operand5 bits
        rt01111second register source operand5 bits
        rd10000register destination operand (contains result of operation)5 bits
        shamt00000shift amount5 bits
        func100000function – often called function code, which indicates the specific variant of the operation in the opcode field6 bits
        +
      • +
      +
    • +
    + +

    Total: 32 bits

    + +

    Let’s examine an ISA: MIPS (3 of 3)

    + +
      +
    • Function call conventions +
        +
      • caller saved registers (4-15, 24, 25)
      • +
      • callee saved registers (16-23, 30, 31)
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register nameNumberUsage
    $zero0constant 0
    $at1reserved for assembler
    $v02expression evaluation and results of a function
    $v13expression evaluation and results of a function
    $a04argument 1
    $a15argument 2
    $a26argument 3
    $a37argument 4
    $t08temporary (not preserved across call)
    $t19temporary (not preserved across call)
    $t210temporary (not preserved across call)
    $t311temporary (not preserved across call)
    $t512temporary (not preserved across call)
    $t613temporary (not preserved across call)
    $t714temporary (not preserved across call)
    $s015saved temporary (preserved across call)
    $s116saved temporary (preserved across call)
    $s217saved temporary (preserved across call)
    $s318saved temporary (preserved across call)
    $s419saved temporary (preserved across call)
    $s520saved temporary (preserved across call)
    $s621saved temporary (preserved across call)
    $s722saved temporary (preserved across call)
    $t824temporary (not preserved across call)
    $t925temporary (not preserved across call)
    $k026reserved for OS kernel
    $k127reserved for OS kernel
    $gp28pointer to global area
    $sp29stack pointer
    $fp30frame pointer
    $ra31return address (used by function call)
    + +

    MIPS - Design guidelines

    + +
      +
    • opcode
    • +
    • rs
    • +
    • rt
    • +
    • rd
    • +
    • shamt
    • +
    • func
    • +
    + +

    3) In terms of machine instruction format: + 1. Create as few of them as possible + 2. Have them all of the same length and same format! + 3. If we have in different machine instruction formats, then position the +fields that have the same purpose in the same location in the format

    +
      +
    • Can all MIPS machine instructions have the same length and same +format? +
        +
      • For example: lw $s1, 20($s2)
      • +
      • When designing its corresponding machine instruction … +
          +
        • Must specify source register using 5 bits -> OK!
        • +
        • Must specify destination register using 5 bits -> OK!
        • +
        • Must specify a constant using 5 bits -> Hum… +
            +
          • Value of constant limited to [0..25-1]
          • +
          • Often use to access array elements +so needs to be >25=32> 2^{5} = 32
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    MIPS ISA designers compromise

    + +
      +
    • Keep all machine instructions format the same length
    • +
    • Consequence -> different formats for different kinds of MIPS +instructions +
        +
      • R-format for register +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          rs5 bits
          rt5 bits
          shamt5 bits
          func6 bits
          +
        • +
        +
      • +
      • I-format for immediate +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          rs5 bits
          rt5 bits
          Address/immediate16 bits
          +
        • +
        +
      • +
      • J-format for jump +
          +
        • + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          Target address26 bits
          +
        • +
        +
      • +
      +
    • +
    • opcode indicates the format of the instruction +
        +
      • This way, the hardware knows whether to treat the last half of the +instruction as 3 fields (R-format) or as 1 field (I-format)
      • +
      • Also, position of fields with same purpose are in the same location +in the 3 formats ☺
      • +
      +
    • +
    + +

    Summary

    + +
      +
    • Types of instruction sets: CISC and RISC
    • +
    • Looked at an example of a RISC instruction set: MIPS +
        +
      • Registers and Memory model
      • +
      • (Sub)set of instructions (assembly + machine instructions)
      • +
      • Function call conventions
      • +
      • Model of computation
      • +
      +
    • +
    • MIPS design principles +
        +
      1. Unambiguous binary encoding of instruction
      2. +
      3. (highlighted) IS functionally complete (“Turing complete”)
      4. +
      5. Machine instruction format -> only 3 of same length but of different format! + * R-format for register + * I-format for immediate + * J-format for jump
      6. +
      +
    • +
    • Also, position of fields with same purpose are in the same location in the 3 +formats ☺
    • +
    + +

    Next lecture

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design guidelines
      • +
      • Example of an instruction set: MIPS
      • +
      • (highlighted) Create our own instruction sets
      • +
      • (highlighted) ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/24/Lecture_24_ISA_MIPS.pdf b/_site/melody/cmpt-295/24/Lecture_24_ISA_MIPS.pdf new file mode 100644 index 0000000..33c99f7 Binary files /dev/null and b/_site/melody/cmpt-295/24/Lecture_24_ISA_MIPS.pdf differ diff --git a/_site/melody/cmpt-295/24/Lecture_24_ISA_MIPS_Annotated.pdf b/_site/melody/cmpt-295/24/Lecture_24_ISA_MIPS_Annotated.pdf new file mode 100644 index 0000000..1849c26 Binary files /dev/null and b/_site/melody/cmpt-295/24/Lecture_24_ISA_MIPS_Annotated.pdf differ diff --git a/_site/melody/cmpt-295/25/25.html b/_site/melody/cmpt-295/25/25.html new file mode 100644 index 0000000..6e06417 --- /dev/null +++ b/_site/melody/cmpt-295/25/25.html @@ -0,0 +1,1310 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Caption: THE #1 PROGRAMMER EXCUSE FOR LEGITIMATELY SLACKING OFF: 'MY CODE IS COMPILING.' Two stick figures standing on desk chairs fight eachother with lightsabers. From outside the open door, somebody yells 'HEY! GET BACK TO WORK!' One of the programers reponds: 'COMPILING!' The person from outside replies back 'OH. CARRY ON.'

    + +

    CMPT 295 - Unit - Instruction Set Architecture

    + +

    Lecture 25

    +
      +
    • ISA Design + Evaluation
    • +
    + +

    Last Lecture

    + +
      +
    • Looked at an example of a RISC instruction set: MIPS +
        +
      • From its Instruction Set Architecture (ISA): +
          +
        • Registers
        • +
        • Memory model
        • +
        • (Sub)set of instructions +
            +
          • Assembly instructions
          • +
          • Machine instructions +
              +
            • Format of R-format of a MIPS machine instruction +
                +
              • Size of its fields
              • +
              +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    Format of assembly instruction not +necessarily == format of machine instruction

    + +

    From last lecture!

    + +

    Example of an ISA: MIPS

    + +
      +
    • Function call conventions +
        +
      • caller saved registers (4-15, 24, 25)
      • +
      • callee saved registers (16-23, 30, 31)
      • +
      +
    • +
    • Model of Computation +
        +
      • Sequential
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register nameNumberUsage
    $zero0constant 0
    $at1reserved for assembler
    $v02expression evaluation and results of a function
    $v13expression evaluation and results of a function
    $a04argument 1
    $a15argument 2
    $a26argument 3
    $a37argument 4
    $t08temporary (not preserved across call)
    $t19temporary (not preserved across call)
    $t210temporary (not preserved across call)
    $t311temporary (not preserved across call)
    $t512temporary (not preserved across call)
    $t613temporary (not preserved across call)
    $t714temporary (not preserved across call)
    $s015saved temporary (preserved across call)
    $s116saved temporary (preserved across call)
    $s217saved temporary (preserved across call)
    $s318saved temporary (preserved across call)
    $s419saved temporary (preserved across call)
    $s520saved temporary (preserved across call)
    $s621saved temporary (preserved across call)
    $s722saved temporary (preserved across call)
    $t824temporary (not preserved across call)
    $t925temporary (not preserved across call)
    $k026reserved for OS kernel
    $k127reserved for OS kernel
    $gp28pointer to global area
    $sp29stack pointer
    $fp30frame pointer
    $ra31return address (used by function call)
    + +

    From last lecture! – MIPS - Design guidelines

    + +
      +
    • 3) In terms of machine instruction format: +
        +
      1. Create as few of them as possible
      2. +
      3. Have them all of the same length and same format!
      4. +
      5. If we have different machine instruction formats, then position the fields that +have the same purpose in the same location in the format +
          +
        • Can all MIPS machine instructions have the same length and same format? +
            +
          • For example: lw $s1, 20($s2)=> opcode, rs, rt, rd, shamt, func?
          • +
          • When designing its corresponding machine instruction … +
              +
            • Must specify source register using 5 bits -> OK!
            • +
            • Must specify destination register using 5 bits -> OK!
            • +
            • Must specify a constant using 5 bits -> Hum…
            • +
            +
          • +
          +
        • +
        +
          +
        • Value of constant limited to [0..25-1]
        • +
        • Often use to access array elements +so needs to be > 25 = 32
        • +
        +
      6. +
      +
    • +
    + +

    From last lecture! – MIPS ISA designers compromise

    + +
      +
    • Keep all machine instructions format the same length
    • +
    • Consequence -> different formats for different kinds of MIPS +instructions +
        +
      • R-format for register +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          rs5 bits
          rt5 bits
          shamt5 bits
          func6 bits
          +
        • +
        +
      • +
      • I-format for immediate +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          rs5 bits
          rt5 bits
          Address/immediate16 bits
          +
        • +
        +
      • +
      • J-format for jump +
          +
        • + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          Target address26 bits
          +
        • +
        +
      • +
      +
    • +
    • opcode indicates the format of the instruction +
        +
      • This way, the hardware knows whether to treat the last half of the +instruction as 3 fields (R-format) or as 1 field (I-format)
      • +
      • Also, position of fields with same purpose are in the same location +in the 3 formats ☺
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design guidelines
      • +
      • Example of an instruction set: MIPS
      • +
      • Create our own instruction sets
      • +
      • ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +

    Let’s design our own ISA - x295M (1 of 2)

    + +
      +
    • Registers and Memory model +
        +
      • # of registers -> 0 registers – model called “Memory Only” (except $sp)
      • +
      • Each memory address has -> 32 bits (m = 32)
      • +
      • Word size -> 32 bits
      • +
      • Byte-addressable memory +so address resolution -> n = 1 byte (8 bits)
      • +
      • Memory size -> 232×n2^{32} \times n -> 232×12^{32} \times 1 byte OR 232×82^{32} \times 8 bits
      • +
      +
    • +
    + +

    Let’s design our own ISA - x295M (2 of 2)

    + +
      +
    • (Sub)set of instructions
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    x295M assembly language instructionsSemantic (i.e., Meaning)Corresponding x295M machine instructions
    ADD a, b, cM[c] = M[a] + M[b]01 <30 bits> 00 <30 bits> 00 <30 bits>
    SUB a, b, cM[c] = M[a] - M[b]10 <30 bits> 00 <30 bits> 00 <30 bits>
    MUL a, b, cM[c] = M[a] * M[b]11 <30 bits> 00 <30 bits> 00 <30 bits>
    + +
      +
    • Memory addressing mode -> Direct (absolute), base + displacement and indirect
    • +
    • Operand model -> “3 Operand” model
    • +
    • Format of corresponding x295M machine instructions: +
        +
      • + + + + + + + + + + + + + + + + + + +
        01memory address of destword 1
        00memory address of src1word 2
        00memory address of src2word 3
        +
      • +
      +
    • +
    • Size of opcode -> 2 bits Size of operand -> 30 bits
    • +
    • Length of 1 instruction -> ___________ bits
    • +
    + +

    Note about the machine code format

    + +
      +
    • This ISA has two machine code formats:
    • +
    + +

    Format 1 (within a word):

    + + + + + + + + +
    opcodememory address
    + +

    Format 2 (within a word):

    + + + + + + + + +
    Xmemory address
    + +

    About Design guideline:

    + +

    3) In terms of machine +instruction format:

    + +
      +
    1. Create as few of them as +possible -> 2 formats
    2. +
    3. Have them all of the +same length -> 32 bits
    4. +
    5. Since we have two +different machine +instruction formats, fields +with same purpose are +positioned in the same +location in the 2 formats -> +
        +
      • operand field +(purpose -> memory +address) positioned in +the same location in +the 2 formats
      • +
      +
    6. +
    + +

    Evaluation of our ISA x295M versus MIPS

    + +

    Sample C code: z=(x+y)×(xy)z = (x + y) \times (x - y)

    + +
      +
    • C code -> Assembly code +
        +
      • ADD 0($sp), 4($sp), 12($sp)
      • +
      • SUB 0($sp), 4($sp), 16($sp)
      • +
      • MUL 12($sp), 16($sp), 8($sp)
      • +
      +
    • +
    • Meaning +
        +
      • M[$sp+12] = M[$sp+0] + M[$sp+4]
      • +
      • M[$sp+16] = M[$sp+0] – M[$sp+4]
      • +
      • M[$sp+8] = M[$sp+12] * M[$sp+16]
      • +
      +
    • +
    + +

    Evaluation of our ISA x295M versus MIPS

    + +

    Sample C code: z=(x+y)×(xy)z = (x + y) \times (x - y)

    + +

    Stack segment:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    MemoryStack Segment
      
      
      
      
      
      
    0x7fffff00<- $sp -> 
      
      
      
    + +

    Text Segment:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    MemoryText Segment
      
      
      
      
      
      
      
      
      
    0x00400000 -> 
    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OpcodeEncoding
    ADD01
    SUB10
    MUL11
    No op00
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Assembly codeMemory address/machine code pt. 1Memory address/machine code pt. 2Memory Address/machine code pt. 3
    ADD 0($sp),4($sp),12($sp)0x00400000 / 01 0x7fffff0c0x00400004 / 00 0x7fffff000x00400008 / 00 0x7fffff04
    SUB 0($sp),4($sp),16($sp)0x0040000c / 10 0x7fffff100x00400010 / 00 0x7fffff000x00400014 / 00 0x7fffff04
    MUL 12($sp),16($sp),8($sp)0x00400018 / 11 0x7fffff080x0040001c / 00 0x7fffff0c0x00400020 / 00 0x7fffff10
    + +

    Evaluation of our ISA x295M versus MIPS

    + +

    Sample C code: z=(x+y)×(xy)z = (x + y) \times (x - y)

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    C code -> Assembly codeMeaning
    lw $s1,0($sp)$s1 = M[$sp + 0]
    lw $s2,4($sp)$s2 = M[$sp + 4]
    add $a3,$s1,$s2$s3 = $s1 + $s2
    sub $s4,$s1,$s2$s4 = $s1 - $s2
    mul $s5,$s3,$s4$s5 = $s3 * $s4
    sw $s5,8($sp)M[$sp + 8] = $s5
    + +

    Evaluation of our ISA x295M versus MIPS

    + +

    Opcode table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Opcode + funcEncoding
    lw3510\text{35}_{10}
    sw4310\text{43}_{10}
    add0+32100 + \text{32}_{10}
    sub0+34100 + \text{34}_{10}
    mul0+36100 + \text{36}_{10}
    + + + +

    Register table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterNumber
    $s11710\text{17}_{10}
    $s21810\text{18}_{10}
    $s31910\text{19}_{10}
    $s42010\text{20}_{10}
    $s52110\text{21}_{10}
    $sp2910\text{29}_{10}
    + +

    Sample C code: z=(x+y)×(xy)z = (x + y) \times (x - y)

    + + + + + + + + + + + + + + + + + + +
    I-formatsrcdest 
    opcode 6 bitsrs 5 bitsrt 5 bitsAddress/immediate 16 bits
    + + + + + + + + + + + + + + + + + + + + + + +
    Assembly codeMachine code
    lw $s1,0($sp)100011 11101 10001 0x0000
    lw $s2,4($sp)100011 11101 10010 0x0004
    sw $s5,8($sp)101011 10101 11101 0x0008
    + + + + + + + + + + + + + + + + + + + + + + +
    R-formatsrc1src2dest  
    opcode 6 bitsrs 5 bitsrt 5 bitsrd 5 bitsshamt 5 bitsfunc 6 bits
    + + + + + + + + + + + + + + + + + + + + + + +
    Assembly codeMachine code
    add $s3,$s1,$s2000000 10001 10010 10011 00000 100000
    sub $s4,$s1,$s2000000 10001 10010 10100 00000 100010
    mul $s5,$s3,$s4000000 10011 10100 10101 00000 100100
    + +

    Evaluation of our ISA x295M versus MIPS

    + +

    Sample C code: z=(x+y)×(xy)z = (x + y) \times (x - y)

    + +

    x295M in machine code:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Memory address of each instructionat address+1+2+3+4+5+6+7
    0x0040000001 111111111111111111111100001100
    0x0040000400 111111111111111111111100000000
    0x0040000800 111111111111111111111100000100
    0x0040000c10 111111111111111111111100010000
    0x0040001000 111111111111111111111100000000
    0x0040001400 111111111111111111111100000100
    0x0040001811 111111111111111111111100001000
    0x0040001c00 111111111111111111111100001100
    0x0040002000 111111111111111111111100010000
    + +

    MIPS in machine code:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Memory address of each instructionMachine code
    0x00400000100011 11101 10001 0000 0000 0000 0000
    0x00400004100011 11101 10010 0000 0000 0000 0100
    0x00400008000000 10001 10010 10011 00000 100000
    0x0040000c000000 10001 10010 10100 00000 100010
    0x00400010000000 10011 10100 10101 00000 100100
    0x00400014101011 10101 11101 0000 0000 0000 0008
    + +

    Which criteria shall we use when comparing/evaluating ISAs?

    + +
      +
    • Whether or not the Instruction set (IS) design guidelines +have been satisfied: +
        +
      1. Each instruction of IS have an unambiguous binary encoding
      2. +
      3. IS is functionally complete -> i.e., it is “Turing complete”
      4. +
      5. In terms of machine instruction format: +
          +
        1. Create as few of them as possible
        2. +
        3. Have them all of the same length
        4. +
        5. If we have different machine instruction formats, then position +the fields that have the same purpose in the same location in +the format
        6. +
        +
      6. +
      +
    • +
    + +

    Which criteria shall we use when comparing/evaluating ISAs?

    + +
      +
    • Program performance -> usually measured using time +
        +
      • If an ISA design results in faster program execution then it is +deemed “better”
      • +
      +
    • +
    • What can affect the time a program takes to execute? +
        +
      • Since accessing memory is slow (slower than accessing +registers), the number of memory accesses a program does +will affect its execution time
      • +
      • Therefore, possible criteria: number of memory accesses
      • +
      • The fewer memory accesses our program makes, the faster it +executes, hence the “better” it is
      • +
      +
    • +
    + +

    Why is memory access slow!

    + +
      +
    • Memory access is the most time constraining aspect of +program execution
    • +
    • Why? Because of transfer rate limitation of the bus between +memory and CPU +
        +
      • Memory is “far away” from the CPU so it takes time to transfer +instructions and data from memory to microprocessor
      • +
      +
    • +
    • This is known as the von Neumann bottleneck
    • +
    • From the above diagram, we can gather that register +access is faster than memory access! Why?
    • +
    + +

    Diagram:

    + +

    A box labled CPU contains Registers, Condition Codes, PC. It is on the left. +A box labled Memory is on the right. +There is an arrow from CPU to Memory labled “Address Bus”. +There is a double-sided arrow from CPU and Memory, labled “Data Bus”. +Finally, there is an arrow from Memory to the CPU labled “Instruction Bus”.

    + +

    How is the von Neumann Bottleneck created?

    + +
      +
    • It is created when memory is accessed
    • +
    • During fetch stage +
        +
      • An instruction is retrieved from memory
      • +
      +
    • +
    • During decode/execute stages +
        +
      • The value of operands may be read from memory
      • +
      • The result may be written to memory
      • +
      +
    • +
    + +

    Evaluation of our ISA x295M versus MIPS

    + +
      +
    • Sample C code: z=(x+y)×(xy)z = (x + y) \times (x - y)
    • +
    • Let’s count the number of memory accesses:
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    x295Mfetchdecode/executeMIPSfetchdecode/execute
    ADD 0($sp), 4($sp), 12($sp) lw $s1,0($sp)   
    SUB 0($sp), 4($sp), 16($sp) lw $s2,4($sp)   
    MUL 12($sp), 16($sp), 8($sp) add $s3,$s1,$s2   
      sub $s4,$s1,$s2   
      mul $s5,$s3,$s4   
      sw $s5,8($sp)   
    + +

    Total: _____

    + +

    Summary

    + +
      +
    • ISA design +
        +
      • MIPS
      • +
      • Created our own x295M: “Memory only”
      • +
      +
    • +
    • ISA Evaluation +
        +
      • Examining the effect of the von Neumann bottleneck on the +execution time of our program by counting number of +memory accesses +
          +
        • The fewer memory accesses our program makes, the faster it +executes, hence the “better” it is
        • +
        +
      • +
      • Improvements: +
          +
        • Decreasing effect of von Neumann bottleneck by reducing +the number of memory accesses
        • +
        +
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design principles
      • +
      • Look at an example of an instruction set: MIPS
      • +
      • Create our own
      • +
      • ISA evaluation
      • +
      +
    • +
    • (highlighted) Implementation of a microprocessor (CPU) based on an ISA +
        +
      • (highlighted) Execution of machine instructions (datapath)
      • +
      • (highlighted) Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/25/25/index.html b/_site/melody/cmpt-295/25/25/index.html new file mode 100644 index 0000000..6e06417 --- /dev/null +++ b/_site/melody/cmpt-295/25/25/index.html @@ -0,0 +1,1310 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Caption: THE #1 PROGRAMMER EXCUSE FOR LEGITIMATELY SLACKING OFF: 'MY CODE IS COMPILING.' Two stick figures standing on desk chairs fight eachother with lightsabers. From outside the open door, somebody yells 'HEY! GET BACK TO WORK!' One of the programers reponds: 'COMPILING!' The person from outside replies back 'OH. CARRY ON.'

    + +

    CMPT 295 - Unit - Instruction Set Architecture

    + +

    Lecture 25

    +
      +
    • ISA Design + Evaluation
    • +
    + +

    Last Lecture

    + +
      +
    • Looked at an example of a RISC instruction set: MIPS +
        +
      • From its Instruction Set Architecture (ISA): +
          +
        • Registers
        • +
        • Memory model
        • +
        • (Sub)set of instructions +
            +
          • Assembly instructions
          • +
          • Machine instructions +
              +
            • Format of R-format of a MIPS machine instruction +
                +
              • Size of its fields
              • +
              +
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    Format of assembly instruction not +necessarily == format of machine instruction

    + +

    From last lecture!

    + +

    Example of an ISA: MIPS

    + +
      +
    • Function call conventions +
        +
      • caller saved registers (4-15, 24, 25)
      • +
      • callee saved registers (16-23, 30, 31)
      • +
      +
    • +
    • Model of Computation +
        +
      • Sequential
      • +
      +
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register nameNumberUsage
    $zero0constant 0
    $at1reserved for assembler
    $v02expression evaluation and results of a function
    $v13expression evaluation and results of a function
    $a04argument 1
    $a15argument 2
    $a26argument 3
    $a37argument 4
    $t08temporary (not preserved across call)
    $t19temporary (not preserved across call)
    $t210temporary (not preserved across call)
    $t311temporary (not preserved across call)
    $t512temporary (not preserved across call)
    $t613temporary (not preserved across call)
    $t714temporary (not preserved across call)
    $s015saved temporary (preserved across call)
    $s116saved temporary (preserved across call)
    $s217saved temporary (preserved across call)
    $s318saved temporary (preserved across call)
    $s419saved temporary (preserved across call)
    $s520saved temporary (preserved across call)
    $s621saved temporary (preserved across call)
    $s722saved temporary (preserved across call)
    $t824temporary (not preserved across call)
    $t925temporary (not preserved across call)
    $k026reserved for OS kernel
    $k127reserved for OS kernel
    $gp28pointer to global area
    $sp29stack pointer
    $fp30frame pointer
    $ra31return address (used by function call)
    + +

    From last lecture! – MIPS - Design guidelines

    + +
      +
    • 3) In terms of machine instruction format: +
        +
      1. Create as few of them as possible
      2. +
      3. Have them all of the same length and same format!
      4. +
      5. If we have different machine instruction formats, then position the fields that +have the same purpose in the same location in the format +
          +
        • Can all MIPS machine instructions have the same length and same format? +
            +
          • For example: lw $s1, 20($s2)=> opcode, rs, rt, rd, shamt, func?
          • +
          • When designing its corresponding machine instruction … +
              +
            • Must specify source register using 5 bits -> OK!
            • +
            • Must specify destination register using 5 bits -> OK!
            • +
            • Must specify a constant using 5 bits -> Hum…
            • +
            +
          • +
          +
        • +
        +
          +
        • Value of constant limited to [0..25-1]
        • +
        • Often use to access array elements +so needs to be > 25 = 32
        • +
        +
      6. +
      +
    • +
    + +

    From last lecture! – MIPS ISA designers compromise

    + +
      +
    • Keep all machine instructions format the same length
    • +
    • Consequence -> different formats for different kinds of MIPS +instructions +
        +
      • R-format for register +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          rs5 bits
          rt5 bits
          shamt5 bits
          func6 bits
          +
        • +
        +
      • +
      • I-format for immediate +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          rs5 bits
          rt5 bits
          Address/immediate16 bits
          +
        • +
        +
      • +
      • J-format for jump +
          +
        • + + + + + + + + + + + + + + + + + +
          LabelSize
          opcode6 bits
          Target address26 bits
          +
        • +
        +
      • +
      +
    • +
    • opcode indicates the format of the instruction +
        +
      • This way, the hardware knows whether to treat the last half of the +instruction as 3 fields (R-format) or as 1 field (I-format)
      • +
      • Also, position of fields with same purpose are in the same location +in the 3 formats ☺
      • +
      +
    • +
    + +

    Today’s Menu

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design guidelines
      • +
      • Example of an instruction set: MIPS
      • +
      • Create our own instruction sets
      • +
      • ISA evaluation
      • +
      +
    • +
    • Implementation of a microprocessor (CPU) based on an ISA +
        +
      • Execution of machine instructions (datapath)
      • +
      • Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +

    Let’s design our own ISA - x295M (1 of 2)

    + +
      +
    • Registers and Memory model +
        +
      • # of registers -> 0 registers – model called “Memory Only” (except $sp)
      • +
      • Each memory address has -> 32 bits (m = 32)
      • +
      • Word size -> 32 bits
      • +
      • Byte-addressable memory +so address resolution -> n = 1 byte (8 bits)
      • +
      • Memory size -> 232×n2^{32} \times n -> 232×12^{32} \times 1 byte OR 232×82^{32} \times 8 bits
      • +
      +
    • +
    + +

    Let’s design our own ISA - x295M (2 of 2)

    + +
      +
    • (Sub)set of instructions
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    x295M assembly language instructionsSemantic (i.e., Meaning)Corresponding x295M machine instructions
    ADD a, b, cM[c] = M[a] + M[b]01 <30 bits> 00 <30 bits> 00 <30 bits>
    SUB a, b, cM[c] = M[a] - M[b]10 <30 bits> 00 <30 bits> 00 <30 bits>
    MUL a, b, cM[c] = M[a] * M[b]11 <30 bits> 00 <30 bits> 00 <30 bits>
    + +
      +
    • Memory addressing mode -> Direct (absolute), base + displacement and indirect
    • +
    • Operand model -> “3 Operand” model
    • +
    • Format of corresponding x295M machine instructions: +
        +
      • + + + + + + + + + + + + + + + + + + +
        01memory address of destword 1
        00memory address of src1word 2
        00memory address of src2word 3
        +
      • +
      +
    • +
    • Size of opcode -> 2 bits Size of operand -> 30 bits
    • +
    • Length of 1 instruction -> ___________ bits
    • +
    + +

    Note about the machine code format

    + +
      +
    • This ISA has two machine code formats:
    • +
    + +

    Format 1 (within a word):

    + + + + + + + + +
    opcodememory address
    + +

    Format 2 (within a word):

    + + + + + + + + +
    Xmemory address
    + +

    About Design guideline:

    + +

    3) In terms of machine +instruction format:

    + +
      +
    1. Create as few of them as +possible -> 2 formats
    2. +
    3. Have them all of the +same length -> 32 bits
    4. +
    5. Since we have two +different machine +instruction formats, fields +with same purpose are +positioned in the same +location in the 2 formats -> +
        +
      • operand field +(purpose -> memory +address) positioned in +the same location in +the 2 formats
      • +
      +
    6. +
    + +

    Evaluation of our ISA x295M versus MIPS

    + +

    Sample C code: z=(x+y)×(xy)z = (x + y) \times (x - y)

    + +
      +
    • C code -> Assembly code +
        +
      • ADD 0($sp), 4($sp), 12($sp)
      • +
      • SUB 0($sp), 4($sp), 16($sp)
      • +
      • MUL 12($sp), 16($sp), 8($sp)
      • +
      +
    • +
    • Meaning +
        +
      • M[$sp+12] = M[$sp+0] + M[$sp+4]
      • +
      • M[$sp+16] = M[$sp+0] – M[$sp+4]
      • +
      • M[$sp+8] = M[$sp+12] * M[$sp+16]
      • +
      +
    • +
    + +

    Evaluation of our ISA x295M versus MIPS

    + +

    Sample C code: z=(x+y)×(xy)z = (x + y) \times (x - y)

    + +

    Stack segment:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    MemoryStack Segment
      
      
      
      
      
      
    0x7fffff00<- $sp -> 
      
      
      
    + +

    Text Segment:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    MemoryText Segment
      
      
      
      
      
      
      
      
      
    0x00400000 -> 
    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OpcodeEncoding
    ADD01
    SUB10
    MUL11
    No op00
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Assembly codeMemory address/machine code pt. 1Memory address/machine code pt. 2Memory Address/machine code pt. 3
    ADD 0($sp),4($sp),12($sp)0x00400000 / 01 0x7fffff0c0x00400004 / 00 0x7fffff000x00400008 / 00 0x7fffff04
    SUB 0($sp),4($sp),16($sp)0x0040000c / 10 0x7fffff100x00400010 / 00 0x7fffff000x00400014 / 00 0x7fffff04
    MUL 12($sp),16($sp),8($sp)0x00400018 / 11 0x7fffff080x0040001c / 00 0x7fffff0c0x00400020 / 00 0x7fffff10
    + +

    Evaluation of our ISA x295M versus MIPS

    + +

    Sample C code: z=(x+y)×(xy)z = (x + y) \times (x - y)

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    C code -> Assembly codeMeaning
    lw $s1,0($sp)$s1 = M[$sp + 0]
    lw $s2,4($sp)$s2 = M[$sp + 4]
    add $a3,$s1,$s2$s3 = $s1 + $s2
    sub $s4,$s1,$s2$s4 = $s1 - $s2
    mul $s5,$s3,$s4$s5 = $s3 * $s4
    sw $s5,8($sp)M[$sp + 8] = $s5
    + +

    Evaluation of our ISA x295M versus MIPS

    + +

    Opcode table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Opcode + funcEncoding
    lw3510\text{35}_{10}
    sw4310\text{43}_{10}
    add0+32100 + \text{32}_{10}
    sub0+34100 + \text{34}_{10}
    mul0+36100 + \text{36}_{10}
    + + + +

    Register table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterNumber
    $s11710\text{17}_{10}
    $s21810\text{18}_{10}
    $s31910\text{19}_{10}
    $s42010\text{20}_{10}
    $s52110\text{21}_{10}
    $sp2910\text{29}_{10}
    + +

    Sample C code: z=(x+y)×(xy)z = (x + y) \times (x - y)

    + + + + + + + + + + + + + + + + + + +
    I-formatsrcdest 
    opcode 6 bitsrs 5 bitsrt 5 bitsAddress/immediate 16 bits
    + + + + + + + + + + + + + + + + + + + + + + +
    Assembly codeMachine code
    lw $s1,0($sp)100011 11101 10001 0x0000
    lw $s2,4($sp)100011 11101 10010 0x0004
    sw $s5,8($sp)101011 10101 11101 0x0008
    + + + + + + + + + + + + + + + + + + + + + + +
    R-formatsrc1src2dest  
    opcode 6 bitsrs 5 bitsrt 5 bitsrd 5 bitsshamt 5 bitsfunc 6 bits
    + + + + + + + + + + + + + + + + + + + + + + +
    Assembly codeMachine code
    add $s3,$s1,$s2000000 10001 10010 10011 00000 100000
    sub $s4,$s1,$s2000000 10001 10010 10100 00000 100010
    mul $s5,$s3,$s4000000 10011 10100 10101 00000 100100
    + +

    Evaluation of our ISA x295M versus MIPS

    + +

    Sample C code: z=(x+y)×(xy)z = (x + y) \times (x - y)

    + +

    x295M in machine code:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Memory address of each instructionat address+1+2+3+4+5+6+7
    0x0040000001 111111111111111111111100001100
    0x0040000400 111111111111111111111100000000
    0x0040000800 111111111111111111111100000100
    0x0040000c10 111111111111111111111100010000
    0x0040001000 111111111111111111111100000000
    0x0040001400 111111111111111111111100000100
    0x0040001811 111111111111111111111100001000
    0x0040001c00 111111111111111111111100001100
    0x0040002000 111111111111111111111100010000
    + +

    MIPS in machine code:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Memory address of each instructionMachine code
    0x00400000100011 11101 10001 0000 0000 0000 0000
    0x00400004100011 11101 10010 0000 0000 0000 0100
    0x00400008000000 10001 10010 10011 00000 100000
    0x0040000c000000 10001 10010 10100 00000 100010
    0x00400010000000 10011 10100 10101 00000 100100
    0x00400014101011 10101 11101 0000 0000 0000 0008
    + +

    Which criteria shall we use when comparing/evaluating ISAs?

    + +
      +
    • Whether or not the Instruction set (IS) design guidelines +have been satisfied: +
        +
      1. Each instruction of IS have an unambiguous binary encoding
      2. +
      3. IS is functionally complete -> i.e., it is “Turing complete”
      4. +
      5. In terms of machine instruction format: +
          +
        1. Create as few of them as possible
        2. +
        3. Have them all of the same length
        4. +
        5. If we have different machine instruction formats, then position +the fields that have the same purpose in the same location in +the format
        6. +
        +
      6. +
      +
    • +
    + +

    Which criteria shall we use when comparing/evaluating ISAs?

    + +
      +
    • Program performance -> usually measured using time +
        +
      • If an ISA design results in faster program execution then it is +deemed “better”
      • +
      +
    • +
    • What can affect the time a program takes to execute? +
        +
      • Since accessing memory is slow (slower than accessing +registers), the number of memory accesses a program does +will affect its execution time
      • +
      • Therefore, possible criteria: number of memory accesses
      • +
      • The fewer memory accesses our program makes, the faster it +executes, hence the “better” it is
      • +
      +
    • +
    + +

    Why is memory access slow!

    + +
      +
    • Memory access is the most time constraining aspect of +program execution
    • +
    • Why? Because of transfer rate limitation of the bus between +memory and CPU +
        +
      • Memory is “far away” from the CPU so it takes time to transfer +instructions and data from memory to microprocessor
      • +
      +
    • +
    • This is known as the von Neumann bottleneck
    • +
    • From the above diagram, we can gather that register +access is faster than memory access! Why?
    • +
    + +

    Diagram:

    + +

    A box labled CPU contains Registers, Condition Codes, PC. It is on the left. +A box labled Memory is on the right. +There is an arrow from CPU to Memory labled “Address Bus”. +There is a double-sided arrow from CPU and Memory, labled “Data Bus”. +Finally, there is an arrow from Memory to the CPU labled “Instruction Bus”.

    + +

    How is the von Neumann Bottleneck created?

    + +
      +
    • It is created when memory is accessed
    • +
    • During fetch stage +
        +
      • An instruction is retrieved from memory
      • +
      +
    • +
    • During decode/execute stages +
        +
      • The value of operands may be read from memory
      • +
      • The result may be written to memory
      • +
      +
    • +
    + +

    Evaluation of our ISA x295M versus MIPS

    + +
      +
    • Sample C code: z=(x+y)×(xy)z = (x + y) \times (x - y)
    • +
    • Let’s count the number of memory accesses:
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    x295Mfetchdecode/executeMIPSfetchdecode/execute
    ADD 0($sp), 4($sp), 12($sp) lw $s1,0($sp)   
    SUB 0($sp), 4($sp), 16($sp) lw $s2,4($sp)   
    MUL 12($sp), 16($sp), 8($sp) add $s3,$s1,$s2   
      sub $s4,$s1,$s2   
      mul $s5,$s3,$s4   
      sw $s5,8($sp)   
    + +

    Total: _____

    + +

    Summary

    + +
      +
    • ISA design +
        +
      • MIPS
      • +
      • Created our own x295M: “Memory only”
      • +
      +
    • +
    • ISA Evaluation +
        +
      • Examining the effect of the von Neumann bottleneck on the +execution time of our program by counting number of +memory accesses +
          +
        • The fewer memory accesses our program makes, the faster it +executes, hence the “better” it is
        • +
        +
      • +
      • Improvements: +
          +
        • Decreasing effect of von Neumann bottleneck by reducing +the number of memory accesses
        • +
        +
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design principles
      • +
      • Look at an example of an instruction set: MIPS
      • +
      • Create our own
      • +
      • ISA evaluation
      • +
      +
    • +
    • (highlighted) Implementation of a microprocessor (CPU) based on an ISA +
        +
      • (highlighted) Execution of machine instructions (datapath)
      • +
      • (highlighted) Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/25/Lecture_25_ISA_Evaluation.pdf b/_site/melody/cmpt-295/25/Lecture_25_ISA_Evaluation.pdf new file mode 100644 index 0000000..87f03dd Binary files /dev/null and b/_site/melody/cmpt-295/25/Lecture_25_ISA_Evaluation.pdf differ diff --git a/_site/melody/cmpt-295/26/26.html b/_site/melody/cmpt-295/26/26.html new file mode 100644 index 0000000..9bddcbc --- /dev/null +++ b/_site/melody/cmpt-295/26/26.html @@ -0,0 +1,412 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 - Unit – Microprocessor Design & Instruction Execution

    + +

    Lecture 26

    + +
      +
    • Intro to Logic Design
    • +
    + +

    Last Lecture

    + +
      +
    • ISA design +
        +
      • MIPS
      • +
      • Created our own x295M: “Memory only”
      • +
      +
    • +
    • ISA Evaluation +
        +
      • Examining the effect of the von Neumann bottleneck on the +execution time of our program by counting number of +memory accesses +
          +
        • The fewer memory accesses our program makes, the faster it +executes, hence the “better” it is
        • +
        +
      • +
      +
    • +
    • Improvements: +
        +
      • Decreasing effect of von Neumann bottleneck by reducing +the number of memory accesses
      • +
      +
    • +
    + +

    And one way to achieve this is by introducing registers in the design of the ISA -> makes machine instructions shorter. +Reducing the number of operands may also help (but not always).

    + +

    Today’s Menu

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design principles
      • +
      • Look at an example of an instruction set: MIPS
      • +
      • Create our own
      • +
      • ISA evaluation
      • +
      +
    • +
    • (highlighted) Implementation of a microprocessor (CPU) based on an ISA +
        +
      • (highlighted) Execution of machine instructions (datapath)
      • +
      • (highlighted) Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +

    The Big Picture

    + +
      +
    • C code: +
        +
      • C Program (.c) -> sum_store.c
      • +
      • C Preprocessor creates: Preprocessed Source -> sum_store.i
      • +
      +
    • +
    • Assembly code: +
        +
      • C compiler creates: Assembly program (.s) -> sum_store.s
      • +
      +
    • +
    • Machine code: +
        +
      • Linker grates: Object (.o) -> sum_store.o
      • +
      +
    • +
    • ISA - Instruction Set Architecture: An agreement establishing how software communicates with CPU. +
        +
      • Loader creates: Executable -> ss
      • +
      • Computer executed it +
          +
        • CPU
        • +
        • Memory
        • +
        +
      • +
      +
    • +
    + +

    Now that we have had a +look at a few instruction +set architectures (ISA), i.e.,

    + +
      +
    • Specification of various +models (memory model, +computational model, +operand model, etc … ), +and …
    • +
    • Design of instruction set (or +subset) (assembly +instructions and their +corresponding machine +instructions, their formats, +etc. ) …
    • +
    + +

    … let’s step over this +interface and explore +how the microprocessor is +constructed so it can +execute these machine +4 instructions

    + +

    Datapath of a MIPS microprocessor

    + +

    Sneak preview (how CPU hardware updates +the PC in fetch-decode-execute loop): +0x00400000 +(from our Lecture 25)

    + +
      +
    • Consider the memory address +0x00400000 i.e., +0000 0000 0100 0000 0000 0000 0000 0000 +which holds the MIPS machine instruction +100011 11101 10001 0000000000000000
    • +
    • PC contains 0x00400000 i.e., +32 line bus with signals 0x00400000 input +into ADDER with “4” as other input
    • +
    • ADDER adds both inputs and produces +0x00400004 i.e., memory address of +next machine instruction which holds +the MIPS machine instruction +100011 11101 10010 0000000000000100 32 line bus with signal 0x00400004 input into +PC overwriting 0x00400000
    • +
    + +

    See micro_graph.html

    + +

    Source: Page 246 in Patterson and Hennessy

    + +

    Digital circuits

    + +
      +
    • In order to understand how the microprocessor executes these machine instructions (series of 0’s and 1’s), we need to have a look at the components of a microprocessor and how they function: Types of components found in a digital system such as a microprocessor +
        +
      1. Combinational logic -> manipulate bits (compute functions on +bits e.g., ADD)
      2. +
      3. Memory elements -> store bits
      4. +
      5. Clock signals -> regulate the update of memory elements
      6. +
      +
    • +
    • And what affects the execution speed of these components +such as propagation delay
    • +
    • So, we need to understand a few things about digital circuits
    • +
    + +

    Microprocessor

    + +
      +
    • Made of resistors, +capacitors, diodes, +and transistors
    • +
    • For example, 10-core Core i7 +Broadwell-E (2016) from Intel +contains 3,200,000,000 transistors +Source: https://en.wikipedia.org/wiki/Transistor_count
    • +
    • Lots of incredibly small components
    • +
    + +

    Source: https://www.newegg.ca/Product/Product.aspx

    + +

    Source: https://www.elprocus.com/semiconductor-devices-types-and-applications

    + +

    Logic gates

    + +
      +
    • Definition: A logic gate is an +electronic device that can +perform a Boolean function +(AND, NAND, OR, XOR, NOT)
    • +
    • On a chip, these can be +made using transistors, +resistors, and diodes
    • +
    • Here is how a NOT logic gate +is constructed:
    • +
    + +

    (Transcriber’s note: I’m sorry but I have no fucking idea how to transcribe this diagram. +It is way too complex to describe with plain text, +and contains special symbols for electrical components I don’t recognize.)

    + +

    Source: http://www.cybermike.net/reference/liec_book/Digital/DIGI_3.html

    + +

    from Quora: 2.5 transistors/logic gate ∴ 640 millions [sic] to ~2 billions [sic] logic gate on 1 microprocessor!

    + +

    Behaviour of logic gates

    + +
      +
    • Here is a diagram representing +the behaviour of an AND logic…both switches must be closed. +
        +
      • A positive and negative terminal are connected to a lamp. On the positive side, between the terminal and the lamp are two gates in series. They are labeled A and B. They are both open. This means they are not connected to the wire.
      • +
      +
    • +
    • Here is a diagram representing +the behaviour of an OR logic…either (or both) switch must be closed!
    • +
    • A positive and negative terminal are connected to a lamp. On the positive side, between the terminal and the lamp are two gates in parallel, where the line splits in two. They are labeled A and B. They are both open. This means they are not connected to the wire.
    • +
    + +

    https://www.electronics-tutorials.ws/boolean/bool_1.html

    + +

    https://www.electronics-tutorials.ws/boolean/bool_2.html

    + +

    Abstracting using black boxes

    + +
      +
    • A black box is used to abstract the function of a device +
        +
      • The input and output of the device are visible/known +
          +
        • The idea is that we need to know these in order to use +the device
        • +
        +
      • +
      • The implementation of the devide (what is inside) is +invisible/unknown, i.e., hidden +
          +
        • The idea is that we do not need to know how the +device is implemented in order to use it
        • +
        +
      • +
      +
    • +
    • Same thing is true for functions in software!
    • +
    + +

    Abstracting logic gates

    + +
      +
    • Instead of drawing logic gates using their electronic components, +we hide these components using a black box -> a symbol +simplified representing a logic gate
    • +
    • Symbols: +
        +
      • AND +
          +
        • Inputs: A, B
        • +
        • Outputs: AB
        • +
        +
      • +
      • OR +
          +
        • Inputs: A, B
        • +
        • Outputs: A+B
        • +
        +
      • +
      • XOR +
          +
        • Inputs: A, B
        • +
        • Outputs: A⊕B
        • +
        +
      • +
      +
    • +
    • Input: a signal i.e., 0 or 1 (abstraction of voltage levels) travels +along the input wire/line
    • +
    • Output: After a time delay (propagation delay tpd), a signal, i.e., 0 +or 1 travels along the output wire/line
    • +
    • Always active +
        +
      • As soon as signal (0 or 1) travels along the input wires/lines, the +logical gate produces a result, i.e., a signal (0 or 1) which then travels +along the output wire/line
      • +
      +
    • +
    + +

    Abstracting logic gates – cont’d

    + +

    (Annotation: Homework: Label input & output line.)

    + +
      +
    • Symbols: +
        +
      • NAND +
          +
        • Inputs: A, B
        • +
        • Outputs: AB\overline{AB}
        • +
        +
      • +
      • NOT (inverter) +
          +
        • Inputs: A
        • +
        • Outputs: A\overline{A}
        • +
        +
      • +
      • NOR +
          +
        • Inputs: A, B
        • +
        • Outputs: A+B\overline{A+B}
        • +
        +
      • +
      • BUFFER +
          +
        • Inputs: A
        • +
        • Outputs: A
        • +
        +
      • +
      +
    • +
    + +

    Purpose of buffer gate: to buffer and/or delay and/or strengthen a signal

    + +

    Propagation delay tpdt_{\text{pd}}

    + +
      +
    • Definition: Longest time elapsed between the +application of an input and the occurrence of the +corresponding output
    • +
    • tpdt_{\text{pd}} often expressed in picosecond (101210^{-12} seconds) +to nanosecond (10910^{-9} seconds)
    • +
    + +

    Summary

    + +
      +
    • We have now started to explore how the microprocessor +executes machine instructions (series of 0’s and 1’s) +
        +
      • More specifically, how its datapath can be constructed
      • +
      +
    • +
    • Microprocessor itself is … +
        +
      • Made of resistors, capacitors, diodes, and transistors
      • +
      • Billions of them, so understanding their behaviours (what they +do) once they are linked together is too onerous
      • +
      • So we resort to abstraction (black box) in order to understand +their functioning +
          +
        • Logic gates: perform a Boolean function
        • +
        +
      • +
      • Hardware components (i.e., logic gates) have propagation +delay +
          +
        • Signals (0’s and 1’s) take time to propagate through them
        • +
        +
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design principles
      • +
      • Look at an example of an instruction set: MIPS
      • +
      • Create our own
      • +
      • ISA evaluation
      • +
      +
    • +
    • (highlighted) Implementation of a microprocessor (CPU) based on an ISA +
        +
      • (highlighted) Execution of machine instructions (datapath)
      • +
      • (highlighted) Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/26/26/index.html b/_site/melody/cmpt-295/26/26/index.html new file mode 100644 index 0000000..9bddcbc --- /dev/null +++ b/_site/melody/cmpt-295/26/26/index.html @@ -0,0 +1,412 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 - Unit – Microprocessor Design & Instruction Execution

    + +

    Lecture 26

    + +
      +
    • Intro to Logic Design
    • +
    + +

    Last Lecture

    + +
      +
    • ISA design +
        +
      • MIPS
      • +
      • Created our own x295M: “Memory only”
      • +
      +
    • +
    • ISA Evaluation +
        +
      • Examining the effect of the von Neumann bottleneck on the +execution time of our program by counting number of +memory accesses +
          +
        • The fewer memory accesses our program makes, the faster it +executes, hence the “better” it is
        • +
        +
      • +
      +
    • +
    • Improvements: +
        +
      • Decreasing effect of von Neumann bottleneck by reducing +the number of memory accesses
      • +
      +
    • +
    + +

    And one way to achieve this is by introducing registers in the design of the ISA -> makes machine instructions shorter. +Reducing the number of operands may also help (but not always).

    + +

    Today’s Menu

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design principles
      • +
      • Look at an example of an instruction set: MIPS
      • +
      • Create our own
      • +
      • ISA evaluation
      • +
      +
    • +
    • (highlighted) Implementation of a microprocessor (CPU) based on an ISA +
        +
      • (highlighted) Execution of machine instructions (datapath)
      • +
      • (highlighted) Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + +

    The Big Picture

    + +
      +
    • C code: +
        +
      • C Program (.c) -> sum_store.c
      • +
      • C Preprocessor creates: Preprocessed Source -> sum_store.i
      • +
      +
    • +
    • Assembly code: +
        +
      • C compiler creates: Assembly program (.s) -> sum_store.s
      • +
      +
    • +
    • Machine code: +
        +
      • Linker grates: Object (.o) -> sum_store.o
      • +
      +
    • +
    • ISA - Instruction Set Architecture: An agreement establishing how software communicates with CPU. +
        +
      • Loader creates: Executable -> ss
      • +
      • Computer executed it +
          +
        • CPU
        • +
        • Memory
        • +
        +
      • +
      +
    • +
    + +

    Now that we have had a +look at a few instruction +set architectures (ISA), i.e.,

    + +
      +
    • Specification of various +models (memory model, +computational model, +operand model, etc … ), +and …
    • +
    • Design of instruction set (or +subset) (assembly +instructions and their +corresponding machine +instructions, their formats, +etc. ) …
    • +
    + +

    … let’s step over this +interface and explore +how the microprocessor is +constructed so it can +execute these machine +4 instructions

    + +

    Datapath of a MIPS microprocessor

    + +

    Sneak preview (how CPU hardware updates +the PC in fetch-decode-execute loop): +0x00400000 +(from our Lecture 25)

    + +
      +
    • Consider the memory address +0x00400000 i.e., +0000 0000 0100 0000 0000 0000 0000 0000 +which holds the MIPS machine instruction +100011 11101 10001 0000000000000000
    • +
    • PC contains 0x00400000 i.e., +32 line bus with signals 0x00400000 input +into ADDER with “4” as other input
    • +
    • ADDER adds both inputs and produces +0x00400004 i.e., memory address of +next machine instruction which holds +the MIPS machine instruction +100011 11101 10010 0000000000000100 32 line bus with signal 0x00400004 input into +PC overwriting 0x00400000
    • +
    + +

    See micro_graph.html

    + +

    Source: Page 246 in Patterson and Hennessy

    + +

    Digital circuits

    + +
      +
    • In order to understand how the microprocessor executes these machine instructions (series of 0’s and 1’s), we need to have a look at the components of a microprocessor and how they function: Types of components found in a digital system such as a microprocessor +
        +
      1. Combinational logic -> manipulate bits (compute functions on +bits e.g., ADD)
      2. +
      3. Memory elements -> store bits
      4. +
      5. Clock signals -> regulate the update of memory elements
      6. +
      +
    • +
    • And what affects the execution speed of these components +such as propagation delay
    • +
    • So, we need to understand a few things about digital circuits
    • +
    + +

    Microprocessor

    + +
      +
    • Made of resistors, +capacitors, diodes, +and transistors
    • +
    • For example, 10-core Core i7 +Broadwell-E (2016) from Intel +contains 3,200,000,000 transistors +Source: https://en.wikipedia.org/wiki/Transistor_count
    • +
    • Lots of incredibly small components
    • +
    + +

    Source: https://www.newegg.ca/Product/Product.aspx

    + +

    Source: https://www.elprocus.com/semiconductor-devices-types-and-applications

    + +

    Logic gates

    + +
      +
    • Definition: A logic gate is an +electronic device that can +perform a Boolean function +(AND, NAND, OR, XOR, NOT)
    • +
    • On a chip, these can be +made using transistors, +resistors, and diodes
    • +
    • Here is how a NOT logic gate +is constructed:
    • +
    + +

    (Transcriber’s note: I’m sorry but I have no fucking idea how to transcribe this diagram. +It is way too complex to describe with plain text, +and contains special symbols for electrical components I don’t recognize.)

    + +

    Source: http://www.cybermike.net/reference/liec_book/Digital/DIGI_3.html

    + +

    from Quora: 2.5 transistors/logic gate ∴ 640 millions [sic] to ~2 billions [sic] logic gate on 1 microprocessor!

    + +

    Behaviour of logic gates

    + +
      +
    • Here is a diagram representing +the behaviour of an AND logic…both switches must be closed. +
        +
      • A positive and negative terminal are connected to a lamp. On the positive side, between the terminal and the lamp are two gates in series. They are labeled A and B. They are both open. This means they are not connected to the wire.
      • +
      +
    • +
    • Here is a diagram representing +the behaviour of an OR logic…either (or both) switch must be closed!
    • +
    • A positive and negative terminal are connected to a lamp. On the positive side, between the terminal and the lamp are two gates in parallel, where the line splits in two. They are labeled A and B. They are both open. This means they are not connected to the wire.
    • +
    + +

    https://www.electronics-tutorials.ws/boolean/bool_1.html

    + +

    https://www.electronics-tutorials.ws/boolean/bool_2.html

    + +

    Abstracting using black boxes

    + +
      +
    • A black box is used to abstract the function of a device +
        +
      • The input and output of the device are visible/known +
          +
        • The idea is that we need to know these in order to use +the device
        • +
        +
      • +
      • The implementation of the devide (what is inside) is +invisible/unknown, i.e., hidden +
          +
        • The idea is that we do not need to know how the +device is implemented in order to use it
        • +
        +
      • +
      +
    • +
    • Same thing is true for functions in software!
    • +
    + +

    Abstracting logic gates

    + +
      +
    • Instead of drawing logic gates using their electronic components, +we hide these components using a black box -> a symbol +simplified representing a logic gate
    • +
    • Symbols: +
        +
      • AND +
          +
        • Inputs: A, B
        • +
        • Outputs: AB
        • +
        +
      • +
      • OR +
          +
        • Inputs: A, B
        • +
        • Outputs: A+B
        • +
        +
      • +
      • XOR +
          +
        • Inputs: A, B
        • +
        • Outputs: A⊕B
        • +
        +
      • +
      +
    • +
    • Input: a signal i.e., 0 or 1 (abstraction of voltage levels) travels +along the input wire/line
    • +
    • Output: After a time delay (propagation delay tpd), a signal, i.e., 0 +or 1 travels along the output wire/line
    • +
    • Always active +
        +
      • As soon as signal (0 or 1) travels along the input wires/lines, the +logical gate produces a result, i.e., a signal (0 or 1) which then travels +along the output wire/line
      • +
      +
    • +
    + +

    Abstracting logic gates – cont’d

    + +

    (Annotation: Homework: Label input & output line.)

    + +
      +
    • Symbols: +
        +
      • NAND +
          +
        • Inputs: A, B
        • +
        • Outputs: AB\overline{AB}
        • +
        +
      • +
      • NOT (inverter) +
          +
        • Inputs: A
        • +
        • Outputs: A\overline{A}
        • +
        +
      • +
      • NOR +
          +
        • Inputs: A, B
        • +
        • Outputs: A+B\overline{A+B}
        • +
        +
      • +
      • BUFFER +
          +
        • Inputs: A
        • +
        • Outputs: A
        • +
        +
      • +
      +
    • +
    + +

    Purpose of buffer gate: to buffer and/or delay and/or strengthen a signal

    + +

    Propagation delay tpdt_{\text{pd}}

    + +
      +
    • Definition: Longest time elapsed between the +application of an input and the occurrence of the +corresponding output
    • +
    • tpdt_{\text{pd}} often expressed in picosecond (101210^{-12} seconds) +to nanosecond (10910^{-9} seconds)
    • +
    + +

    Summary

    + +
      +
    • We have now started to explore how the microprocessor +executes machine instructions (series of 0’s and 1’s) +
        +
      • More specifically, how its datapath can be constructed
      • +
      +
    • +
    • Microprocessor itself is … +
        +
      • Made of resistors, capacitors, diodes, and transistors
      • +
      • Billions of them, so understanding their behaviours (what they +do) once they are linked together is too onerous
      • +
      • So we resort to abstraction (black box) in order to understand +their functioning +
          +
        • Logic gates: perform a Boolean function
        • +
        +
      • +
      • Hardware components (i.e., logic gates) have propagation +delay +
          +
        • Signals (0’s and 1’s) take time to propagate through them
        • +
        +
      • +
      +
    • +
    + +

    Next Lecture

    + +
      +
    • Instruction Set Architecture (ISA) +
        +
      • Definition of ISA
      • +
      +
    • +
    • Instruction Set design +
        +
      • Design principles
      • +
      • Look at an example of an instruction set: MIPS
      • +
      • Create our own
      • +
      • ISA evaluation
      • +
      +
    • +
    • (highlighted) Implementation of a microprocessor (CPU) based on an ISA +
        +
      • (highlighted) Execution of machine instructions (datapath)
      • +
      • (highlighted) Intro to logic design + Combinational logic + Sequential logic circuit
      • +
      • Sequential execution of machine instructions
      • +
      • Pipelined execution of machine instructions + Hazards
      • +
      +
    • +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/26/Lecture_26_Intro_to_Logic_Design.pdf b/_site/melody/cmpt-295/26/Lecture_26_Intro_to_Logic_Design.pdf new file mode 100644 index 0000000..0665976 Binary files /dev/null and b/_site/melody/cmpt-295/26/Lecture_26_Intro_to_Logic_Design.pdf differ diff --git a/_site/melody/cmpt-295/26/Lecture_26_Intro_to_Logic_Design_Annotated.pdf b/_site/melody/cmpt-295/26/Lecture_26_Intro_to_Logic_Design_Annotated.pdf new file mode 100644 index 0000000..bfa658e Binary files /dev/null and b/_site/melody/cmpt-295/26/Lecture_26_Intro_to_Logic_Design_Annotated.pdf differ diff --git a/_site/melody/cmpt-295/26/micro_graph.html b/_site/melody/cmpt-295/26/micro_graph.html new file mode 100644 index 0000000..cf71343 --- /dev/null +++ b/_site/melody/cmpt-295/26/micro_graph.html @@ -0,0 +1,551 @@ + + + + + + +

    + Graph +

    +

    + The ideal solution to this would actually be to turn the graph into a 3D braille model so it can be printed. Due to conditions of remoteness and the time required to do such a task though, this is not possible right now. +

    +

    + Please do not navigate to connections manually. Use the links. +

    +

    + Nodes of Graph +

    + +

    + Graph Connections (to) +

    + +

    + Graph Connections (form) +

    + + + diff --git a/_site/melody/cmpt-295/Lecture_06_Data_Representation_Fractional_Numbers.pdf b/_site/melody/cmpt-295/Lecture_06_Data_Representation_Fractional_Numbers.pdf new file mode 100644 index 0000000..a1c8ac4 Binary files /dev/null and b/_site/melody/cmpt-295/Lecture_06_Data_Representation_Fractional_Numbers.pdf differ diff --git a/_site/melody/cmpt-295/Lecture_06_Data_Representation_Fractional_Numbers_Annotated.pdf b/_site/melody/cmpt-295/Lecture_06_Data_Representation_Fractional_Numbers_Annotated.pdf new file mode 100644 index 0000000..35500d5 Binary files /dev/null and b/_site/melody/cmpt-295/Lecture_06_Data_Representation_Fractional_Numbers_Annotated.pdf differ diff --git a/_site/melody/cmpt-295/activities/02/02.html b/_site/melody/cmpt-295/activities/02/02.html new file mode 100644 index 0000000..93b4b8a --- /dev/null +++ b/_site/melody/cmpt-295/activities/02/02.html @@ -0,0 +1,93 @@ + + + + + | tait.tech + + + + + +
    +
    +

    Participation Activity 2

    + +

    1

    + +

    Rounding M=1.100001010011101001010111012M = \text{1.10000101001110100101011101}_{2} would produce the following frac: True or False?

    + + + + + + + + + + + + + + + + + + +
    sexpfrac
    N/AN/A10000101001110100101011
    + +

    2

    + +

    These C statements print 1 (true) on the screen as the result of executing aFloat == anInt: True or False?

    + +
    float aFloat = 12345; int anInt = aFloat;
    +printf("%.2f == %d -> %d", aFloat, anInt, aFloat==anInt);
    +
    + +

    3

    + +

    If we have:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressValue
    %rax0x0100
    %rbx0x0200
    %rcx128
    0x06005
    0x0400256
    0x06108
    + +

    %crx would contain 5 once the following instruction has been executed: True or False?

    + +
    movq 0x10(%rbx,%rax,4), %rcx
    +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/activities/02/02/index.html b/_site/melody/cmpt-295/activities/02/02/index.html new file mode 100644 index 0000000..6d465b0 --- /dev/null +++ b/_site/melody/cmpt-295/activities/02/02/index.html @@ -0,0 +1,95 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Participation Activity 2

    + +

    1

    + +

    Rounding M=1.100001010011101001010111012M = \text{1.10000101001110100101011101}_{2} would produce the following frac: True or False?

    + + + + + + + + + + + + + + + + + + +
    sexpfrac
    N/AN/A10000101001110100101011
    + +

    2

    + +

    These C statements print 1 (true) on the screen as the result of executing aFloat == anInt: True or False?

    + +
    float aFloat = 12345; int anInt = aFloat;
    +printf("%.2f == %d -> %d", aFloat, anInt, aFloat==anInt);
    +
    + +

    3

    + +

    If we have:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Register/AddressValue
    %rax0x0100
    %rbx0x0200
    %rcx128
    0x06005
    0x0400256
    0x06108
    + +

    %crx would contain 5 once the following instruction has been executed: True or False?

    + +
    movq 0x10(%rbx,%rax,4), %rcx
    +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/activities/02/Participation_Activity_2.pptx b/_site/melody/cmpt-295/activities/02/Participation_Activity_2.pptx new file mode 100644 index 0000000..f8fe826 Binary files /dev/null and b/_site/melody/cmpt-295/activities/02/Participation_Activity_2.pptx differ diff --git a/_site/melody/cmpt-295/activities/04/04-solution.html b/_site/melody/cmpt-295/activities/04/04-solution.html new file mode 100644 index 0000000..7eab87f --- /dev/null +++ b/_site/melody/cmpt-295/activities/04/04-solution.html @@ -0,0 +1,87 @@ + + + + + | tait.tech + + + + +
    +
    +

    Participation Activity 4

    + +

    Last name:

    + +

    First name:

    + +

    Std#:

    + +

    Memory Allocation Example:

    + +

    Where does eveything go?

    + +

    Indicate with an arrow in which memory segment the content of each box will be found one we start executing the program.

    + +

    Code (box is indicated as a comment):

    + +
    #include ... // cyan box (shared libraries; header files and associated code.)
    +
    +char hugeArray[1 << 31]; /* 2^31 = 2GB */ // purple box  (Data)
    +int global = 0; // purple box (Data)
    +
    +int useless(){ return 0; } // (text; function code)
    +
    +int main()
    +{
    +  void *ptr1, *ptr2; // red box (Stack)
    +  int local = 0; // red box (Stack)
    +  ptr1 = malloc(1 << 28); /* 2^28 = 256 MB */ // blue box (Heap)
    +  ptr2 = malloc(1 << 8); /* 2^8 = 256 B*/ // blue box (Heap)
    +/* some print statements */
    +}
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    M[]
    Kernel
    Stack (arrow pointing down)
    ...
    Shared Libraries
    ...
    Heap (arrow pointing up)
    Data
    Text
    ...
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/activities/04/04-solution/index.html b/_site/melody/cmpt-295/activities/04/04-solution/index.html new file mode 100644 index 0000000..b4ce662 --- /dev/null +++ b/_site/melody/cmpt-295/activities/04/04-solution/index.html @@ -0,0 +1,89 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    Participation Activity 4

    + +

    Last name:

    + +

    First name:

    + +

    Std#:

    + +

    Memory Allocation Example:

    + +

    Where does eveything go?

    + +

    Indicate with an arrow in which memory segment the content of each box will be found one we start executing the program.

    + +

    Code (box is indicated as a comment):

    + +
    #include ... // cyan box (shared libraries; header files and associated code.)
    +
    +char hugeArray[1 << 31]; /* 2^31 = 2GB */ // purple box  (Data)
    +int global = 0; // purple box (Data)
    +
    +int useless(){ return 0; } // (text; function code)
    +
    +int main()
    +{
    +  void *ptr1, *ptr2; // red box (Stack)
    +  int local = 0; // red box (Stack)
    +  ptr1 = malloc(1 << 28); /* 2^28 = 256 MB */ // blue box (Heap)
    +  ptr2 = malloc(1 << 8); /* 2^8 = 256 B*/ // blue box (Heap)
    +/* some print statements */
    +}
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    M[]
    Kernel
    Stack (arrow pointing down)
    ...
    Shared Libraries
    ...
    Heap (arrow pointing up)
    Data
    Text
    ...
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/activities/04/04.html b/_site/melody/cmpt-295/activities/04/04.html new file mode 100644 index 0000000..ae66383 --- /dev/null +++ b/_site/melody/cmpt-295/activities/04/04.html @@ -0,0 +1,85 @@ + + + + + | tait.tech + + + + +
    +
    +

    Participation Activity 4

    + +

    Last name:

    + +

    First name:

    + +

    Std#:

    + +

    Memory Allocation Example:

    + +

    Where does eveything go?

    + +

    Indicate with an arrow in which memory segment the content of each box will be found one we start executing the program.

    + +

    Code (box is indicated as a comment):

    + +
    #include ... // cyan box
    +
    +char hugeArray[1 << 31]; /* 2^31 = 2GB */ // purple box
    +int global = 0; // purple box
    +
    +int main()
    +{
    +  void *ptr1, *ptr2; // red box
    +  int local = 0; // red box
    +  ptr1 = malloc(1 << 28); /* 2^28 = 256 MB */ // blue box
    +  ptr2 = malloc(1 << 8); /* 2^8 = 256 B*/ // blue box
    +/* some print statements */
    +}
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    M[]
    Kernel
    Stack (arrow pointing down)
    ...
    Shared Libraries
    ...
    Heap (arrow pointing up)
    Data
    Text
    ...
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/activities/04/04/index.html b/_site/melody/cmpt-295/activities/04/04/index.html new file mode 100644 index 0000000..37df364 --- /dev/null +++ b/_site/melody/cmpt-295/activities/04/04/index.html @@ -0,0 +1,87 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    Participation Activity 4

    + +

    Last name:

    + +

    First name:

    + +

    Std#:

    + +

    Memory Allocation Example:

    + +

    Where does eveything go?

    + +

    Indicate with an arrow in which memory segment the content of each box will be found one we start executing the program.

    + +

    Code (box is indicated as a comment):

    + +
    #include ... // cyan box
    +
    +char hugeArray[1 << 31]; /* 2^31 = 2GB */ // purple box
    +int global = 0; // purple box
    +
    +int main()
    +{
    +  void *ptr1, *ptr2; // red box
    +  int local = 0; // red box
    +  ptr1 = malloc(1 << 28); /* 2^28 = 256 MB */ // blue box
    +  ptr2 = malloc(1 << 8); /* 2^8 = 256 B*/ // blue box
    +/* some print statements */
    +}
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    M[]
    Kernel
    Stack (arrow pointing down)
    ...
    Shared Libraries
    ...
    Heap (arrow pointing up)
    Data
    Text
    ...
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/activities/04/Participation_Activity_4_1217_CAL.pdf b/_site/melody/cmpt-295/activities/04/Participation_Activity_4_1217_CAL.pdf new file mode 100644 index 0000000..ae00cd4 Binary files /dev/null and b/_site/melody/cmpt-295/activities/04/Participation_Activity_4_1217_CAL.pdf differ diff --git a/_site/melody/cmpt-295/activities/04/Participation_Activity_4_1217_SOLUTION.pdf b/_site/melody/cmpt-295/activities/04/Participation_Activity_4_1217_SOLUTION.pdf new file mode 100644 index 0000000..f07890f Binary files /dev/null and b/_site/melody/cmpt-295/activities/04/Participation_Activity_4_1217_SOLUTION.pdf differ diff --git a/_site/melody/cmpt-295/activities/cmpt-295-activity-04-solution.zip b/_site/melody/cmpt-295/activities/cmpt-295-activity-04-solution.zip new file mode 100644 index 0000000..a80c17b Binary files /dev/null and b/_site/melody/cmpt-295/activities/cmpt-295-activity-04-solution.zip differ diff --git a/_site/melody/cmpt-295/activities/cmpt-295-activity-04.zip b/_site/melody/cmpt-295/activities/cmpt-295-activity-04.zip new file mode 100644 index 0000000..278fcf2 Binary files /dev/null and b/_site/melody/cmpt-295/activities/cmpt-295-activity-04.zip differ diff --git a/_site/melody/cmpt-295/activities/cmpt-295-activity-2.zip b/_site/melody/cmpt-295/activities/cmpt-295-activity-2.zip new file mode 100644 index 0000000..8b3c2e6 Binary files /dev/null and b/_site/melody/cmpt-295/activities/cmpt-295-activity-2.zip differ diff --git a/_site/melody/cmpt-295/asses/ass2/Assignment_2.pdf b/_site/melody/cmpt-295/asses/ass2/Assignment_2.pdf new file mode 100644 index 0000000..2ca297b Binary files /dev/null and b/_site/melody/cmpt-295/asses/ass2/Assignment_2.pdf differ diff --git a/_site/melody/cmpt-295/asses/ass2/ass2.html b/_site/melody/cmpt-295/asses/ass2/ass2.html new file mode 100644 index 0000000..1ecb1e3 --- /dev/null +++ b/_site/melody/cmpt-295/asses/ass2/ass2.html @@ -0,0 +1,530 @@ + + + + + | tait.tech + + + + + +
    +
    +

    Assignment 2

    + +

    CMPT 295 – Fall 2021

    + +

    Objectives: +In this assignment, you will gain familiarity with:

    + +
      +
    • IEEE floating point representation
    • +
    + +
    + +

    Submission:

    + +
      +
    • When creating your assignment document (i.e., your answers to this assignment), please, +
        +
      • Include the number of the question you are answering (e.g., Question 1.a. I) followed by your answer, keeping the questions in their original numerical order. Formatting your assignment document this way makes it a lot easier to mark. ☺
      • +
      • Add your full name and student number at the top of the first page of your document.
      • +
      +
    • +
    • Submit your document called Assignment_2.pdf, which must include your answers to all of the questions in Assignment 2.
    • +
    • If you write your answers by hand (as opposed to using a computer application to write them), when putting your assignment document together, do not take photos (no .jpg) of your assignment sheets! Scan them instead! +Better quality -> easier to read -> easier to mark! 
    • +
    + +


    + +

    Due:

    + +
      +
    • Friday, Oct. 1 at 4pm on CourSys.
    • +
    • Late assignments will receive a grade of 0, but they will be marked (if they are submitted before the solutions are posted on Monday) in order to provide feedback to the student.
    • +
    + +
    + +

    Requirements:

    + +
      +
    • Show your work (as illustrated in lectures).
    • +
    + +
    + +

    Marking scheme:

    + +
      +
    • This assignment will be marked as follows: +
        +
      • Questions 1 and 2 will be marked for correctness.
      • +
      +
    • +
    • The amount of marks for each question is indicated as part of the question.
    • +
    • A solution will be posted on Monday after the due date.
    • +
    + +
    + +
      +
    1. [8 marks] Floating point conversion and Rounding. +
        +
      1. Represent the following numbers in IEEE floating point representation (single precision), clearly showing the effect of rounding on the frac (mantissa) if rounding occurs. Then express your final answer in binary and in hexadecimal form. +
          +
        1. + 0.0011111112\text{0.001111111}_{2} +
        2. +
        3. + 3.141601562510\text{3.1416015625}_{10} +
        4. +
        5. + -0.910\text{-0.9}_{10} +
        6. +
        7. 1310\frac{1}{3}_{10} (a third)
        8. +
        +
      2. +
      3. Convert 0x4AEA4C1A from IEEE floating point representation (single precision) to a fractional decimal number (i.e., a real number).
      4. +
      5. Round the following binary numbers (rounding position is bolded – it is the bit at the 242^{-4} position -) following the rounding rules of the IEEE floating point representation. +
          +
        1. + 1.00111112\text{1.0011111}_{2} +
        2. +
        3. + 1.10010012\text{1.1001001}_{2} +
        4. +
        5. + 1.01111002\text{1.0111100}_{2} +
        6. +
        7. + 1.01101002\text{1.0110100}_{2} +
        8. +
        +
      6. +
      7. For each of the above rounded binary numbers, indicate what type of rounding you performed and compute the value that is either added to or subtracted from the original number (listed above) as a result of the rounding process. In other words, compute the error introduced by the rounding process.
      8. +
      +
    2. +
    3. [12 marks] Creating hypothetical smaller floating-point representations based on the IEEE floating point format allows us to investigate this encoding scheme more easily, since the numbers are easier to compute and manipulate.

      +Below is a table listing several fractional decimal numbers represented as 6-bit IEEE-like floating-point numbers (w = 6). The format of these 6-bit floating-point numbers is as follows: 1 bit is used to express for the sign (s), 3 bits are used to express exp +(k = 3) and 2 bits are used to represent frac (n = 2), in the following order: sign exp frac.

      +Complete the table (the same way as in Figure 2.35 in our textbook) then answer the questions below the table. +Tip: Have a look at Figure 2.35 in our textbook, which illustrates a similar table for a hypothetical 8-bit IEEE-like floating-point +format. This will give you an idea of how to complete the table. Also, Figure 2.34 displays the complete range of these 6-bit IEEElike floating point numbers as well as their values between -1.0 and 1.0. This diagram may be helpful when you are checking your work.

    4. +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ExponentFractionValue
    DescriptionBit representationexpE2E2^{E}fracMM2EM 2^{E}VDecimal
    zero0 000 0000.0
    smallest positive denormalized0 000 01
    0 000 10
    Largest positive denormalized0 000 11
    Smallest positive normalized0 001 00
    0 001 01
    0 001 10
    0 001 11
    0 010 01
    0 010 10
    0 010 11
    one0 011 00
    0 011 01
    0 011 10
    0 011 11
    0 100 00
    0 100 01
    0 100 10
    0 100 11
    0 101 00
    0 101 01
    0 101 10
    0 101 11
    0 110 00
    0 110 01
    0 110 10
    0 110 11
    + infinity-------
    NaN------NaN-
    + +
      +
    1. What is the value of the bias?
    2. +
    3. Consider two adjacent denormalized numbers. How far apart are they? Expressed this difference as a fractional decimal number +(i.e., a real number).
    4. +
    5. Consider two adjacent normalized numbers with the exp field set to 001. How far apart are they? Expressed this difference as a +decimal number.
    6. +
    7. Consider two adjacent normalized numbers with the exp field set to 010. How far apart are they? Expressed this difference as a +decimal number.
    8. +
    9. Consider two adjacent normalized numbers with the exp field set to 011. How far apart are they? Expressed this difference as a +decimal number.
    10. +
    11. Without doing any calculations, can you guess how far apart are two adjacent normalized numbers … +
        +
      1. with the exp field set to 100?
      2. +
      3. with the exp field set to 101?
      4. +
      5. with the exp field set to 110?
      6. +
      +
    12. +
    13. What is the “range” (not contiguous) of fractional decimal numbers that can be represented using this 6-bit IEEE-like floatingpoint representation?
    14. +
    15. What is the range of the normalized exponent E (E found in the equation v=(-1)sM2Ev = \text{(-1)}^{s} M 2^{E} ) which can be represented by this +6-bit IEEE-like floating-point representation?
    16. +
    17. +

      Give an example of a fractional decimal numbers that cannot be represented using this 6-bit IEEE-like floating-point +representation, but is within the “range” of representable values, which you expressed as your answer to Question 7. above.

      +
    18. +
    19. +

      Give an example of a real number that would overflow if we were trying to represent it using this 6-bit IEEE-like floating-point +representation. The best way to answer this question is to convert this real number into a 6-bit IEEE-like floating-point +representation and clearly indicate why it would overflow.

      +
    20. +
    21. How close is the value of the frac of the largest normalized number to 1? In other words, how close is M to 2? Yet another way +of phrasing this question is to ask: what is the value of ε (epsilon) in this equation 1 <= M < 2 - ε? Express your answer as a +fractional decimal number (i.e., a real number).
    22. +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/asses/ass2/ass2/index.html b/_site/melody/cmpt-295/asses/ass2/ass2/index.html new file mode 100644 index 0000000..aef7833 --- /dev/null +++ b/_site/melody/cmpt-295/asses/ass2/ass2/index.html @@ -0,0 +1,532 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Assignment 2

    + +

    CMPT 295 – Fall 2021

    + +

    Objectives: +In this assignment, you will gain familiarity with:

    + +
      +
    • IEEE floating point representation
    • +
    + +
    + +

    Submission:

    + +
      +
    • When creating your assignment document (i.e., your answers to this assignment), please, +
        +
      • Include the number of the question you are answering (e.g., Question 1.a. I) followed by your answer, keeping the questions in their original numerical order. Formatting your assignment document this way makes it a lot easier to mark. ☺
      • +
      • Add your full name and student number at the top of the first page of your document.
      • +
      +
    • +
    • Submit your document called Assignment_2.pdf, which must include your answers to all of the questions in Assignment 2.
    • +
    • If you write your answers by hand (as opposed to using a computer application to write them), when putting your assignment document together, do not take photos (no .jpg) of your assignment sheets! Scan them instead! +Better quality -> easier to read -> easier to mark! 
    • +
    + +


    + +

    Due:

    + +
      +
    • Friday, Oct. 1 at 4pm on CourSys.
    • +
    • Late assignments will receive a grade of 0, but they will be marked (if they are submitted before the solutions are posted on Monday) in order to provide feedback to the student.
    • +
    + +
    + +

    Requirements:

    + +
      +
    • Show your work (as illustrated in lectures).
    • +
    + +
    + +

    Marking scheme:

    + +
      +
    • This assignment will be marked as follows: +
        +
      • Questions 1 and 2 will be marked for correctness.
      • +
      +
    • +
    • The amount of marks for each question is indicated as part of the question.
    • +
    • A solution will be posted on Monday after the due date.
    • +
    + +
    + +
      +
    1. [8 marks] Floating point conversion and Rounding. +
        +
      1. Represent the following numbers in IEEE floating point representation (single precision), clearly showing the effect of rounding on the frac (mantissa) if rounding occurs. Then express your final answer in binary and in hexadecimal form. +
          +
        1. + 0.0011111112\text{0.001111111}_{2} +
        2. +
        3. + 3.141601562510\text{3.1416015625}_{10} +
        4. +
        5. + -0.910\text{-0.9}_{10} +
        6. +
        7. 1310\frac{1}{3}_{10} (a third)
        8. +
        +
      2. +
      3. Convert 0x4AEA4C1A from IEEE floating point representation (single precision) to a fractional decimal number (i.e., a real number).
      4. +
      5. Round the following binary numbers (rounding position is bolded – it is the bit at the 242^{-4} position -) following the rounding rules of the IEEE floating point representation. +
          +
        1. + 1.00111112\text{1.0011111}_{2} +
        2. +
        3. + 1.10010012\text{1.1001001}_{2} +
        4. +
        5. + 1.01111002\text{1.0111100}_{2} +
        6. +
        7. + 1.01101002\text{1.0110100}_{2} +
        8. +
        +
      6. +
      7. For each of the above rounded binary numbers, indicate what type of rounding you performed and compute the value that is either added to or subtracted from the original number (listed above) as a result of the rounding process. In other words, compute the error introduced by the rounding process.
      8. +
      +
    2. +
    3. [12 marks] Creating hypothetical smaller floating-point representations based on the IEEE floating point format allows us to investigate this encoding scheme more easily, since the numbers are easier to compute and manipulate.

      +Below is a table listing several fractional decimal numbers represented as 6-bit IEEE-like floating-point numbers (w = 6). The format of these 6-bit floating-point numbers is as follows: 1 bit is used to express for the sign (s), 3 bits are used to express exp +(k = 3) and 2 bits are used to represent frac (n = 2), in the following order: sign exp frac.

      +Complete the table (the same way as in Figure 2.35 in our textbook) then answer the questions below the table. +Tip: Have a look at Figure 2.35 in our textbook, which illustrates a similar table for a hypothetical 8-bit IEEE-like floating-point +format. This will give you an idea of how to complete the table. Also, Figure 2.34 displays the complete range of these 6-bit IEEElike floating point numbers as well as their values between -1.0 and 1.0. This diagram may be helpful when you are checking your work.

    4. +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ExponentFractionValue
    DescriptionBit representationexpE2E2^{E}fracMM2EM 2^{E}VDecimal
    zero0 000 0000.0
    smallest positive denormalized0 000 01
    0 000 10
    Largest positive denormalized0 000 11
    Smallest positive normalized0 001 00
    0 001 01
    0 001 10
    0 001 11
    0 010 01
    0 010 10
    0 010 11
    one0 011 00
    0 011 01
    0 011 10
    0 011 11
    0 100 00
    0 100 01
    0 100 10
    0 100 11
    0 101 00
    0 101 01
    0 101 10
    0 101 11
    0 110 00
    0 110 01
    0 110 10
    0 110 11
    + infinity-------
    NaN------NaN-
    + +
      +
    1. What is the value of the bias?
    2. +
    3. Consider two adjacent denormalized numbers. How far apart are they? Expressed this difference as a fractional decimal number +(i.e., a real number).
    4. +
    5. Consider two adjacent normalized numbers with the exp field set to 001. How far apart are they? Expressed this difference as a +decimal number.
    6. +
    7. Consider two adjacent normalized numbers with the exp field set to 010. How far apart are they? Expressed this difference as a +decimal number.
    8. +
    9. Consider two adjacent normalized numbers with the exp field set to 011. How far apart are they? Expressed this difference as a +decimal number.
    10. +
    11. Without doing any calculations, can you guess how far apart are two adjacent normalized numbers … +
        +
      1. with the exp field set to 100?
      2. +
      3. with the exp field set to 101?
      4. +
      5. with the exp field set to 110?
      6. +
      +
    12. +
    13. What is the “range” (not contiguous) of fractional decimal numbers that can be represented using this 6-bit IEEE-like floatingpoint representation?
    14. +
    15. What is the range of the normalized exponent E (E found in the equation v=(-1)sM2Ev = \text{(-1)}^{s} M 2^{E} ) which can be represented by this +6-bit IEEE-like floating-point representation?
    16. +
    17. +

      Give an example of a fractional decimal numbers that cannot be represented using this 6-bit IEEE-like floating-point +representation, but is within the “range” of representable values, which you expressed as your answer to Question 7. above.

      +
    18. +
    19. +

      Give an example of a real number that would overflow if we were trying to represent it using this 6-bit IEEE-like floating-point +representation. The best way to answer this question is to convert this real number into a 6-bit IEEE-like floating-point +representation and clearly indicate why it would overflow.

      +
    20. +
    21. How close is the value of the frac of the largest normalized number to 1? In other words, how close is M to 2? Yet another way +of phrasing this question is to ask: what is the value of ε (epsilon) in this equation 1 <= M < 2 - ε? Express your answer as a +fractional decimal number (i.e., a real number).
    22. +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/asses/ass3/03/index.html b/_site/melody/cmpt-295/asses/ass3/03/index.html new file mode 100644 index 0000000..e4b99e0 --- /dev/null +++ b/_site/melody/cmpt-295/asses/ass3/03/index.html @@ -0,0 +1,288 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Assignment 3

    + +

    CMPT 295 – Fall 2021

    + +

    Objectives:

    + +
      +
    • +

      Memory addressing modes

      +
    • +
    • +

      Assembly instructions

      +
    • +
    • +

      Reading object code (machine level instruction) expressed in hexadecimal and +understanding how these instructions are stored in memory

      +
    • +
    • +

      Writing a C program that corresponds to given assembly program

      +
    • +
    + +

    Submission:

    + +
      +
    • +

      Submit your document called Assignment_3.pdf, which must include the number of the +question you are answering (e.g., Question 1) followed by your answer, keeping the +questions in their original numerical order. Formatting your assignment document this +way makes it a lot easier to mark. +o Add your full name and student number at the top of the first page of your +document.

      +
    • +
    • +

      If you write your answers by hand (as opposed to using a computer application to +write them), when putting your assignment document together, do not take photos of +your assignment sheets! Scan them instead! Better quality -> easier to read -> easier to +mark!

      +
    • +
    + +

    Due:

    + +
      +
    • Friday Oct. 8 at 4pm on CourSys
    • +
    • Late assignments will receive a grade of 0, but they will be marked (if they are submitted +before the solutions are posted on Monday) in order to provide feedback to the +student.
    • +
    + +

    Marking scheme:

    +
      +
    • This assignment will be marked as follows: +
        +
      • Questions 1, 2 and 3 will be marked for correctness.
      • +
      +
    • +
    • The amount of marks for each question is indicated as part of the question.
    • +
    • A solution will be posted on Monday after the due date.
    • +
    + +
    + +

    1. [10 marks] Memory addressing modes

    + +

    Assume the following values are stored at the indicated memory addresses and registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Memory AddressValue
    0x2300x23
    0x2340x00
    0x2350x01
    0x23A0xed
    0x2400xff
    + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rdi0x230
    %rsi0x234
    %rcx0x4
    %rax0x1
    + +

    Imagine that the operands in the table below are the Src (source) operands for some +unspecified assembly instructions (any instruction except lea*), fill in the following table +with the appropriate answers.

    + +

    Note: We do not need to know what these assembly instructions are in order to fill the table.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OperandOperand Value (expressed in hexadecimal)Operand Form (Choices are: immediate, Register, or one of the 9 Memory Addressing Modes)
    %rsi Register
    (%rdi) Indirect memory addressing mode
    $0x23A  
    0x2400xff 
    10(%rdi) “Base + displacement” memory addressing mode
    560(%rcx,%rax)  
    -550(, %rdi, 2)  
    0x6(%rdi, %rax, 4)  
    + +

    Still using the first table listed above displaying the values stored at various memory +addresses and registers, fill in the following table with three different Src (source) operands +for some unspecified assembly instructions (any instruction except lea*). For each row, +this operand must result in the operand Value listed and must satisfy the Operand Form +listed.

    + + + + + + + + + + + + + + + + + + + + + + + + +
    OperandValueOperand Form (Choices are: Immediate, Register or one of the 9 Memory Addressing Modes)
     0x00Absolute memory addressing mode
     0x00Scaled indexed memory addressing mode
     0x00Indexed memory addressing mode
    + +

    2. [2 marks] Machine level instructions and their memory location

    + +

    Consider a function called arith, defined in a file called arith.c and called from the +main function found in the file called main.c.

    + +

    This function arith performs some arithmetic manipulation on its three parameters.

    + +

    Compiling main.c and arith.c files, we created an executable called ar, then we +executed the command:

    + +
    +objdump –d ar > arith.objdump
    +
    + +

    We display the partial content of arith.objdump below. The file arith.objdump is +the disassembled version of the executable file ar. +Your task is to fill in its missing parts, which have been underlined:

    + +
    +0000000000$400527 <arith>:
    +400527:   48 8d 04 37       lea   (%rdi,%rsi,1),%rax
    +______:   48 01 d0          add   %rdx,%rax
    +40052e:   48 8d 0c 76       lea   (%rsi,%rsi,2),%rxc
    +______:   48 c1 e1 04       shl   $0x4,%rcx
    +400536:   48 8d 54 0f 04    lea   0x4(%rdi,%rcx,1),%rdx
    +______:   48 0f af c2       imul  %rdx,%rax
    +______:   c3                retq
    +
    + +

    3. [8 marks] C program versus assembly program

    + +

    Do the Homework Problem 3.58 at the end of Chapter 3. Make sure you satisfy the following requirements:

    + +
      +
    • Your code must be commented and well spaced such that others (i.e., TA’s) can read +your code and understand what each instruction does.
    • +
    • About comments: +
        +
      • Comment of Type 1: Here is an example of a useful comment: cmpl %edx, %r8d # loop while j < N
      • +
      • Comment of Type 2: Here is an example of a not so useful comment: cmpl %edx, %r8d # compare %edx with %r8d
      • +
      • Do you see the difference? Make sure you write comments of Type 1.
      • +
      +
    • +
    • You cannot use the goto statement.
    • +
    • You must write your program using C (not C++) and your program must compile on our target machine.
    • +
    + +

    Once you have created your program and saved it in a file called decode2.c, generate its +assembly code version using the optimization level “g” (–Og) and save it in a file called +decode2.s.

    + +

    Include the content of both files decode2.c and decode2.s in your assignment

    + +

    Assignment_3.pdf document. Label them well.

    + +

    You do not have to electronically submit your files decode2.c and decode2.s on +CourSys. However, your program must be functionally correct (i.e., it must compile, +execute properly and solve this problem).

    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/asses/ass3/Assignment_3.pdf b/_site/melody/cmpt-295/asses/ass3/Assignment_3.pdf new file mode 100644 index 0000000..81d82d7 Binary files /dev/null and b/_site/melody/cmpt-295/asses/ass3/Assignment_3.pdf differ diff --git a/_site/melody/cmpt-295/asses/ass3/ass3.html b/_site/melody/cmpt-295/asses/ass3/ass3.html new file mode 100644 index 0000000..f1efd8b --- /dev/null +++ b/_site/melody/cmpt-295/asses/ass3/ass3.html @@ -0,0 +1,286 @@ + + + + + | tait.tech + + + + + +
    +
    +

    Assignment 3

    + +

    CMPT 295 – Fall 2021

    + +

    Objectives:

    + +
      +
    • +

      Memory addressing modes

      +
    • +
    • +

      Assembly instructions

      +
    • +
    • +

      Reading object code (machine level instruction) expressed in hexadecimal and +understanding how these instructions are stored in memory

      +
    • +
    • +

      Writing a C program that corresponds to given assembly program

      +
    • +
    + +

    Submission:

    + +
      +
    • +

      Submit your document called Assignment_3.pdf, which must include the number of the +question you are answering (e.g., Question 1) followed by your answer, keeping the +questions in their original numerical order. Formatting your assignment document this +way makes it a lot easier to mark. +o Add your full name and student number at the top of the first page of your +document.

      +
    • +
    • +

      If you write your answers by hand (as opposed to using a computer application to +write them), when putting your assignment document together, do not take photos of +your assignment sheets! Scan them instead! Better quality -> easier to read -> easier to +mark!

      +
    • +
    + +

    Due:

    + +
      +
    • Friday Oct. 8 at 4pm on CourSys
    • +
    • Late assignments will receive a grade of 0, but they will be marked (if they are submitted +before the solutions are posted on Monday) in order to provide feedback to the +student.
    • +
    + +

    Marking scheme:

    +
      +
    • This assignment will be marked as follows: +
        +
      • Questions 1, 2 and 3 will be marked for correctness.
      • +
      +
    • +
    • The amount of marks for each question is indicated as part of the question.
    • +
    • A solution will be posted on Monday after the due date.
    • +
    + +
    + +

    1. [10 marks] Memory addressing modes

    + +

    Assume the following values are stored at the indicated memory addresses and registers:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Memory AddressValue
    0x2300x23
    0x2340x00
    0x2350x01
    0x23A0xed
    0x2400xff
    + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
    %rdi0x230
    %rsi0x234
    %rcx0x4
    %rax0x1
    + +

    Imagine that the operands in the table below are the Src (source) operands for some +unspecified assembly instructions (any instruction except lea*), fill in the following table +with the appropriate answers.

    + +

    Note: We do not need to know what these assembly instructions are in order to fill the table.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OperandOperand Value (expressed in hexadecimal)Operand Form (Choices are: immediate, Register, or one of the 9 Memory Addressing Modes)
    %rsi Register
    (%rdi) Indirect memory addressing mode
    $0x23A  
    0x2400xff 
    10(%rdi) “Base + displacement” memory addressing mode
    560(%rcx,%rax)  
    -550(, %rdi, 2)  
    0x6(%rdi, %rax, 4)  
    + +

    Still using the first table listed above displaying the values stored at various memory +addresses and registers, fill in the following table with three different Src (source) operands +for some unspecified assembly instructions (any instruction except lea*). For each row, +this operand must result in the operand Value listed and must satisfy the Operand Form +listed.

    + + + + + + + + + + + + + + + + + + + + + + + + +
    OperandValueOperand Form (Choices are: Immediate, Register or one of the 9 Memory Addressing Modes)
     0x00Absolute memory addressing mode
     0x00Scaled indexed memory addressing mode
     0x00Indexed memory addressing mode
    + +

    2. [2 marks] Machine level instructions and their memory location

    + +

    Consider a function called arith, defined in a file called arith.c and called from the +main function found in the file called main.c.

    + +

    This function arith performs some arithmetic manipulation on its three parameters.

    + +

    Compiling main.c and arith.c files, we created an executable called ar, then we +executed the command:

    + +
    +objdump –d ar > arith.objdump
    +
    + +

    We display the partial content of arith.objdump below. The file arith.objdump is +the disassembled version of the executable file ar. +Your task is to fill in its missing parts, which have been underlined:

    + +
    +0000000000$400527 <arith>:
    +400527:   48 8d 04 37       lea   (%rdi,%rsi,1),%rax
    +______:   48 01 d0          add   %rdx,%rax
    +40052e:   48 8d 0c 76       lea   (%rsi,%rsi,2),%rxc
    +______:   48 c1 e1 04       shl   $0x4,%rcx
    +400536:   48 8d 54 0f 04    lea   0x4(%rdi,%rcx,1),%rdx
    +______:   48 0f af c2       imul  %rdx,%rax
    +______:   c3                retq
    +
    + +

    3. [8 marks] C program versus assembly program

    + +

    Do the Homework Problem 3.58 at the end of Chapter 3. Make sure you satisfy the following requirements:

    + +
      +
    • Your code must be commented and well spaced such that others (i.e., TA’s) can read +your code and understand what each instruction does.
    • +
    • About comments: +
        +
      • Comment of Type 1: Here is an example of a useful comment: cmpl %edx, %r8d # loop while j < N
      • +
      • Comment of Type 2: Here is an example of a not so useful comment: cmpl %edx, %r8d # compare %edx with %r8d
      • +
      • Do you see the difference? Make sure you write comments of Type 1.
      • +
      +
    • +
    • You cannot use the goto statement.
    • +
    • You must write your program using C (not C++) and your program must compile on our target machine.
    • +
    + +

    Once you have created your program and saved it in a file called decode2.c, generate its +assembly code version using the optimization level “g” (–Og) and save it in a file called +decode2.s.

    + +

    Include the content of both files decode2.c and decode2.s in your assignment

    + +

    Assignment_3.pdf document. Label them well.

    + +

    You do not have to electronically submit your files decode2.c and decode2.s on +CourSys. However, your program must be functionally correct (i.e., it must compile, +execute properly and solve this problem).

    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/asses/ass4/Assignment_4.pdf b/_site/melody/cmpt-295/asses/ass4/Assignment_4.pdf new file mode 100644 index 0000000..0c2436b Binary files /dev/null and b/_site/melody/cmpt-295/asses/ass4/Assignment_4.pdf differ diff --git a/_site/melody/cmpt-295/asses/ass4/ass4.html b/_site/melody/cmpt-295/asses/ass4/ass4.html new file mode 100644 index 0000000..09151bb --- /dev/null +++ b/_site/melody/cmpt-295/asses/ass4/ass4.html @@ -0,0 +1,370 @@ + + + + + | tait.tech + + + + + +
    +
    +

    Assignment 4

    + +

    CMPT 295 – Fall 2021

    + +

    Objectives:

    + +
      +
    • Hand tracing assembly code
    • +
    • Translating assembly code into C code
    • +
    • Writing x86-64 assembly code
    • +
    + +
    + +

    Submission Assignment 4 is a little unusual

    + +
      +
    • Doing Assigment 4 will help you to prepare for Midterm 1 even though Assigment 4 is due after our Midterm 1.
    • +
    • On Friday, Oct. 15 by 4pm, submit the following 3 documents on CourSys: +
        +
      • Assignment_4.pdf which is to contain only your solution to Question 2. You do not have to submit your solution to Question 1. Do Question 1 as part of your studying for our Midterm 1.
      • +
      • main.c and calculator.s, i.e., your solution to Question 3.
      • +
      +
    • +
    • Late assignments will receive a grade of 0, but they will be marked (if they are submitted before the solutions are posted on the Monday after the due date) in order to provide feedback to the student.
    • +
    + +
    + +

    Marking scheme:

    + +

    This assignment will be marked as follows:

    + +
      +
    • Question 1 is not marked. Do Question 1 as part of your studying for our Midterm 1. +
        +
      • A solution to Question 1 will be posted along with this Assignment 4.
      • +
      +
    • +
    • Questions 2 and 3 will be marked for correctness. +
        +
      • Solution to Question 2 and Question 3 will be posted on the Monday after the due date.
      • +
      +
    • +
    + +

    The amount of marks for each question is indicated as part of the question.

    + +
    + +

    1) [0 marks] Hand tracing assembly code - Do Question 1 as part of your studying for our Midterm 1.

    + +

    Consider the following code:

    + +

    C function abs(…):

    + +
    int abs(int x){
    +  if(x<0){
    +    x=-x;
    +  }
    +  return x;
    +}
    +
    + +

    abs(…)version 1 (based on a non-optimized gcc version):

    + +
      .global abs
    +abs:
    +  movl %edi,%eax
    +  cmpl $0,%eax
    +  jgq endif
    +  negl %eax
    +endif:
    +  ret
    +
    + +

    abs(…)version 2 (an optimized gcc version)

    + +
      .global abs
    +abs:
    +  movl %edi,%edx
    +  movl %edi,%eax
    +  sarl $31,%edx
    +  xorl %edx,%eax
    +  subl %edx,%eax
    +  ret
    +
    + +

    The left column contains the C function abs(…), the middle column contains the assembly +code version of the C function abs(…) we wrote in class (we shall call it “version 1”) and the +right column contains the assembly code version of abs(…) the gcc compiler may produce +when it assembles the C function abs(…) using level 2 optimization (“–O2”). We shall call it +“version 2”.

    + +

    Notice how gcc assembles abs(…) without branching, i.e., without affecting the execution +flow (without using the jump instructions). We shall see in our next Unit (Chapter 4 of our +textbook) that branching is rather unpredictable and may cause problem in the execution +pipeline of the microprocessor. For this reason, the assembly code version (version 1) of +abs(…) which branches may run more slowly.

    + +

    In this question, you are asked to hand trace both versions of abs(…) using 2 test cases:

    + +
      +
    • Test case 1: x = 7, expected result: 7
    • +
    • Test case 2: x = -7, expected result: 7
    • +
    + +

    and show the result of executing each instruction. In doing so, complete the tables below:

    + +

    Note:

    + +
      +
    • The first table has been completed as an example. Feel free to use it as a model when you complete the other three tables.
    • +
    • Remember that x(y)=x+yx – (-y) = x + y.
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    abs(…) version 1 Test case 1: x = 7 Expected result: 7Result of executing instructions in the left column
    movl %edi, %eaxCopy content of %edi (x = 7) into %eax, i.e., %edi <- 00000000000000000000000000000111 <- 29 0’s -> %eax <- 00000000000000000000000000000111 <- 29 0’s
    cmpl $0, %eax3 outcomes: x0>0x – 0 > 0, x0<0x – 0 < 0, x0=0x – 0 = 0 Here since %eax contains 7, then the only possible (true) outcome is 7 – 0 > 0, i.e., when the microprocessor evaluates 7 – 0, it obtains the result 7. This result (being greater than 0: 7 > 0) sets the condition codes to “g” and therefore …
    jge endif… the jump instruction is executed
    negl %eaxand this instruction is not executed
    endif: retand this instruction is executed.
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    abs(…) version 1 Test case 2) x = -7 Expected result: 7Result of executing instructions on the left column
    movl %edi, %eax 
    cmpl $0, %eax 
    jge endif 
    negl %eax 
    endif: ret 
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    abs(…) version 2 Test case 1) x = 7 Expected result: 7Result of executing instruction in the left column
    movl %edi,%edx 
    movl %edi,%eax 
    sarl $31,%edx 
    xorl %edx,%eax 
    subl %edx,%eax 
    ret 
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    abs(…) version 2 Test case 2) x = - 7 Expected result: 7Result of executing instruction in the left column
    movl %edi,%edx 
    movl %edi,%eax 
    sarl $31,%edx 
    xorl %edx,%eax 
    subl %edx,%eax 
    ret 
    + +

    2) [8 marks] Translating assembly code into C code - Read the entire question before answering it!

    + +

    Consider the following assembly code:

    + +
    # long func(long x, int n)
    +# x in %rdi, n in %esi, result in %rax
    +func:
    +  movl %esi,%ecx
    +  movl $1,%edx
    +  movl $0,%eax
    +  jmp cond
    +loop:
    +  movq %rdi,%r8
    +  andq %rdx,%r8
    +  orq %r8,%rax
    +  salq %cl,%rdx # shift left the value stored in %rdx by an amount related to the value in %cl*
    +cond:
    +  testq %rdx, %rdx # Value in %rdx is >0, <0, =0 ?
    +  jne loop # jump if %rdx != 0; fall thru to ret if %rdx = 0
    +ret
    +
    + + + +

    Section 3.5.3 of our textbook explains how a shift instruction works when it has the +register %cl as one of its operands. Check it out!

    + +

    The assembly code above was generated by compiling C code that had the following overall form:

    + +
    long func(long x, int n) {
    +  long result=___;
    +  long mask;
    +
    +  for (mask=___;mask ___;mask=___)
    +    result|= ___;
    +  return result;
    +}
    +
    + +

    Your task is to fill in the missing parts of the C function func above to get a program +equivalent (note: it may not be exactly the same) to the generated assembly code displayed +above it. You will find it helpful to examine the assembly code before, during, and after the +loop to form a consistent mapping between the registers and the C function variables.

    + +

    You may also find the following questions helpful in figuring out the assembly code. Note +that you do not have to submit the answers to the five questions below as part fo +Assignemnt 4 as these answers will be reflected in the C function you are asked to complete +and submit.

    + +
      +
    1. (a) Which registers hold program values x, n, result, and mask?
    2. +
    3. (b) What is the initial value of result and of mask?
    4. +
    5. (c) What is the test condition for mask?
    6. +
    7. (d) How is mask updated?
    8. +
    9. (e) How is result updated?
    10. +
    + +

    3) [12 marks] Writing x86-64 assembly code

    + +

    Download Assn4_Q3_Files.zip, in which you will find a makefile, main.c and an +incomplete calculator.s. The latter contains four functions implementing arithmetic +and logical operations in assembly code.

    + +

    Your task is to complete the implementation of the three incomplete functions, namely, +plus, minus and mul. In doing so, you must satisfy the requirements found in each of the +functions of calculator.s. You must also satisfy the requirements below.

    + +

    You will also need to figure out what the function XX does and once you have done so, you +will need to change its name to something more descriptive (in main.c and in +calculator.s) and add its description in the indicated place in calculator.s.

    + +

    Requirements:

    + +
      +
    • Your assembly program must follow the following standard: +
        +
      • Your code must be commented such that others (i.e., TA’s) can read your code and understand what each instruction does.
      • +
      • About comments: +
          +
        • Comment of Type 1: Here is an example of a useful comment: cmpl %edx, %r8d # loop while j < N
        • +
        • Comment of Type 2: Here is an example of a not so useful comment: cmpl %edx, %r8d # compare %r8d to %edx
        • +
        +
      • +
      • Do you see the difference? Make sure you write comments of Type 1.
      • +
      +
    • +
    • Also, describe the algorithm you used to perform the multiplication in a comment at the top of mul function.
    • +
    • Your code must compile (using gcc) and execute on the target machine.
    • +
    • Each of your code files (main.c and calculator.s) must contain a header comment block including the filename, the purpose/description of your program, your name and the date.
    • +
    • For all of the four functions, the register %edi will contain the argument x and the register %esi will contain the argument y. The register %eax will carry the return value.
    • +
    • You may use registers %rax, %rcx, %rdx, %rsi, %rdi, %r8, %r9, %r10 and %r11 as temporary registers.
    • +
    • You must not modify the values of registers %rbx, %rbp, %rsp, %r12, %r13, %r14 and %r15. We shall soon see why.
    • +
    • You cannot modify the given makefile.
    • +
    + +

    Hint for the implementation of the mul function:

    + +

    Long ago, computers were restricted in their arithmetic prowess and were only able to +perform additions and subtractions. Multiplications and divisions needed to be implemented +by the programmer using the arithmetic operations available.

    + +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/asses/ass4/ass4/index.html b/_site/melody/cmpt-295/asses/ass4/ass4/index.html new file mode 100644 index 0000000..087ef44 --- /dev/null +++ b/_site/melody/cmpt-295/asses/ass4/ass4/index.html @@ -0,0 +1,372 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Assignment 4

    + +

    CMPT 295 – Fall 2021

    + +

    Objectives:

    + +
      +
    • Hand tracing assembly code
    • +
    • Translating assembly code into C code
    • +
    • Writing x86-64 assembly code
    • +
    + +
    + +

    Submission Assignment 4 is a little unusual

    + +
      +
    • Doing Assigment 4 will help you to prepare for Midterm 1 even though Assigment 4 is due after our Midterm 1.
    • +
    • On Friday, Oct. 15 by 4pm, submit the following 3 documents on CourSys: +
        +
      • Assignment_4.pdf which is to contain only your solution to Question 2. You do not have to submit your solution to Question 1. Do Question 1 as part of your studying for our Midterm 1.
      • +
      • main.c and calculator.s, i.e., your solution to Question 3.
      • +
      +
    • +
    • Late assignments will receive a grade of 0, but they will be marked (if they are submitted before the solutions are posted on the Monday after the due date) in order to provide feedback to the student.
    • +
    + +
    + +

    Marking scheme:

    + +

    This assignment will be marked as follows:

    + +
      +
    • Question 1 is not marked. Do Question 1 as part of your studying for our Midterm 1. +
        +
      • A solution to Question 1 will be posted along with this Assignment 4.
      • +
      +
    • +
    • Questions 2 and 3 will be marked for correctness. +
        +
      • Solution to Question 2 and Question 3 will be posted on the Monday after the due date.
      • +
      +
    • +
    + +

    The amount of marks for each question is indicated as part of the question.

    + +
    + +

    1) [0 marks] Hand tracing assembly code - Do Question 1 as part of your studying for our Midterm 1.

    + +

    Consider the following code:

    + +

    C function abs(…):

    + +
    int abs(int x){
    +  if(x<0){
    +    x=-x;
    +  }
    +  return x;
    +}
    +
    + +

    abs(…)version 1 (based on a non-optimized gcc version):

    + +
      .global abs
    +abs:
    +  movl %edi,%eax
    +  cmpl $0,%eax
    +  jgq endif
    +  negl %eax
    +endif:
    +  ret
    +
    + +

    abs(…)version 2 (an optimized gcc version)

    + +
      .global abs
    +abs:
    +  movl %edi,%edx
    +  movl %edi,%eax
    +  sarl $31,%edx
    +  xorl %edx,%eax
    +  subl %edx,%eax
    +  ret
    +
    + +

    The left column contains the C function abs(…), the middle column contains the assembly +code version of the C function abs(…) we wrote in class (we shall call it “version 1”) and the +right column contains the assembly code version of abs(…) the gcc compiler may produce +when it assembles the C function abs(…) using level 2 optimization (“–O2”). We shall call it +“version 2”.

    + +

    Notice how gcc assembles abs(…) without branching, i.e., without affecting the execution +flow (without using the jump instructions). We shall see in our next Unit (Chapter 4 of our +textbook) that branching is rather unpredictable and may cause problem in the execution +pipeline of the microprocessor. For this reason, the assembly code version (version 1) of +abs(…) which branches may run more slowly.

    + +

    In this question, you are asked to hand trace both versions of abs(…) using 2 test cases:

    + +
      +
    • Test case 1: x = 7, expected result: 7
    • +
    • Test case 2: x = -7, expected result: 7
    • +
    + +

    and show the result of executing each instruction. In doing so, complete the tables below:

    + +

    Note:

    + +
      +
    • The first table has been completed as an example. Feel free to use it as a model when you complete the other three tables.
    • +
    • Remember that x(y)=x+yx – (-y) = x + y.
    • +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    abs(…) version 1 Test case 1: x = 7 Expected result: 7Result of executing instructions in the left column
    movl %edi, %eaxCopy content of %edi (x = 7) into %eax, i.e., %edi <- 00000000000000000000000000000111 <- 29 0’s -> %eax <- 00000000000000000000000000000111 <- 29 0’s
    cmpl $0, %eax3 outcomes: x0>0x – 0 > 0, x0<0x – 0 < 0, x0=0x – 0 = 0 Here since %eax contains 7, then the only possible (true) outcome is 7 – 0 > 0, i.e., when the microprocessor evaluates 7 – 0, it obtains the result 7. This result (being greater than 0: 7 > 0) sets the condition codes to “g” and therefore …
    jge endif… the jump instruction is executed
    negl %eaxand this instruction is not executed
    endif: retand this instruction is executed.
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    abs(…) version 1 Test case 2) x = -7 Expected result: 7Result of executing instructions on the left column
    movl %edi, %eax 
    cmpl $0, %eax 
    jge endif 
    negl %eax 
    endif: ret 
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    abs(…) version 2 Test case 1) x = 7 Expected result: 7Result of executing instruction in the left column
    movl %edi,%edx 
    movl %edi,%eax 
    sarl $31,%edx 
    xorl %edx,%eax 
    subl %edx,%eax 
    ret 
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    abs(…) version 2 Test case 2) x = - 7 Expected result: 7Result of executing instruction in the left column
    movl %edi,%edx 
    movl %edi,%eax 
    sarl $31,%edx 
    xorl %edx,%eax 
    subl %edx,%eax 
    ret 
    + +

    2) [8 marks] Translating assembly code into C code - Read the entire question before answering it!

    + +

    Consider the following assembly code:

    + +
    # long func(long x, int n)
    +# x in %rdi, n in %esi, result in %rax
    +func:
    +  movl %esi,%ecx
    +  movl $1,%edx
    +  movl $0,%eax
    +  jmp cond
    +loop:
    +  movq %rdi,%r8
    +  andq %rdx,%r8
    +  orq %r8,%rax
    +  salq %cl,%rdx # shift left the value stored in %rdx by an amount related to the value in %cl*
    +cond:
    +  testq %rdx, %rdx # Value in %rdx is >0, <0, =0 ?
    +  jne loop # jump if %rdx != 0; fall thru to ret if %rdx = 0
    +ret
    +
    + + + +

    Section 3.5.3 of our textbook explains how a shift instruction works when it has the +register %cl as one of its operands. Check it out!

    + +

    The assembly code above was generated by compiling C code that had the following overall form:

    + +
    long func(long x, int n) {
    +  long result=___;
    +  long mask;
    +
    +  for (mask=___;mask ___;mask=___)
    +    result|= ___;
    +  return result;
    +}
    +
    + +

    Your task is to fill in the missing parts of the C function func above to get a program +equivalent (note: it may not be exactly the same) to the generated assembly code displayed +above it. You will find it helpful to examine the assembly code before, during, and after the +loop to form a consistent mapping between the registers and the C function variables.

    + +

    You may also find the following questions helpful in figuring out the assembly code. Note +that you do not have to submit the answers to the five questions below as part fo +Assignemnt 4 as these answers will be reflected in the C function you are asked to complete +and submit.

    + +
      +
    1. (a) Which registers hold program values x, n, result, and mask?
    2. +
    3. (b) What is the initial value of result and of mask?
    4. +
    5. (c) What is the test condition for mask?
    6. +
    7. (d) How is mask updated?
    8. +
    9. (e) How is result updated?
    10. +
    + +

    3) [12 marks] Writing x86-64 assembly code

    + +

    Download Assn4_Q3_Files.zip, in which you will find a makefile, main.c and an +incomplete calculator.s. The latter contains four functions implementing arithmetic +and logical operations in assembly code.

    + +

    Your task is to complete the implementation of the three incomplete functions, namely, +plus, minus and mul. In doing so, you must satisfy the requirements found in each of the +functions of calculator.s. You must also satisfy the requirements below.

    + +

    You will also need to figure out what the function XX does and once you have done so, you +will need to change its name to something more descriptive (in main.c and in +calculator.s) and add its description in the indicated place in calculator.s.

    + +

    Requirements:

    + +
      +
    • Your assembly program must follow the following standard: +
        +
      • Your code must be commented such that others (i.e., TA’s) can read your code and understand what each instruction does.
      • +
      • About comments: +
          +
        • Comment of Type 1: Here is an example of a useful comment: cmpl %edx, %r8d # loop while j < N
        • +
        • Comment of Type 2: Here is an example of a not so useful comment: cmpl %edx, %r8d # compare %r8d to %edx
        • +
        +
      • +
      • Do you see the difference? Make sure you write comments of Type 1.
      • +
      +
    • +
    • Also, describe the algorithm you used to perform the multiplication in a comment at the top of mul function.
    • +
    • Your code must compile (using gcc) and execute on the target machine.
    • +
    • Each of your code files (main.c and calculator.s) must contain a header comment block including the filename, the purpose/description of your program, your name and the date.
    • +
    • For all of the four functions, the register %edi will contain the argument x and the register %esi will contain the argument y. The register %eax will carry the return value.
    • +
    • You may use registers %rax, %rcx, %rdx, %rsi, %rdi, %r8, %r9, %r10 and %r11 as temporary registers.
    • +
    • You must not modify the values of registers %rbx, %rbp, %rsp, %r12, %r13, %r14 and %r15. We shall soon see why.
    • +
    • You cannot modify the given makefile.
    • +
    + +

    Hint for the implementation of the mul function:

    + +

    Long ago, computers were restricted in their arithmetic prowess and were only able to +perform additions and subtractions. Multiplications and divisions needed to be implemented +by the programmer using the arithmetic operations available.

    + +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/asses/ass5/05.html b/_site/melody/cmpt-295/asses/ass5/05.html new file mode 100644 index 0000000..7888f3b --- /dev/null +++ b/_site/melody/cmpt-295/asses/ass5/05.html @@ -0,0 +1,420 @@ + + + + + | tait.tech + + + + +
    +
    +

    CMPT 295 – Fall 2021

    + +

    Assignment 5

    + +

    Objectives:

    + +
      +
    • x86-64 function calls and stack
    • +
    • Investigating the size of some x86-64 assembly instructions
    • +
    + +
    + +

    Submission:

    + +
      +
    • Submit your document called Assignment_5.pdf om CourSys. +
        +
      • Add your full name and student number at the top of the first page of your document.
      • +
      +
    • +
    • If you write your answers by hand (as opposed to using a computer application to +write them), when putting your assignment document together, do not take photos of +your assignment sheets! Scan them instead! Better quality -> easier to read -> easier to +mark!
    • +
    + +
    + +

    Due:

    + +
      +
    • Friday Oct. 29 at 4pm on CourSys
    • +
    • Late assignments will receive a grade of 0, but they will be marked (if they are submitted +before the solutions are posted on Monday) in order to provide feedback to the +student.
    • +
    + +
    + +

    Marking scheme:

    + +
      +
    • All questions of this assignment will be marked for correctness.
    • +
    • The amount of marks for each question is indicated as part of the question.
    • +
    • solution will be posted on Monday after the due date.
    • +
    + +
    + +

    1) [10 marks] x86-64 function calls and stack

    + +

    a. Hand trace the code from our Lab 4 (main.c , main.s, p1.c, p1.s, p2.c and p2.s) using the test case, i.e., x = 6, y = 9, buf[40].

    + +
      +
    • As you do so, draw the corresponding Stack Diagram for the entire program, i.e., +until you reach (but have not yet executed) the ret instruction of the main +function. To do so, you can either print the “Stack Diagram” sheet at the end of +this assignment and do the drawing by hand, then scan the result and include it +into this assignment document OR do the drawing by electronically annotating the +“Stack Diagram” sheet at the end of this assignment then include the result into +this assignment document.
    • +
    • The use of the Register Table is optional: use it only if you find it useful. You do +not have to include it as part of your assignment document.
    • +
    • Indicate the movement of %rsp by crossing its old location and rewriting “%rsp” +to indicate its new location (as we have done in our lectures and Lab 4).
    • +
    • Cross the content of the stack that has been popped.
    • +
    • When the value of a stack location is changed, cross its old value and write the +new value in the same stack location.
    • +
    • Make sure you include the content of buf in your Stack Diagram.
    • +
    • When drawing your stack diagram, you do not have to show the effect on the stack +of the 5 call instructions at lines 33, 43, 45 and 57 in main.s and at line 47 in +p1.s. These are calls to printf(…), puts(…) and sprintf(…). In other +words, you do not have to add the return addresses associated to these 5 calls +onto the stack.
    • +
    • Hint: The solution to Participation Activity 6 (which will be posted after its +deadeline on Monday) will give us a great head start!
    • +
    + +

    b. Modify main.c by reducing the size of buf[] from 40 to 24.

    + +
      +
    • Remake the code and hand trace it using the following test case, i.e., x = 6, y = +9, buf[24].
    • +
    • Repeat the instructions found in the section a. above and create a second drawing +of the stack using the Stack Diagram sheet found at the end of this assignment.
    • +
    • Make sure you include the content of buf in your Stack Diagram drawing.
    • +
    • On the same sheet as your Stack Diagram, asnwer the question:What happens to +the “canary value” in this situation?
    • +
    + +
    + +

    2. [10 marks] Investigating the size of some x86-64 assembly instructions

    + +

    Complete the following three tables:

    + +

    Table 1:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    X86-64 InstructionsTheir size (in bytes)
    xorq %rax, %rax 
    xorl %eax, %eax 
    movq $0, %rax 
    movl $0, %eax 
    + +

    Table 2:

    + + + + + + + + + + + + + + + + + + + + + + +
    X86-64 InstructionsTheir size (in bytes)
    addl $1, %eax 
    leal 1(%eax), %eax 
    incl %eax 
    + +

    Table 3:

    + + + + + + + + + + + + + + + + + + +
    X86-64 InstructionsTheir size (in bytes)
    addl $8, %eax 
    leal 8(%eax), %eax 
    + +

    To complete the above tables, you must:

    +
      +
    • Write an assembly program and include all the above x86-64 instructions in it. Of +course, this program is going to be rather nonsensical. Not a problem!
    • +
    • Compile your program.
    • +
    • Use the objdump tool on the resulting object file you obtained from the +compilation process.
    • +
    • Looking at the result will lead you to the answer, i.e., the size of each of these x8664 instructions in bytes.
    • +
    • In each table, bold the instruction that is the most space efficient.
    • +
    • Copy the content of your program (*.s) into this assignment (into this +Assignment_5.pdf document).
    • +
    • Copy the result you obtained from the objdump tool into this assignment (into this +Assignment_5.pdf document).
    • +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Base + DisplacementStackPurpose
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
    + +

    Register Table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/asses/ass5/05/index.html b/_site/melody/cmpt-295/asses/ass5/05/index.html new file mode 100644 index 0000000..7867f35 --- /dev/null +++ b/_site/melody/cmpt-295/asses/ass5/05/index.html @@ -0,0 +1,422 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    CMPT 295 – Fall 2021

    + +

    Assignment 5

    + +

    Objectives:

    + +
      +
    • x86-64 function calls and stack
    • +
    • Investigating the size of some x86-64 assembly instructions
    • +
    + +
    + +

    Submission:

    + +
      +
    • Submit your document called Assignment_5.pdf om CourSys. +
        +
      • Add your full name and student number at the top of the first page of your document.
      • +
      +
    • +
    • If you write your answers by hand (as opposed to using a computer application to +write them), when putting your assignment document together, do not take photos of +your assignment sheets! Scan them instead! Better quality -> easier to read -> easier to +mark!
    • +
    + +
    + +

    Due:

    + +
      +
    • Friday Oct. 29 at 4pm on CourSys
    • +
    • Late assignments will receive a grade of 0, but they will be marked (if they are submitted +before the solutions are posted on Monday) in order to provide feedback to the +student.
    • +
    + +
    + +

    Marking scheme:

    + +
      +
    • All questions of this assignment will be marked for correctness.
    • +
    • The amount of marks for each question is indicated as part of the question.
    • +
    • solution will be posted on Monday after the due date.
    • +
    + +
    + +

    1) [10 marks] x86-64 function calls and stack

    + +

    a. Hand trace the code from our Lab 4 (main.c , main.s, p1.c, p1.s, p2.c and p2.s) using the test case, i.e., x = 6, y = 9, buf[40].

    + +
      +
    • As you do so, draw the corresponding Stack Diagram for the entire program, i.e., +until you reach (but have not yet executed) the ret instruction of the main +function. To do so, you can either print the “Stack Diagram” sheet at the end of +this assignment and do the drawing by hand, then scan the result and include it +into this assignment document OR do the drawing by electronically annotating the +“Stack Diagram” sheet at the end of this assignment then include the result into +this assignment document.
    • +
    • The use of the Register Table is optional: use it only if you find it useful. You do +not have to include it as part of your assignment document.
    • +
    • Indicate the movement of %rsp by crossing its old location and rewriting “%rsp” +to indicate its new location (as we have done in our lectures and Lab 4).
    • +
    • Cross the content of the stack that has been popped.
    • +
    • When the value of a stack location is changed, cross its old value and write the +new value in the same stack location.
    • +
    • Make sure you include the content of buf in your Stack Diagram.
    • +
    • When drawing your stack diagram, you do not have to show the effect on the stack +of the 5 call instructions at lines 33, 43, 45 and 57 in main.s and at line 47 in +p1.s. These are calls to printf(…), puts(…) and sprintf(…). In other +words, you do not have to add the return addresses associated to these 5 calls +onto the stack.
    • +
    • Hint: The solution to Participation Activity 6 (which will be posted after its +deadeline on Monday) will give us a great head start!
    • +
    + +

    b. Modify main.c by reducing the size of buf[] from 40 to 24.

    + +
      +
    • Remake the code and hand trace it using the following test case, i.e., x = 6, y = +9, buf[24].
    • +
    • Repeat the instructions found in the section a. above and create a second drawing +of the stack using the Stack Diagram sheet found at the end of this assignment.
    • +
    • Make sure you include the content of buf in your Stack Diagram drawing.
    • +
    • On the same sheet as your Stack Diagram, asnwer the question:What happens to +the “canary value” in this situation?
    • +
    + +
    + +

    2. [10 marks] Investigating the size of some x86-64 assembly instructions

    + +

    Complete the following three tables:

    + +

    Table 1:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    X86-64 InstructionsTheir size (in bytes)
    xorq %rax, %rax 
    xorl %eax, %eax 
    movq $0, %rax 
    movl $0, %eax 
    + +

    Table 2:

    + + + + + + + + + + + + + + + + + + + + + + +
    X86-64 InstructionsTheir size (in bytes)
    addl $1, %eax 
    leal 1(%eax), %eax 
    incl %eax 
    + +

    Table 3:

    + + + + + + + + + + + + + + + + + + +
    X86-64 InstructionsTheir size (in bytes)
    addl $8, %eax 
    leal 8(%eax), %eax 
    + +

    To complete the above tables, you must:

    +
      +
    • Write an assembly program and include all the above x86-64 instructions in it. Of +course, this program is going to be rather nonsensical. Not a problem!
    • +
    • Compile your program.
    • +
    • Use the objdump tool on the resulting object file you obtained from the +compilation process.
    • +
    • Looking at the result will lead you to the answer, i.e., the size of each of these x8664 instructions in bytes.
    • +
    • In each table, bold the instruction that is the most space efficient.
    • +
    • Copy the content of your program (*.s) into this assignment (into this +Assignment_5.pdf document).
    • +
    • Copy the result you obtained from the objdump tool into this assignment (into this +Assignment_5.pdf document).
    • +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Base + DisplacementStackPurpose
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
    + +

    Register Table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    RegisterValue
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/asses/ass5/Assignment_5.pdf b/_site/melody/cmpt-295/asses/ass5/Assignment_5.pdf new file mode 100644 index 0000000..a7299dc Binary files /dev/null and b/_site/melody/cmpt-295/asses/ass5/Assignment_5.pdf differ diff --git a/_site/melody/cmpt-295/asses/ass6/Assignment_6.pdf b/_site/melody/cmpt-295/asses/ass6/Assignment_6.pdf new file mode 100644 index 0000000..a181d5c Binary files /dev/null and b/_site/melody/cmpt-295/asses/ass6/Assignment_6.pdf differ diff --git a/_site/melody/cmpt-295/asses/ass6/ass6.html b/_site/melody/cmpt-295/asses/ass6/ass6.html new file mode 100644 index 0000000..abafa92 --- /dev/null +++ b/_site/melody/cmpt-295/asses/ass6/ass6.html @@ -0,0 +1,328 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 – Fall 2021 – Assignment 6

    + +

    Objectives:

    + +
      +
    • Recursion in x86-64 assembly code
    • +
    • Manipulating 2D arrays (matrices) in x86-64 assembly code
    • +
    • Experimenting with Stack Randomization
    • +
    • There are some extra problems at the end of this assignment!
    • +
    + +
    + +

    Submission:

    + +
      +
    • Submit your document called Assignment_6.pdf, which must include your answers to all +of the questions in Assignment 6. +
        +
      • Add your full name and student number at the top of the first page of your document Assignment_6.pdf.
      • +
      • You will also need to submit your source code file produced in Question 3 on CourSys. More submission details outlined in Question 3.
      • +
      +
    • +
    • Submit your assignment Assignment_6.pdf on CourSys.
    • +
    + +
    + +

    Due:

    + +
      +
    • Friday, Nov. 5 at 23:59:59
    • +
    • Late assignments will receive a grade of 0, but they will be marked (if they are submitted before the solutions are posted on Monday) in order to provide feedback to the +student.
    • +
    + +
    + +

    Marking scheme:

    + +
      +
    • Question 1 and Question 2 of this assignment will be marked for correctness.
    • +
    • Question 3 of this assignment will be marked for completeness.
    • +
    • The amount of marks for each question is indicated as part of the question.
    • +
    • A solution will be posted on Monday after the due date.
    • +
    + +
    + +

    1) [5 marks] Recursion in x86-64 assembly code

    + +

    In this problem, you are asked to rewrite the mul function you wrote in Assisgnment 4. This +time, instead of using a loop, you are to use recursion. In doing so, you must use the stack (so +we can get some practice), either by pushing/popping or by getting a section of it (e.g., subq +$24, %rsp) and releasing it (e.g., addq $24, %rsp) at the end of your program. +Yes, it is certainly possible to recursively implement mul without saving the values of the +argument registers (edi and esi) onto the stack. However, for practice’s sake, let’s save them +onto the stack as stated in the question. +Use your files from Assignment 4: main.c, makefile and calculator.s. Then, copy +the following and paste it over (replace) your entire mul function in calculator.s:

    + +
    mul: # performs integer multiplication - when both operands
    +are non-negative!
    +# x in edi, y in esi
    +# You can assume that both parameters are non-negative.
    +# Requirements:
    +# - cannot use imul* instruction
    +#
    +(or any kind of instruction that multiplies such as mul)
    +# - you must use recursion (no loop) and the stack
    +
    + +

    Implement this mul function satisfying its new requirements (above in green). You must +also satisfy the requirements below.

    + +

    Requirements:

    + +
      +
    • Your code must be commented such that others (i.e., TA’s) can read your code and +understand what each instruction does. +
        +
      • About comments: +
          +
        • Comment of Type 1: Here is an example of a useful comment: cmpl %edx, %r8d # loop while j < N
        • +
        • Comment of Type 2: Here is an example of a not so useful comment: cmpl %edx, %r8d # compare %edx with %r8d
        • +
        +
      • +
      • Do you see the difference? Make sure you write comments of Type 1.
      • +
      +
    • +
    • Make sure you update the header comment block in calculator.s.
    • +
    • You must use the makefile provided in Assignment 4 when compiling your code. This makefile cannot be modified.
    • +
    • You cannot modify the prototype of the function mul. The reason is that your code may be tested using a test driver built based on this function prototype.
    • +
    • Your code must compile (using gcc) on our target machine and execute on our target machine.
    • +
    • You must follow the x86-64 function call and register saving conventions described in class and in the textbook.
    • +
    • Do not push/pop registers unless you make use of them in your function mul and +their content needs to be preseved. Memory accesses are expensive! We’ll soon see why!
    • +
    + +

    Submission:

    + +

    Once your new calculator.s compiles, executes and has been tested (i.e., it +multiplies) on our target machine, copy and paste the entire content of your new +calculator.s in this assignment (yes, it will still have the other functions you +implemented in Assignment 4 in it – that is OK!).

    + +

    2) [13 marks] Manipulating 2D arrays (matrices) in x86-64 assembly code

    + +

    In linear algebra, a matrix is a rectangular grid of numbers. Its dimensions are specified by its +number of rows and of columns. This question focuses on the representation and +manipulation of square matrices, i.e., where the number of rows and the number of columns +both equal n. +Here is an example of a square matrix where n = 4:

    + +

    A=[1234567812345678]=[A00A01A02A03A10A11A12A13A20A21A22A23A30A31A32A33] +A = \begin{bmatrix} +1 & -2 & 3 & -4\\ +-5 & 6 & -7 & 8\\ +-1 & 2 & -3 & 4\\ +5 & -6 & 7 & -8 +\end{bmatrix} += +\begin{bmatrix} +A_{00} & A_{01} & A_{02} & A_{03}\\ +A_{10} & A_{11} & A_{12} & A_{13}\\ +A_{20} & A_{21} & A_{22} & A_{23}\\ +A_{30} & A_{31} & A_{32} & A_{33} +\end{bmatrix} +

    + +

    Note the notation AijA_{ij} refers to the matrix entry at the ith row and the jth column of A. Each row +of the matrix A resembles a one dimensional array in the programming language C, with the +value of j increasing for each element. The matrix A has i such rows. +Because of this resemblance, matrices can be represented (modeled) in our C programs using +two dimensional arrays. One dimensional arrays are stored in contiguous memory space, +where their element 0 is followed by their element 1 which is followed by their element 2, +etc… Two-dimensional arrays follow a similar pattern when stored in memory: the one row +following the other. In other words, the elements from row 0 are followed by the elements +from row 1, which are followed by elements of row 2, and so on. Thus, a two dimensional +array, representing a n x n matrix, has n2n^{2} elements, and the base pointer A, contains the +address of the first element of the array, i.e., the 0th element of row 0.

    + +

    Because of this regular pattern, accessing a two dimensional array element can be done in a +random fashion, where the address of Aij=A+L(i×n+j)A_{ij} = A + L (i \times n + j), where L is the size (in bytes) of +each array element. For example, when L = 1, as it is for this assignment, then the element +A32 can be found at address A+1(34+2)=A+14A + 1 ( 3 * 4 + 2 ) = A + 14. +In this question, you are asked to rotate a matrix 90 degrees clockwise. One way to do this is +to first transpose the matrix then to reverse its columns. +Wikipedia says that, in linear algebra, the transpose of a matrix is an operator which flips a +matrix over its diagonal, i.e., it switches the row and column indices of the matrix by +producing another matrix denoted as AT. Thank you, Wikipedia.

    + +

    Here is an example where ATA^{T} is the transpose of matrix A (using the diagonal “1, 6, -3, -8”):

    + +

    A=[1234567812345678] +A = \begin{bmatrix} +1 & -2 & 3 & -4\\ +-5 & 6 & -7 & 8\\ +-1 & 2 & -3 & 4\\ +5 & -6 & 7 & -8 +\end{bmatrix} +

    + +

    AT=[1515262637374848] +A^{T} = \begin{bmatrix} +1 & -5 & -1 & 5\\ +-2 & 6 & 2 & -6\\ +3 & -7 & -3 & 7\\ +-4 & 8 & 4 & -8 +\end{bmatrix} +

    + +

    We reverse the columns of the transpose matrix AT, by swapping the last column with the +first column, the penultimate column with the second column, etc… +Using the same example as above, here is what AT looks like once it has been reversed. We +call this final matrix A’:

    + +

    AT=[1515262637374848] +A^{T} = \begin{bmatrix} +1 & -5 & -1 & 5\\ +-2 & 6 & 2 & -6\\ +3 & -7 & -3 & 7\\ +-4 & 8 & 4 & -8 +\end{bmatrix} +

    + +

    A’=[5151626273738484] +\text{A’} = \begin{bmatrix} +5 & -1 & -5 & 1\\ +-6 & 2 & 6 & -2\\ +7 & -3 & -7 & 3\\ +-8 & 4 & 8 & -4 +\end{bmatrix} +

    + +

    As you can see, A’ is the rotated version of A (A has been rotated by 90 degrees clockwise).

    + +

    Your task is to implement these two functions in x86-64 assembly code:

    + +
      +
    • void transpose(void *, int );
    • +
    • void reverseColumns(void *, int n);
    • +
    + +

    When they are called in this order, using a two dimensional array as their first argument, the +effect will be to rotate this array by 90 degrees clockwise.

    + +

    Download Assn6-Files_Q3.zip, expand it and open the files (makefile, main.c +and an incomplete matrix.s. ). Have a look at main.c and notice its content. Have a look +at matrix.s. It contains functions manipulating matrices such as copy, transpose and reverseColumns. You need to complete the implementation of the functions +transpose and reverseColumns. The function copy has already been implemented +for you. You may find hand tracing its code useful. You may also want to have a look at the +video recording of Lecture 22 Part 1 posted on our course web site in which the code of the +copy function copy.s is explained. Finally, you may want to “make” the given code and see +what it does.

    + +

    Requirements:

    + +
      +
    • Your code must be commented such that others (i.e., TA’s) can read your code and +understand what each instruction does.
    • +
    • About comments: +
        +
      • Comment of Type 1: Here is an example of a useful comment: +cmpl %edx, %r8d # loop while j < N
      • +
      • Comment of Type 2: Here is an example of a not so useful comment: +cmpl %edx, %r8d # compare %edx with %r8d +Do you see the difference? Make sure you write comments of Type 1.
      • +
      +
    • +
    • You must add a header comment block to the file matrix.s. This header comment +block must include the filename, the purpose/description of its functions, your name, your student number and the date.
    • +
    • You must use the makefile provided when compiling your code. This makefile cannot +be modified.
    • +
    • You cannot modify the code that has been supplied to you in the zip file. This signifies +that, amongst other things, you must not change the prototype of the functions given. The reason is that these functions may be tested using a test driver built based on +these function prototypes.
    • +
    • Your code must compile (using gcc) on our target machine and execute on our target machine. Your code must also solve the problem, i.e., rotate the array by 90 degrees clockwise.
    • +
    • You must follow the x86-64 function call and register saving conventions described in class and in the textbook.
    • +
    • Do not push/pop registers unless you make use of them in your functions and their content needs to be preseved.
    • +
    + +

    Submission:

    + +
      +
    • Electronically submit your file matrix.s via CourSys.
    • +
    • Also copy and paste the entire content of your matrix.s in this assignment. Make +sure it is the same version of matrix.s that compiles and executes on our target +machine.
    • +
    + +

    3) [2 marks] Experimenting with Stack Randomization

    + +

    We saw inn Lecture 22 that one of the ways to counter buffer overflow attacks was to use +compilers (like gcc for Linux) that implemented “safety” mechanisms such as Stack +Randomization. On way the compiler does this stack randomization is by assigning a different +start address to the stack every time a program execxutes. This changing start address to the +stack makes it difficult for hackers to predict the location of return addresses on the stack.

    + +

    Before you proceed with this question, make sure you read Section 3.10.4 in our textbook +and review our course lecture notes on this topic.

    + +

    In this assignment you are asked to confirm or disprove the fact that gcc does implement +Stack Randomization by running your own experiement as follows:

    + +
      +
    • Download from our course web site (see Lecture 22) the password program +(password.c) we saw as part of our Lecture 22 demo.
    • +
    • Add a statement that prints the memory address of the first byte of the array +password.
    • +
    • Execute your program 10 times and record the addresses at which this local variable +array is stored on the stack.
    • +
    • Then state your conclusion and support it with your experiential results.
    • +
    • Finally, calculate the range of variance in the memory addresses you obtained (if +any) over the 10 executions of your program.
    • +
    • Copy the content of your password.c into this assignment (into this +Assignment_6.pdf document).
    • +
    + +
    + +

    Extra Problems – Not to be submitted – Not for grades, but for practice only!

    + +

    A. Passing arguments and returning return value

    + +

    Have a look at Slide 5 of our Lecture 18. As you hand trace the code, comment each line +explaining what each instruction does and why?

    + +

    Here is an example:

    + +

    pushq %r13 # main function needs to use registers for temporary storage so it saves the +original value of the callee saved register %r13 so it can make use of it.

    + +
    + +

    B. Recursion in x86-64 assembly code

    + +

    Have a look at Slide 6 of our Lecture 20.

    + +
      +
    1. Hand trace the C code of the function countOnesR(…) and figure out what it does. +Feel free to use the second test case on Slide 13 (of our Lecture 20) as you are hand +tracing the code.
    2. +
    3. Hand trace its assembly code and as you do so, draw its stack diagram on Slide 14. Feel +free to use the Register Table, if you find it useful.
    4. +
    5. Create a third test case following the same format as the format used on Slide 13.
    6. +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/asses/ass6/ass6/index.html b/_site/melody/cmpt-295/asses/ass6/ass6/index.html new file mode 100644 index 0000000..abafa92 --- /dev/null +++ b/_site/melody/cmpt-295/asses/ass6/ass6/index.html @@ -0,0 +1,328 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295 – Fall 2021 – Assignment 6

    + +

    Objectives:

    + +
      +
    • Recursion in x86-64 assembly code
    • +
    • Manipulating 2D arrays (matrices) in x86-64 assembly code
    • +
    • Experimenting with Stack Randomization
    • +
    • There are some extra problems at the end of this assignment!
    • +
    + +
    + +

    Submission:

    + +
      +
    • Submit your document called Assignment_6.pdf, which must include your answers to all +of the questions in Assignment 6. +
        +
      • Add your full name and student number at the top of the first page of your document Assignment_6.pdf.
      • +
      • You will also need to submit your source code file produced in Question 3 on CourSys. More submission details outlined in Question 3.
      • +
      +
    • +
    • Submit your assignment Assignment_6.pdf on CourSys.
    • +
    + +
    + +

    Due:

    + +
      +
    • Friday, Nov. 5 at 23:59:59
    • +
    • Late assignments will receive a grade of 0, but they will be marked (if they are submitted before the solutions are posted on Monday) in order to provide feedback to the +student.
    • +
    + +
    + +

    Marking scheme:

    + +
      +
    • Question 1 and Question 2 of this assignment will be marked for correctness.
    • +
    • Question 3 of this assignment will be marked for completeness.
    • +
    • The amount of marks for each question is indicated as part of the question.
    • +
    • A solution will be posted on Monday after the due date.
    • +
    + +
    + +

    1) [5 marks] Recursion in x86-64 assembly code

    + +

    In this problem, you are asked to rewrite the mul function you wrote in Assisgnment 4. This +time, instead of using a loop, you are to use recursion. In doing so, you must use the stack (so +we can get some practice), either by pushing/popping or by getting a section of it (e.g., subq +$24, %rsp) and releasing it (e.g., addq $24, %rsp) at the end of your program. +Yes, it is certainly possible to recursively implement mul without saving the values of the +argument registers (edi and esi) onto the stack. However, for practice’s sake, let’s save them +onto the stack as stated in the question. +Use your files from Assignment 4: main.c, makefile and calculator.s. Then, copy +the following and paste it over (replace) your entire mul function in calculator.s:

    + +
    mul: # performs integer multiplication - when both operands
    +are non-negative!
    +# x in edi, y in esi
    +# You can assume that both parameters are non-negative.
    +# Requirements:
    +# - cannot use imul* instruction
    +#
    +(or any kind of instruction that multiplies such as mul)
    +# - you must use recursion (no loop) and the stack
    +
    + +

    Implement this mul function satisfying its new requirements (above in green). You must +also satisfy the requirements below.

    + +

    Requirements:

    + +
      +
    • Your code must be commented such that others (i.e., TA’s) can read your code and +understand what each instruction does. +
        +
      • About comments: +
          +
        • Comment of Type 1: Here is an example of a useful comment: cmpl %edx, %r8d # loop while j < N
        • +
        • Comment of Type 2: Here is an example of a not so useful comment: cmpl %edx, %r8d # compare %edx with %r8d
        • +
        +
      • +
      • Do you see the difference? Make sure you write comments of Type 1.
      • +
      +
    • +
    • Make sure you update the header comment block in calculator.s.
    • +
    • You must use the makefile provided in Assignment 4 when compiling your code. This makefile cannot be modified.
    • +
    • You cannot modify the prototype of the function mul. The reason is that your code may be tested using a test driver built based on this function prototype.
    • +
    • Your code must compile (using gcc) on our target machine and execute on our target machine.
    • +
    • You must follow the x86-64 function call and register saving conventions described in class and in the textbook.
    • +
    • Do not push/pop registers unless you make use of them in your function mul and +their content needs to be preseved. Memory accesses are expensive! We’ll soon see why!
    • +
    + +

    Submission:

    + +

    Once your new calculator.s compiles, executes and has been tested (i.e., it +multiplies) on our target machine, copy and paste the entire content of your new +calculator.s in this assignment (yes, it will still have the other functions you +implemented in Assignment 4 in it – that is OK!).

    + +

    2) [13 marks] Manipulating 2D arrays (matrices) in x86-64 assembly code

    + +

    In linear algebra, a matrix is a rectangular grid of numbers. Its dimensions are specified by its +number of rows and of columns. This question focuses on the representation and +manipulation of square matrices, i.e., where the number of rows and the number of columns +both equal n. +Here is an example of a square matrix where n = 4:

    + +

    A=[1234567812345678]=[A00A01A02A03A10A11A12A13A20A21A22A23A30A31A32A33] +A = \begin{bmatrix} +1 & -2 & 3 & -4\\ +-5 & 6 & -7 & 8\\ +-1 & 2 & -3 & 4\\ +5 & -6 & 7 & -8 +\end{bmatrix} += +\begin{bmatrix} +A_{00} & A_{01} & A_{02} & A_{03}\\ +A_{10} & A_{11} & A_{12} & A_{13}\\ +A_{20} & A_{21} & A_{22} & A_{23}\\ +A_{30} & A_{31} & A_{32} & A_{33} +\end{bmatrix} +

    + +

    Note the notation AijA_{ij} refers to the matrix entry at the ith row and the jth column of A. Each row +of the matrix A resembles a one dimensional array in the programming language C, with the +value of j increasing for each element. The matrix A has i such rows. +Because of this resemblance, matrices can be represented (modeled) in our C programs using +two dimensional arrays. One dimensional arrays are stored in contiguous memory space, +where their element 0 is followed by their element 1 which is followed by their element 2, +etc… Two-dimensional arrays follow a similar pattern when stored in memory: the one row +following the other. In other words, the elements from row 0 are followed by the elements +from row 1, which are followed by elements of row 2, and so on. Thus, a two dimensional +array, representing a n x n matrix, has n2n^{2} elements, and the base pointer A, contains the +address of the first element of the array, i.e., the 0th element of row 0.

    + +

    Because of this regular pattern, accessing a two dimensional array element can be done in a +random fashion, where the address of Aij=A+L(i×n+j)A_{ij} = A + L (i \times n + j), where L is the size (in bytes) of +each array element. For example, when L = 1, as it is for this assignment, then the element +A32 can be found at address A+1(34+2)=A+14A + 1 ( 3 * 4 + 2 ) = A + 14. +In this question, you are asked to rotate a matrix 90 degrees clockwise. One way to do this is +to first transpose the matrix then to reverse its columns. +Wikipedia says that, in linear algebra, the transpose of a matrix is an operator which flips a +matrix over its diagonal, i.e., it switches the row and column indices of the matrix by +producing another matrix denoted as AT. Thank you, Wikipedia.

    + +

    Here is an example where ATA^{T} is the transpose of matrix A (using the diagonal “1, 6, -3, -8”):

    + +

    A=[1234567812345678] +A = \begin{bmatrix} +1 & -2 & 3 & -4\\ +-5 & 6 & -7 & 8\\ +-1 & 2 & -3 & 4\\ +5 & -6 & 7 & -8 +\end{bmatrix} +

    + +

    AT=[1515262637374848] +A^{T} = \begin{bmatrix} +1 & -5 & -1 & 5\\ +-2 & 6 & 2 & -6\\ +3 & -7 & -3 & 7\\ +-4 & 8 & 4 & -8 +\end{bmatrix} +

    + +

    We reverse the columns of the transpose matrix AT, by swapping the last column with the +first column, the penultimate column with the second column, etc… +Using the same example as above, here is what AT looks like once it has been reversed. We +call this final matrix A’:

    + +

    AT=[1515262637374848] +A^{T} = \begin{bmatrix} +1 & -5 & -1 & 5\\ +-2 & 6 & 2 & -6\\ +3 & -7 & -3 & 7\\ +-4 & 8 & 4 & -8 +\end{bmatrix} +

    + +

    A’=[5151626273738484] +\text{A’} = \begin{bmatrix} +5 & -1 & -5 & 1\\ +-6 & 2 & 6 & -2\\ +7 & -3 & -7 & 3\\ +-8 & 4 & 8 & -4 +\end{bmatrix} +

    + +

    As you can see, A’ is the rotated version of A (A has been rotated by 90 degrees clockwise).

    + +

    Your task is to implement these two functions in x86-64 assembly code:

    + +
      +
    • void transpose(void *, int );
    • +
    • void reverseColumns(void *, int n);
    • +
    + +

    When they are called in this order, using a two dimensional array as their first argument, the +effect will be to rotate this array by 90 degrees clockwise.

    + +

    Download Assn6-Files_Q3.zip, expand it and open the files (makefile, main.c +and an incomplete matrix.s. ). Have a look at main.c and notice its content. Have a look +at matrix.s. It contains functions manipulating matrices such as copy, transpose and reverseColumns. You need to complete the implementation of the functions +transpose and reverseColumns. The function copy has already been implemented +for you. You may find hand tracing its code useful. You may also want to have a look at the +video recording of Lecture 22 Part 1 posted on our course web site in which the code of the +copy function copy.s is explained. Finally, you may want to “make” the given code and see +what it does.

    + +

    Requirements:

    + +
      +
    • Your code must be commented such that others (i.e., TA’s) can read your code and +understand what each instruction does.
    • +
    • About comments: +
        +
      • Comment of Type 1: Here is an example of a useful comment: +cmpl %edx, %r8d # loop while j < N
      • +
      • Comment of Type 2: Here is an example of a not so useful comment: +cmpl %edx, %r8d # compare %edx with %r8d +Do you see the difference? Make sure you write comments of Type 1.
      • +
      +
    • +
    • You must add a header comment block to the file matrix.s. This header comment +block must include the filename, the purpose/description of its functions, your name, your student number and the date.
    • +
    • You must use the makefile provided when compiling your code. This makefile cannot +be modified.
    • +
    • You cannot modify the code that has been supplied to you in the zip file. This signifies +that, amongst other things, you must not change the prototype of the functions given. The reason is that these functions may be tested using a test driver built based on +these function prototypes.
    • +
    • Your code must compile (using gcc) on our target machine and execute on our target machine. Your code must also solve the problem, i.e., rotate the array by 90 degrees clockwise.
    • +
    • You must follow the x86-64 function call and register saving conventions described in class and in the textbook.
    • +
    • Do not push/pop registers unless you make use of them in your functions and their content needs to be preseved.
    • +
    + +

    Submission:

    + +
      +
    • Electronically submit your file matrix.s via CourSys.
    • +
    • Also copy and paste the entire content of your matrix.s in this assignment. Make +sure it is the same version of matrix.s that compiles and executes on our target +machine.
    • +
    + +

    3) [2 marks] Experimenting with Stack Randomization

    + +

    We saw inn Lecture 22 that one of the ways to counter buffer overflow attacks was to use +compilers (like gcc for Linux) that implemented “safety” mechanisms such as Stack +Randomization. On way the compiler does this stack randomization is by assigning a different +start address to the stack every time a program execxutes. This changing start address to the +stack makes it difficult for hackers to predict the location of return addresses on the stack.

    + +

    Before you proceed with this question, make sure you read Section 3.10.4 in our textbook +and review our course lecture notes on this topic.

    + +

    In this assignment you are asked to confirm or disprove the fact that gcc does implement +Stack Randomization by running your own experiement as follows:

    + +
      +
    • Download from our course web site (see Lecture 22) the password program +(password.c) we saw as part of our Lecture 22 demo.
    • +
    • Add a statement that prints the memory address of the first byte of the array +password.
    • +
    • Execute your program 10 times and record the addresses at which this local variable +array is stored on the stack.
    • +
    • Then state your conclusion and support it with your experiential results.
    • +
    • Finally, calculate the range of variance in the memory addresses you obtained (if +any) over the 10 executions of your program.
    • +
    • Copy the content of your password.c into this assignment (into this +Assignment_6.pdf document).
    • +
    + +
    + +

    Extra Problems – Not to be submitted – Not for grades, but for practice only!

    + +

    A. Passing arguments and returning return value

    + +

    Have a look at Slide 5 of our Lecture 18. As you hand trace the code, comment each line +explaining what each instruction does and why?

    + +

    Here is an example:

    + +

    pushq %r13 # main function needs to use registers for temporary storage so it saves the +original value of the callee saved register %r13 so it can make use of it.

    + +
    + +

    B. Recursion in x86-64 assembly code

    + +

    Have a look at Slide 6 of our Lecture 20.

    + +
      +
    1. Hand trace the C code of the function countOnesR(…) and figure out what it does. +Feel free to use the second test case on Slide 13 (of our Lecture 20) as you are hand +tracing the code.
    2. +
    3. Hand trace its assembly code and as you do so, draw its stack diagram on Slide 14. Feel +free to use the Register Table, if you find it useful.
    4. +
    5. Create a third test case following the same format as the format used on Slide 13.
    6. +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/asses/ass7/Assignment_7.pdf b/_site/melody/cmpt-295/asses/ass7/Assignment_7.pdf new file mode 100644 index 0000000..25f0589 Binary files /dev/null and b/_site/melody/cmpt-295/asses/ass7/Assignment_7.pdf differ diff --git a/_site/melody/cmpt-295/asses/ass7/ass7.html b/_site/melody/cmpt-295/asses/ass7/ass7.html new file mode 100644 index 0000000..3efc47a --- /dev/null +++ b/_site/melody/cmpt-295/asses/ass7/ass7.html @@ -0,0 +1,930 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Assignment 7 - CMPT 295 – Fall 2021

    + +

    Objectives:

    + +
      +
    • Designing and evaluating instruction sets (ISA)
    • +
    + +
    + +

    Submission:

    + +
      +
    • Submit your document called Assignment_7.pdf, which must include your answers to all +of the questions in Assignment 7. +
        +
      • Add your full name and student number at the top of the first page of your +document Assignment_7.pdf.
      • +
      +
    • +
    • Submit your assignment Assignment_7.pdf on CourSys.
    • +
    + +
    + +

    Due:

    + +
      +
    • Friday Nov. 12 at 23:59:59.
    • +
    • Late assignments will receive a grade of 0, but they will be marked (if they are submitted +before the solutions are posted on Monday) in order to provide feedback to the +student.
    • +
    + +
    + +

    Marking scheme:

    + +
      +
    • The marks assigned to each question is indicated in [ ].
    • +
    • This assignment will be marked for correctness.
    • +
    • A solution will be posted on Monday after the due date.
    • +
    + +
    + +

    Note: In our lectures, we designed and evaluated the instruction set called x295M. In this +assignment, we shall design and evaluate three (3) other instruction sets: x295, x295+ +and x295++.

    + +

    Be aware: All of these 4 instruction sets are different!

    + +
    + +

    Instruction Set 1 – x295

    + +

    A. Description of x295 instruction set architecture (ISA)

    + +
      +
    • Data type size : 16 bits (for example, an integer has 16 bits)
    • +
    • Memory model +
        +
      • Size of memory: 212×162^{12} \times 16 +
          +
        • m = 12 -> this means that each memory address has 12 bits.
        • +
        • n = 16 -> this means that the address resolution, i.e., the smallest +addressable memory “chunk”, is a memory “chunk” of 16 bits. +
            +
          • Each memory “chunk” of 16 bits has a distinct memory address.
          • +
          • This is not a byte-addressable computer
          • +
          +
        • +
        +
      • +
      • Word size: 16 bits -> this means that when the microprocessor reads from/writes +to memory, it reads/writes 16 bits at a time. +
          +
        • In this ISA, the address resolution == the word size. It is not always the case.
        • +
        +
      • +
      • Number of registers: 0
      • +
      +
    • +
    • Instruction set (assembly and machine instructions) +
        +
      • Maximum number of instructions: 16 +
          +
        • This means that we need a maximum of 4 bits to distinctly represent each +of these 16 instructions.
        • +
        • Therefore, the size of the opcode field, in the machine instructions, will be +4 bits (24=162^4 = 16)
        • +
        +
      • +
      • Operand Model: +
          +
        • Memory (only) – only memory locations are operands, no registers are +used as operands except the register representing the stack pointer
        • +
        • 3-operand model
        • +
        • In the machine instructions, the order of these operands is: Dest, Src1, Src2
        • +
        +
      • +
      • Memory addressing mode: Direct, Base and Displacement and Indirect +
          +
        • These may not all be used in the instructions found in this assignment.
        • +
        +
      • +
      • Assembly instructions (in this assignment we shall only define a subset of these +instructions) and their format and meaning: +
          +
        • ADD a,b,c; meaning: M[c] <- M[a] + M[b]
        • +
        • SUB a,b,c; meaning: M[c] <- M[a] - M[b]
        • +
        • MUL a,b,c; meaning: M[c] <- M[a] * M[b]
        • +
        • In these assembly instructions, the order of the operands is: Src1, Src2, +Dest.
        • +
        +
      • +
      • Machine code format: +opcode +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          labelbits
          opcode4
          dest12
          padding4
          src12
          padding4
          src212
          +
        • +
        • This format is made of 3 words, each word is 16 bits in length (word size). This +format must be used to form all three machine instructions corresponding to the +three assembly instructions listed above. +The bit patterns for the opcode are: +
            +
          • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
            Opcode (instruction)bit pattern (4 bits)
            padding0000
            ADD0001
            SUB0010
            MUL0011
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    B. Evaluation of x295 instruction set

    + + + + + + + + + + + + + + + + +
    C programx295 assembly programx295 machine code
    z = (x + y) * (x - y);
    ADD x,y,tmp1
    +SUB x,y,imp2
    +MUL tmp1,tmp2,z
    (where tmp1 and tmp2 are memory addresses for holding temporary resylts)
    0001 <Dest 12 bits>
    +0000 <Src1 12 bits>
    +0000 <Src2 12 bits>
    +
    +0011 <Dest 12 bits>
    +0000 <Src1 12 bits>
    +0000 <Src2 12 bits>
    +
    + +Note: in the above machine code, we arbitarily express the memory address as `<Dest 12 bits>`, `<Src1 12 bits>` and `<Src2 12 bits>` as opposed to using actual memory addresses in the memory address fields (fields in between < and >). +
    + +

    1) The first step in evaluating our x295 instruction set is to write an assembly program using +the assembly instructions defined by our x295 instruction set. This step has been done +for us and is displayed in the table above in column “x295 assembly program”.

    + +

    Note that as we are performing this step, we are verifying that our x295 instruction set +contains sufficient instructions to allow us to write such program in x295 assembly code.

    + +

    (There is nothing for us to do in this part of our x295 instruction set evaluation. We can +use this as a model to follow when answering the rest of this assignment.)

    + +

    Once we have our x295 assembly program, we need to transform it into its x295 +machine code equivalent. This step has been done for us and is displayed in the table +above in column “x295 machine code”.

    + +

    Note that as we are performing this step, we are verifying that our x295 instruction set +contains sufficient instructions to allow us to write such program in x295 machine code.

    + +

    (Again, there is nothing for us to do in this part of our x295 instruction set evaluation. +We can use this as a model to follow when answering the rest of this assignment.)

    + +

    2) The next step is evaluating our x295 instruction set is to execute (hand trace) our +assembly program or its corresponding machine code and using the metric (criteria) called +memory traffic, we count the number of memory accesses our program makes during its +execution. In other words, we count how many time the execution of our program +required a word (16 bits) to be read from or written to memory.

    + +

    Note that as we are performing this step, we are verifying that the meaning of the +instructions contained in our x295 instruction set is such that hand tracing these +instructions does indeed produce the result the software developer that wrote the above +C program expected. This is to say that if we use the test case: x = 3, y = 2 when hand +tracing our x295 assembly code (or its machine code equivalent), we would obtain the +same (expected) result as if we were to hand trace the C program itself.

    + +

    For us to do: As part of this step in the evaluation of our x295 instruction set, we are +asked to evaluate the number of word size memory accesses made by the +microprocessor when it is fetching and decoding/executing each assembly code +statement (or machine code statement) listed in the left column in the table below. Also +we need to justify our count. Finally, let’s total our counts. [2 marks]

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    x295 program (in machine language and assembly code)Fetch (number of word size memory accessors) + Provide an expaination explaining the countDecode/Execute (number of word size memory accessors) + Provide an explaination explaining the count
    ADD x,y,tmp1
    +
    +
    0001 <Dest 12 bits>
    +0000 <Src1 12 bits>
    +0000 <Src2 12 bits>
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    SUB x,y,tmp2
    +
    +
    0010 <Dest 12 bits>
    +0000 <Src1 12 bits>
    +0000 <Src2 12 bits>
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    MUL tmp1,tmp2,z
    +
    +
    0011 <Dest 12 bits>
    +0000 <Src1 12 bits>
    +0000 <Src2 12 bits>
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    Grand total: ____Total: ____Total: ____
    + +

    3) We also evaluate our instruction set using the metric (criteria) called static code size:

    + +
      +
    • The code size of our x295 program is 3 instructions.
    • +
    • And since each instruction is 3 word long, the code size of our x295 program is 9 +words.
    • +
    + +

    (There is nothing for us to do in this part of our x295 instruction set evaluation. We can +use this as a model to follow when answering the rest of this assignment.)

    + +
    + +

    Instruction Set 2 – x295+

    + +

    A. Description of x295+ instruction set architecture (ISA)

    + +

    Considering the results of our x295 instruction set evaluation, one possible improvement in +order to reduce the number of memory accesses could be to reduce the size of the machine +instructions. Since these are made of an opcode and a memory address, we could introduce +registers which could hold these memory addresses and take less space in the machine code +than memory addresses (i.e., registers will take fewer bits to identify than memory addresses).

    + +

    With the idea of introducing registers, we specified a second instruction set architecture (ISA) +with the same components as x295, but with the following modifications:

    + +
      +
    • Memory model +
        +
      • Number of registers: 8 x 16-bit registers +
          +
        • This means that we need a maximum of 3 bits to distinctly represent each +of these 8 (232^3) registers.
        • +
        • Therefore, the size of any register field, in the machine instructions, will be +3 bits in length.
        • +
        • Each register contains 16 bits.
        • +
        +
      • +
      • The bit patterns to uniquely identify each the registers are: +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          RegisterBit pattern (3 bits)
          r0000
          r1001
          r2010
          r3011
          r4100
          r5101
          r6110
          r7111
          +
        • +
        +
      • +
      +
    • +
    • Instruction set (assembly and machine instructions) +
        +
      • Operand Model: +
          +
        • Registers
        • +
        • We shall also use memory addresses
        • +
        +
      • +
      • Assembly instructions: +
          +
        • ADD rA,rB,rC; meaning: rC <- rA + rB
        • +
        • SUB rA,rB,rC; meaning: rC <- rA - rB
        • +
        • MUL rA,rB,rC; meaning: rC <- rA * rB
        • +
        • LOAD a,rC; meaning: rC <- M[a]
        • +
        • STORE rA,c; meaning: M[c] <- rA
        • +
        +
      • +
      • Machine code format: +
          +
        • Format 1: +
            +
          • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
            LabelBits
            Opcode4 bits
            Dest3 bits
            Src13 bits
            Src23 bits
            XXXXXX
            +
          • +
          • This format is made of 16 bits (1 word) and it can be used to form the machine instruction corresponding to the ADD, SUB and MUL assembly instructions.
          • +
          • XXX -> these’s X’s can be either 0’s or 1’s – they are not used by the microproccesor.
          • +
          +
        • +
        +
      • +
      • Format 2: +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelBits
          Opcode4 bits
          Dest3 bits
          XXX3 bits
          XXXXXX6 bits
          padding4 bits
          Memory address of Src12 bits
          +
        • +
        • This format is made of 32 bits (2 words) and it can be used to form the machine instuctions corresponding to the LOAD assembly instruction.
        • +
        • XXX, XXXXXX and XXXX (padding) -> These X’s can be either 0’s or 1’s – they are not used by the microproccesor.
        • +
        +
      • +
      • Format 3: +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelBits
          Opcode4 bits
          XXX3 bits
          Src3 bits
          XXXXXX6 bits
          padding3 bits
          Memory address of Dest12 bits
          +
        • +
        • This format is made of 32 bits (2 words) and it can be used to form the machine instruction corresponding to the STORE assembly instruction.
        • +
        • XXX and XXXXXX -> these X’s can be either 0’s or 1’s – they are not used by the microprocessor.
        • +
        +
      • +
      +
    • +
    + +

    When the microprocessor decodes the opcode for a LOAD instruction, it will +know where to find the field Dest and where to find the field Src in the +instruction itself (Dest -> the first field after the opcode in the first word of the +insruction and Src -> the memory address located in the least significant 12 bits +of the second word composing this instruction). It will also know to ignore all +other bits (X’s) in the instruction.

    + +

    When the microprocessor decodes the opcode for a STORE instruction, it will +know where to find the field Src and where to find the field Dest in the +instruction itself (Src -> the second field after the opcode in the first word of the +insruction and Dest -> the memory address located in the least significant 12 bits +of the second word composing this instruction). It will also know to ignore all +other bits (X’s) in the instruction.

    + +

    Note: About ISA design principles: When creating formats to encode instruction +set …

    + +
      +
    • As few of them as possible were created.
    • +
    • The fields that have the same purpose (such as Opcode, Dest and Src) +are placed in the same location in as many of the formats as possible.
    • +
    + +

    This helps simplify the design of the microprocessor (its datapath). +The bit patterns for the opcode are:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Opcode (instruction)Bit pattern (4 bits)
    padding0000
    ADD0001
    SUB0010
    MUL0011
    LOAD1010
    STORE1011
    + +

    B. Description of x295+ instruction set

    + + + + + + + + + + + + + + + + +
    C programx295+ assembly programx295+ machine code
    z = (x + y) * (x - y);
    (empty)
    1010 000 XXX XXXXXX 0000 <Src 12 bits>
    +
    +1010 001 XXX XXXXXX 0000 <Src 12 bits>
    +
    +0001 010 000 001 XXX
    +
    +0010 011 000 001 XXX
    +
    +0011 100 010 011 XXX
    +
    +1011 XXX 100 XXXXXX 0000 <Src 12 bits>
    + +

    1) The first step in evaluating our x295+ instruction set is to translate the C program into

    + +
      +
    • an assembly program using the assembly instructions defined by our x295+ +instruction set and
    • +
    • its corresponding machine code using the machine instructions defined by our +x295+ instruction set.
    • +
    + +

    For us to do: Our task is to complete the middle column (assembly code) in the table +above. The machine code has already been given. [3 marks]

    + +

    2) The next step in evaluating our x295+ instruction set is to execute (hand trace) our +assembly program or its corresponding machine code and using the metric (criteria) called +memory traffic, we count the number of memory accesses our program makes during its +execution. In other words, we count how many time the execution of our program +required a word (16 bits) to be read from or written to memory.

    + +

    For us to do: As part of this step in the evaluation of our x295+ instruction set, complete +the table below. [4 marks]

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    x295+ program (in assembly and machine code)Fetch (number of word size memory accesses) + Provide an explaination explaining the countDecode/Execute (number of word size memory accesses) + Provide an explaination explaining the count
    Assembly code: ____
    +Machine code:
    1010 000 XXX XXXXXX 0000 <Src 12 bits>
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaionation ____
    Assembly code: ____
    +Machine code:
    1010 001 XXX XXXXXX 0000 <Src 12 bits>
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    Assembly code: ____
    +Machine code:
    0001 010 000 001 XXX
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    Assembly code: ____
    +Machine code:
    0010 011 000 001 XXX
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    Assembly code: ____
    +Machine code:
    0011 100 010 011 XXX
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    Assembly code: ____
    +Machine code:
    1011 XXX 100 XXXXXX 0000 <Src 12 bits>
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    Grand total: ____Total: ____Total: ____
    + +

    3) We also evaluate our instruction set using the metric (criteria) called static code size. For +us to do: Fill in the blanks in the statement below: [0.5 marks]

    +
      +
    • The code size of our x295+ program is ____ instructions ( ____ words).
    • +
    + +

    Instruction Set 3 – x295++

    + +

    A. Description of x295++ instruction set architecture (ISA)

    + +

    Would reducing the number of operands to the instructions in our instruction set decrease +the number of memory accesses the microprocessor does when fetching, decoding and +executing our program?

    + +

    Would introducing another instruction, namely COPY, in our instruction set also decrease the +number of memory accesses the microprocessor does when fetching. decoding and executing +our program?

    + +

    With the above two ideas in mind, we specified a third instruction set architecture (ISA) with +the same components as x295 and x295+, but with the following modifications:

    + +
      +
    • Instruction set (assembly and machine instructions) +
        +
      • Operand Model: +
          +
        • 2 operand model
        • +
        +
      • +
      • Assembly instructions: +
          +
        • ADD rA,rC; meaning: rC <- rA + rC
        • +
        • SUB rA,rC; meaning: rC <- rA - rC
        • +
        • MUL rA,rC; meaning: rC <- rA * rB
        • +
        • COPY rA,rC; meaning: rC <- rA
        • +
        • LOAD a,rC; meaning: rC <- M[a]
        • +
        • STORE rA,c; meaning: M[c] <- rA
        • +
        +
      • +
      • Machine code formats: +
          +
        • Format 1: +
            +
          • + + + + + + + + + + + + + + + + + + + + + + + + + +
            LablelBits
            Opcode4 bits
            Dest3 bits
            Src3 bits
            XXXXXX6 bits
            +
          • +
          • This format is made of 16 bits (1 word) and it can be used to form the machine +instructions corresponding to the ADD, SUB, MUL and COPY assembly instructions.
          • +
          • XXXXXX -> these X’s can be either 0’s or 1’s.
          • +
          • The bit patterns for the opcode are: +Opcode (instruction)|Bit pattern (4 bits) +—|— +padding|0000 +ADD|0001 +SUB|0010 +MUL|0011 +COPY|1001 +LOAD|1010 +STORE|1011 +…|…
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    B. Evaluation of x295++ instruction set

    + + + + + + + + + + + + + + +
    C programx295++ assembly programx295++ machine code
    z = (x + y) * (x – y);  
    + +

    1) The first step in evaluating our x295++ instruction set is to translate the C program into

    +
      +
    • an assembly program using the assembly instructions defined by our x295++ +instruction set and
    • +
    • its corresponding machine code using the machine instructions defined by our +x295++ instruction set.
    • +
    • For us to do: Our task is to complete the middle column (assembly code) and the right +column (machine code) in the table above. [5 marks] +
        +
      • Challenge: Can you express your x295++ assembly program and machine code +with the fewest number of instructions. This may require you to first +readjust the above C program.
      • +
      +
    • +
    + +

    2) The next step in evaluating our x295++ instruction set is to execute (hand trace) our +assembly program or its corresponding machine code and using the metric (criteria) called +memory traffic, we count the number of memory accesses our program makes during its +execution. In other words, we count how many time the execution of our program +required a word (16 bits) to be read from or written to memory.

    + +

    For us to do: As part of this step in the evaluation of our x295++ instruction set, +complete the table below. [4 marks]

    + + + + + + + + + + + + + + + + + + + +
    x295++ program (in assembly and machine code)Fetch (number of word size memory accesses) + Provide an explaination explaining the countDecode/Execute (number of word size memory accesses) + Provide an explaination explainaing the count
    ... expand the table by adding as many rows as needed using the +row format seen in the tables above ...
    Grand total: ____Total: ____Total: ____
    + +

    3) We also evaluate our instruction set using the metric (criteria) called static code size. For +us to do: Fill in the blanks in the statement below: [0.5 marks]

    +
      +
    • The code size of our x295++ program is ___ instructions ( _______ words).
    • +
    + +

    Conclusion

    + +
    + +

    For us to do: Considering the memory traffic metric (number of memory accesses required +by our test program), which instruction set (x295, x295+ or x295++) produces the most +time efficient program? [0.5 marks]

    + +

    For us to do: Considering the static code size metric (number of instructions/words required +to implement our test program), which instruction set (x295, x295+ or x295++) produces +the smallest program? [0.5 marks]

    + +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/asses/ass7/ass7/index.html b/_site/melody/cmpt-295/asses/ass7/ass7/index.html new file mode 100644 index 0000000..3efc47a --- /dev/null +++ b/_site/melody/cmpt-295/asses/ass7/ass7/index.html @@ -0,0 +1,930 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Assignment 7 - CMPT 295 – Fall 2021

    + +

    Objectives:

    + +
      +
    • Designing and evaluating instruction sets (ISA)
    • +
    + +
    + +

    Submission:

    + +
      +
    • Submit your document called Assignment_7.pdf, which must include your answers to all +of the questions in Assignment 7. +
        +
      • Add your full name and student number at the top of the first page of your +document Assignment_7.pdf.
      • +
      +
    • +
    • Submit your assignment Assignment_7.pdf on CourSys.
    • +
    + +
    + +

    Due:

    + +
      +
    • Friday Nov. 12 at 23:59:59.
    • +
    • Late assignments will receive a grade of 0, but they will be marked (if they are submitted +before the solutions are posted on Monday) in order to provide feedback to the +student.
    • +
    + +
    + +

    Marking scheme:

    + +
      +
    • The marks assigned to each question is indicated in [ ].
    • +
    • This assignment will be marked for correctness.
    • +
    • A solution will be posted on Monday after the due date.
    • +
    + +
    + +

    Note: In our lectures, we designed and evaluated the instruction set called x295M. In this +assignment, we shall design and evaluate three (3) other instruction sets: x295, x295+ +and x295++.

    + +

    Be aware: All of these 4 instruction sets are different!

    + +
    + +

    Instruction Set 1 – x295

    + +

    A. Description of x295 instruction set architecture (ISA)

    + +
      +
    • Data type size : 16 bits (for example, an integer has 16 bits)
    • +
    • Memory model +
        +
      • Size of memory: 212×162^{12} \times 16 +
          +
        • m = 12 -> this means that each memory address has 12 bits.
        • +
        • n = 16 -> this means that the address resolution, i.e., the smallest +addressable memory “chunk”, is a memory “chunk” of 16 bits. +
            +
          • Each memory “chunk” of 16 bits has a distinct memory address.
          • +
          • This is not a byte-addressable computer
          • +
          +
        • +
        +
      • +
      • Word size: 16 bits -> this means that when the microprocessor reads from/writes +to memory, it reads/writes 16 bits at a time. +
          +
        • In this ISA, the address resolution == the word size. It is not always the case.
        • +
        +
      • +
      • Number of registers: 0
      • +
      +
    • +
    • Instruction set (assembly and machine instructions) +
        +
      • Maximum number of instructions: 16 +
          +
        • This means that we need a maximum of 4 bits to distinctly represent each +of these 16 instructions.
        • +
        • Therefore, the size of the opcode field, in the machine instructions, will be +4 bits (24=162^4 = 16)
        • +
        +
      • +
      • Operand Model: +
          +
        • Memory (only) – only memory locations are operands, no registers are +used as operands except the register representing the stack pointer
        • +
        • 3-operand model
        • +
        • In the machine instructions, the order of these operands is: Dest, Src1, Src2
        • +
        +
      • +
      • Memory addressing mode: Direct, Base and Displacement and Indirect +
          +
        • These may not all be used in the instructions found in this assignment.
        • +
        +
      • +
      • Assembly instructions (in this assignment we shall only define a subset of these +instructions) and their format and meaning: +
          +
        • ADD a,b,c; meaning: M[c] <- M[a] + M[b]
        • +
        • SUB a,b,c; meaning: M[c] <- M[a] - M[b]
        • +
        • MUL a,b,c; meaning: M[c] <- M[a] * M[b]
        • +
        • In these assembly instructions, the order of the operands is: Src1, Src2, +Dest.
        • +
        +
      • +
      • Machine code format: +opcode +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          labelbits
          opcode4
          dest12
          padding4
          src12
          padding4
          src212
          +
        • +
        • This format is made of 3 words, each word is 16 bits in length (word size). This +format must be used to form all three machine instructions corresponding to the +three assembly instructions listed above. +The bit patterns for the opcode are: +
            +
          • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
            Opcode (instruction)bit pattern (4 bits)
            padding0000
            ADD0001
            SUB0010
            MUL0011
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    B. Evaluation of x295 instruction set

    + + + + + + + + + + + + + + + + +
    C programx295 assembly programx295 machine code
    z = (x + y) * (x - y);
    ADD x,y,tmp1
    +SUB x,y,imp2
    +MUL tmp1,tmp2,z
    (where tmp1 and tmp2 are memory addresses for holding temporary resylts)
    0001 <Dest 12 bits>
    +0000 <Src1 12 bits>
    +0000 <Src2 12 bits>
    +
    +0011 <Dest 12 bits>
    +0000 <Src1 12 bits>
    +0000 <Src2 12 bits>
    +
    + +Note: in the above machine code, we arbitarily express the memory address as `<Dest 12 bits>`, `<Src1 12 bits>` and `<Src2 12 bits>` as opposed to using actual memory addresses in the memory address fields (fields in between < and >). +
    + +

    1) The first step in evaluating our x295 instruction set is to write an assembly program using +the assembly instructions defined by our x295 instruction set. This step has been done +for us and is displayed in the table above in column “x295 assembly program”.

    + +

    Note that as we are performing this step, we are verifying that our x295 instruction set +contains sufficient instructions to allow us to write such program in x295 assembly code.

    + +

    (There is nothing for us to do in this part of our x295 instruction set evaluation. We can +use this as a model to follow when answering the rest of this assignment.)

    + +

    Once we have our x295 assembly program, we need to transform it into its x295 +machine code equivalent. This step has been done for us and is displayed in the table +above in column “x295 machine code”.

    + +

    Note that as we are performing this step, we are verifying that our x295 instruction set +contains sufficient instructions to allow us to write such program in x295 machine code.

    + +

    (Again, there is nothing for us to do in this part of our x295 instruction set evaluation. +We can use this as a model to follow when answering the rest of this assignment.)

    + +

    2) The next step is evaluating our x295 instruction set is to execute (hand trace) our +assembly program or its corresponding machine code and using the metric (criteria) called +memory traffic, we count the number of memory accesses our program makes during its +execution. In other words, we count how many time the execution of our program +required a word (16 bits) to be read from or written to memory.

    + +

    Note that as we are performing this step, we are verifying that the meaning of the +instructions contained in our x295 instruction set is such that hand tracing these +instructions does indeed produce the result the software developer that wrote the above +C program expected. This is to say that if we use the test case: x = 3, y = 2 when hand +tracing our x295 assembly code (or its machine code equivalent), we would obtain the +same (expected) result as if we were to hand trace the C program itself.

    + +

    For us to do: As part of this step in the evaluation of our x295 instruction set, we are +asked to evaluate the number of word size memory accesses made by the +microprocessor when it is fetching and decoding/executing each assembly code +statement (or machine code statement) listed in the left column in the table below. Also +we need to justify our count. Finally, let’s total our counts. [2 marks]

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    x295 program (in machine language and assembly code)Fetch (number of word size memory accessors) + Provide an expaination explaining the countDecode/Execute (number of word size memory accessors) + Provide an explaination explaining the count
    ADD x,y,tmp1
    +
    +
    0001 <Dest 12 bits>
    +0000 <Src1 12 bits>
    +0000 <Src2 12 bits>
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    SUB x,y,tmp2
    +
    +
    0010 <Dest 12 bits>
    +0000 <Src1 12 bits>
    +0000 <Src2 12 bits>
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    MUL tmp1,tmp2,z
    +
    +
    0011 <Dest 12 bits>
    +0000 <Src1 12 bits>
    +0000 <Src2 12 bits>
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    Grand total: ____Total: ____Total: ____
    + +

    3) We also evaluate our instruction set using the metric (criteria) called static code size:

    + +
      +
    • The code size of our x295 program is 3 instructions.
    • +
    • And since each instruction is 3 word long, the code size of our x295 program is 9 +words.
    • +
    + +

    (There is nothing for us to do in this part of our x295 instruction set evaluation. We can +use this as a model to follow when answering the rest of this assignment.)

    + +
    + +

    Instruction Set 2 – x295+

    + +

    A. Description of x295+ instruction set architecture (ISA)

    + +

    Considering the results of our x295 instruction set evaluation, one possible improvement in +order to reduce the number of memory accesses could be to reduce the size of the machine +instructions. Since these are made of an opcode and a memory address, we could introduce +registers which could hold these memory addresses and take less space in the machine code +than memory addresses (i.e., registers will take fewer bits to identify than memory addresses).

    + +

    With the idea of introducing registers, we specified a second instruction set architecture (ISA) +with the same components as x295, but with the following modifications:

    + +
      +
    • Memory model +
        +
      • Number of registers: 8 x 16-bit registers +
          +
        • This means that we need a maximum of 3 bits to distinctly represent each +of these 8 (232^3) registers.
        • +
        • Therefore, the size of any register field, in the machine instructions, will be +3 bits in length.
        • +
        • Each register contains 16 bits.
        • +
        +
      • +
      • The bit patterns to uniquely identify each the registers are: +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          RegisterBit pattern (3 bits)
          r0000
          r1001
          r2010
          r3011
          r4100
          r5101
          r6110
          r7111
          +
        • +
        +
      • +
      +
    • +
    • Instruction set (assembly and machine instructions) +
        +
      • Operand Model: +
          +
        • Registers
        • +
        • We shall also use memory addresses
        • +
        +
      • +
      • Assembly instructions: +
          +
        • ADD rA,rB,rC; meaning: rC <- rA + rB
        • +
        • SUB rA,rB,rC; meaning: rC <- rA - rB
        • +
        • MUL rA,rB,rC; meaning: rC <- rA * rB
        • +
        • LOAD a,rC; meaning: rC <- M[a]
        • +
        • STORE rA,c; meaning: M[c] <- rA
        • +
        +
      • +
      • Machine code format: +
          +
        • Format 1: +
            +
          • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
            LabelBits
            Opcode4 bits
            Dest3 bits
            Src13 bits
            Src23 bits
            XXXXXX
            +
          • +
          • This format is made of 16 bits (1 word) and it can be used to form the machine instruction corresponding to the ADD, SUB and MUL assembly instructions.
          • +
          • XXX -> these’s X’s can be either 0’s or 1’s – they are not used by the microproccesor.
          • +
          +
        • +
        +
      • +
      • Format 2: +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelBits
          Opcode4 bits
          Dest3 bits
          XXX3 bits
          XXXXXX6 bits
          padding4 bits
          Memory address of Src12 bits
          +
        • +
        • This format is made of 32 bits (2 words) and it can be used to form the machine instuctions corresponding to the LOAD assembly instruction.
        • +
        • XXX, XXXXXX and XXXX (padding) -> These X’s can be either 0’s or 1’s – they are not used by the microproccesor.
        • +
        +
      • +
      • Format 3: +
          +
        • + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
          LabelBits
          Opcode4 bits
          XXX3 bits
          Src3 bits
          XXXXXX6 bits
          padding3 bits
          Memory address of Dest12 bits
          +
        • +
        • This format is made of 32 bits (2 words) and it can be used to form the machine instruction corresponding to the STORE assembly instruction.
        • +
        • XXX and XXXXXX -> these X’s can be either 0’s or 1’s – they are not used by the microprocessor.
        • +
        +
      • +
      +
    • +
    + +

    When the microprocessor decodes the opcode for a LOAD instruction, it will +know where to find the field Dest and where to find the field Src in the +instruction itself (Dest -> the first field after the opcode in the first word of the +insruction and Src -> the memory address located in the least significant 12 bits +of the second word composing this instruction). It will also know to ignore all +other bits (X’s) in the instruction.

    + +

    When the microprocessor decodes the opcode for a STORE instruction, it will +know where to find the field Src and where to find the field Dest in the +instruction itself (Src -> the second field after the opcode in the first word of the +insruction and Dest -> the memory address located in the least significant 12 bits +of the second word composing this instruction). It will also know to ignore all +other bits (X’s) in the instruction.

    + +

    Note: About ISA design principles: When creating formats to encode instruction +set …

    + +
      +
    • As few of them as possible were created.
    • +
    • The fields that have the same purpose (such as Opcode, Dest and Src) +are placed in the same location in as many of the formats as possible.
    • +
    + +

    This helps simplify the design of the microprocessor (its datapath). +The bit patterns for the opcode are:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Opcode (instruction)Bit pattern (4 bits)
    padding0000
    ADD0001
    SUB0010
    MUL0011
    LOAD1010
    STORE1011
    + +

    B. Description of x295+ instruction set

    + + + + + + + + + + + + + + + + +
    C programx295+ assembly programx295+ machine code
    z = (x + y) * (x - y);
    (empty)
    1010 000 XXX XXXXXX 0000 <Src 12 bits>
    +
    +1010 001 XXX XXXXXX 0000 <Src 12 bits>
    +
    +0001 010 000 001 XXX
    +
    +0010 011 000 001 XXX
    +
    +0011 100 010 011 XXX
    +
    +1011 XXX 100 XXXXXX 0000 <Src 12 bits>
    + +

    1) The first step in evaluating our x295+ instruction set is to translate the C program into

    + +
      +
    • an assembly program using the assembly instructions defined by our x295+ +instruction set and
    • +
    • its corresponding machine code using the machine instructions defined by our +x295+ instruction set.
    • +
    + +

    For us to do: Our task is to complete the middle column (assembly code) in the table +above. The machine code has already been given. [3 marks]

    + +

    2) The next step in evaluating our x295+ instruction set is to execute (hand trace) our +assembly program or its corresponding machine code and using the metric (criteria) called +memory traffic, we count the number of memory accesses our program makes during its +execution. In other words, we count how many time the execution of our program +required a word (16 bits) to be read from or written to memory.

    + +

    For us to do: As part of this step in the evaluation of our x295+ instruction set, complete +the table below. [4 marks]

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    x295+ program (in assembly and machine code)Fetch (number of word size memory accesses) + Provide an explaination explaining the countDecode/Execute (number of word size memory accesses) + Provide an explaination explaining the count
    Assembly code: ____
    +Machine code:
    1010 000 XXX XXXXXX 0000 <Src 12 bits>
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaionation ____
    Assembly code: ____
    +Machine code:
    1010 001 XXX XXXXXX 0000 <Src 12 bits>
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    Assembly code: ____
    +Machine code:
    0001 010 000 001 XXX
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    Assembly code: ____
    +Machine code:
    0010 011 000 001 XXX
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    Assembly code: ____
    +Machine code:
    0011 100 010 011 XXX
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    Assembly code: ____
    +Machine code:
    1011 XXX 100 XXXXXX 0000 <Src 12 bits>
    Count: ____
    +Explaination: ____
    Count: ____
    +Explaination: ____
    Grand total: ____Total: ____Total: ____
    + +

    3) We also evaluate our instruction set using the metric (criteria) called static code size. For +us to do: Fill in the blanks in the statement below: [0.5 marks]

    +
      +
    • The code size of our x295+ program is ____ instructions ( ____ words).
    • +
    + +

    Instruction Set 3 – x295++

    + +

    A. Description of x295++ instruction set architecture (ISA)

    + +

    Would reducing the number of operands to the instructions in our instruction set decrease +the number of memory accesses the microprocessor does when fetching, decoding and +executing our program?

    + +

    Would introducing another instruction, namely COPY, in our instruction set also decrease the +number of memory accesses the microprocessor does when fetching. decoding and executing +our program?

    + +

    With the above two ideas in mind, we specified a third instruction set architecture (ISA) with +the same components as x295 and x295+, but with the following modifications:

    + +
      +
    • Instruction set (assembly and machine instructions) +
        +
      • Operand Model: +
          +
        • 2 operand model
        • +
        +
      • +
      • Assembly instructions: +
          +
        • ADD rA,rC; meaning: rC <- rA + rC
        • +
        • SUB rA,rC; meaning: rC <- rA - rC
        • +
        • MUL rA,rC; meaning: rC <- rA * rB
        • +
        • COPY rA,rC; meaning: rC <- rA
        • +
        • LOAD a,rC; meaning: rC <- M[a]
        • +
        • STORE rA,c; meaning: M[c] <- rA
        • +
        +
      • +
      • Machine code formats: +
          +
        • Format 1: +
            +
          • + + + + + + + + + + + + + + + + + + + + + + + + + +
            LablelBits
            Opcode4 bits
            Dest3 bits
            Src3 bits
            XXXXXX6 bits
            +
          • +
          • This format is made of 16 bits (1 word) and it can be used to form the machine +instructions corresponding to the ADD, SUB, MUL and COPY assembly instructions.
          • +
          • XXXXXX -> these X’s can be either 0’s or 1’s.
          • +
          • The bit patterns for the opcode are: +Opcode (instruction)|Bit pattern (4 bits) +—|— +padding|0000 +ADD|0001 +SUB|0010 +MUL|0011 +COPY|1001 +LOAD|1010 +STORE|1011 +…|…
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    B. Evaluation of x295++ instruction set

    + + + + + + + + + + + + + + +
    C programx295++ assembly programx295++ machine code
    z = (x + y) * (x – y);  
    + +

    1) The first step in evaluating our x295++ instruction set is to translate the C program into

    +
      +
    • an assembly program using the assembly instructions defined by our x295++ +instruction set and
    • +
    • its corresponding machine code using the machine instructions defined by our +x295++ instruction set.
    • +
    • For us to do: Our task is to complete the middle column (assembly code) and the right +column (machine code) in the table above. [5 marks] +
        +
      • Challenge: Can you express your x295++ assembly program and machine code +with the fewest number of instructions. This may require you to first +readjust the above C program.
      • +
      +
    • +
    + +

    2) The next step in evaluating our x295++ instruction set is to execute (hand trace) our +assembly program or its corresponding machine code and using the metric (criteria) called +memory traffic, we count the number of memory accesses our program makes during its +execution. In other words, we count how many time the execution of our program +required a word (16 bits) to be read from or written to memory.

    + +

    For us to do: As part of this step in the evaluation of our x295++ instruction set, +complete the table below. [4 marks]

    + + + + + + + + + + + + + + + + + + + +
    x295++ program (in assembly and machine code)Fetch (number of word size memory accesses) + Provide an explaination explaining the countDecode/Execute (number of word size memory accesses) + Provide an explaination explainaing the count
    ... expand the table by adding as many rows as needed using the +row format seen in the tables above ...
    Grand total: ____Total: ____Total: ____
    + +

    3) We also evaluate our instruction set using the metric (criteria) called static code size. For +us to do: Fill in the blanks in the statement below: [0.5 marks]

    +
      +
    • The code size of our x295++ program is ___ instructions ( _______ words).
    • +
    + +

    Conclusion

    + +
    + +

    For us to do: Considering the memory traffic metric (number of memory accesses required +by our test program), which instruction set (x295, x295+ or x295++) produces the most +time efficient program? [0.5 marks]

    + +

    For us to do: Considering the static code size metric (number of instructions/words required +to implement our test program), which instruction set (x295, x295+ or x295++) produces +the smallest program? [0.5 marks]

    + +
    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/asses/cmpt-295-ass3.zip b/_site/melody/cmpt-295/asses/cmpt-295-ass3.zip new file mode 100644 index 0000000..b721a5a Binary files /dev/null and b/_site/melody/cmpt-295/asses/cmpt-295-ass3.zip differ diff --git a/_site/melody/cmpt-295/asses/cmpt-295-ass4.zip b/_site/melody/cmpt-295/asses/cmpt-295-ass4.zip new file mode 100644 index 0000000..98ca003 Binary files /dev/null and b/_site/melody/cmpt-295/asses/cmpt-295-ass4.zip differ diff --git a/_site/melody/cmpt-295/asses/cmpt-295-ass5.zip b/_site/melody/cmpt-295/asses/cmpt-295-ass5.zip new file mode 100644 index 0000000..e8e58c8 Binary files /dev/null and b/_site/melody/cmpt-295/asses/cmpt-295-ass5.zip differ diff --git a/_site/melody/cmpt-295/asses/cmpt-295-ass6.zip b/_site/melody/cmpt-295/asses/cmpt-295-ass6.zip new file mode 100644 index 0000000..79e05f6 Binary files /dev/null and b/_site/melody/cmpt-295/asses/cmpt-295-ass6.zip differ diff --git a/_site/melody/cmpt-295/asses/cmpt-295-ass7.zip b/_site/melody/cmpt-295/asses/cmpt-295-ass7.zip new file mode 100644 index 0000000..92472ac Binary files /dev/null and b/_site/melody/cmpt-295/asses/cmpt-295-ass7.zip differ diff --git a/_site/melody/cmpt-295/asses/cmpt295-ass2.zip b/_site/melody/cmpt-295/asses/cmpt295-ass2.zip new file mode 100644 index 0000000..868823f Binary files /dev/null and b/_site/melody/cmpt-295/asses/cmpt295-ass2.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-09.zip b/_site/melody/cmpt-295/cmpt-295-09.zip new file mode 100644 index 0000000..75d2413 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-09.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-10-annotated.zip b/_site/melody/cmpt-295/cmpt-295-10-annotated.zip new file mode 100644 index 0000000..5599028 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-10-annotated.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-10.zip b/_site/melody/cmpt-295/cmpt-295-10.zip new file mode 100644 index 0000000..1023b93 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-10.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-11.zip b/_site/melody/cmpt-295/cmpt-295-11.zip new file mode 100644 index 0000000..df1cfca Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-11.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-12.zip b/_site/melody/cmpt-295/cmpt-295-12.zip new file mode 100644 index 0000000..cb85627 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-12.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-13-updates.zip b/_site/melody/cmpt-295/cmpt-295-13-updates.zip new file mode 100644 index 0000000..5339de6 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-13-updates.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-13.zip b/_site/melody/cmpt-295/cmpt-295-13.zip new file mode 100644 index 0000000..1c27e51 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-13.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-14-updated.zip b/_site/melody/cmpt-295/cmpt-295-14-updated.zip new file mode 100644 index 0000000..0e0e57c Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-14-updated.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-14.zip b/_site/melody/cmpt-295/cmpt-295-14.zip new file mode 100644 index 0000000..07b3a32 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-14.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-15.zip b/_site/melody/cmpt-295/cmpt-295-15.zip new file mode 100644 index 0000000..97b673b Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-15.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-16.zip b/_site/melody/cmpt-295/cmpt-295-16.zip new file mode 100644 index 0000000..b0c76f6 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-16.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-17.zip b/_site/melody/cmpt-295/cmpt-295-17.zip new file mode 100644 index 0000000..cde01d5 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-17.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-18.zip b/_site/melody/cmpt-295/cmpt-295-18.zip new file mode 100644 index 0000000..a58be0d Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-18.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-19.zip b/_site/melody/cmpt-295/cmpt-295-19.zip new file mode 100644 index 0000000..fb0b29c Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-19.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-20.zip b/_site/melody/cmpt-295/cmpt-295-20.zip new file mode 100644 index 0000000..b6fbd13 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-20.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-21.zip b/_site/melody/cmpt-295/cmpt-295-21.zip new file mode 100644 index 0000000..9994618 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-21.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-22.zip b/_site/melody/cmpt-295/cmpt-295-22.zip new file mode 100644 index 0000000..632ea4e Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-22.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-23.zip b/_site/melody/cmpt-295/cmpt-295-23.zip new file mode 100644 index 0000000..236e99f Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-23.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-24.zip b/_site/melody/cmpt-295/cmpt-295-24.zip new file mode 100644 index 0000000..1cca09e Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-24.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-25.zip b/_site/melody/cmpt-295/cmpt-295-25.zip new file mode 100644 index 0000000..fa93dea Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-25.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-26.zip b/_site/melody/cmpt-295/cmpt-295-26.zip new file mode 100644 index 0000000..fa7467a Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-26.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-demo.zip b/_site/melody/cmpt-295/cmpt-295-demo.zip new file mode 100644 index 0000000..3756de6 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-demo.zip differ diff --git a/_site/melody/cmpt-295/cmpt-295-midterm2.zip b/_site/melody/cmpt-295/cmpt-295-midterm2.zip new file mode 100644 index 0000000..6dbaaf9 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt-295-midterm2.zip differ diff --git a/_site/melody/cmpt-295/cmpt205-06.zip b/_site/melody/cmpt-295/cmpt205-06.zip new file mode 100644 index 0000000..04257b7 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt205-06.zip differ diff --git a/_site/melody/cmpt-295/cmpt205-07.zip b/_site/melody/cmpt-295/cmpt205-07.zip new file mode 100644 index 0000000..e5a52a0 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt205-07.zip differ diff --git a/_site/melody/cmpt-295/cmpt205-08.zip b/_site/melody/cmpt-295/cmpt205-08.zip new file mode 100644 index 0000000..d98bc06 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt205-08.zip differ diff --git a/_site/melody/cmpt-295/cmpt295-05.zip b/_site/melody/cmpt-295/cmpt295-05.zip new file mode 100644 index 0000000..9d338f7 Binary files /dev/null and b/_site/melody/cmpt-295/cmpt295-05.zip differ diff --git a/_site/melody/cmpt-295/computer-bus/Computer_Bus.pdf b/_site/melody/cmpt-295/computer-bus/Computer_Bus.pdf new file mode 100644 index 0000000..67d0497 Binary files /dev/null and b/_site/melody/cmpt-295/computer-bus/Computer_Bus.pdf differ diff --git a/_site/melody/cmpt-295/computer-bus/Computer_Bus.pdf.txt b/_site/melody/cmpt-295/computer-bus/Computer_Bus.pdf.txt new file mode 100644 index 0000000..1f53a6d --- /dev/null +++ b/_site/melody/cmpt-295/computer-bus/Computer_Bus.pdf.txt @@ -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. diff --git a/_site/melody/cmpt-295/demo/Demo_Copy_Annotated.pdf b/_site/melody/cmpt-295/demo/Demo_Copy_Annotated.pdf new file mode 100644 index 0000000..6d546e1 Binary files /dev/null and b/_site/melody/cmpt-295/demo/Demo_Copy_Annotated.pdf differ diff --git a/_site/melody/cmpt-295/demo/demo.html b/_site/melody/cmpt-295/demo/demo.html new file mode 100644 index 0000000..220b883 --- /dev/null +++ b/_site/melody/cmpt-295/demo/demo.html @@ -0,0 +1,67 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    Demo

    + +
      .global copy
    +copy:
    +# A in rdi, C in rsi, N in edx
    +  xorl %eax,%eax # set eax to 0
    +# since this is a leaf function, no need to save caller-saved registers rcx and r8
    +  xorl %ecx,%ecx # row number i is in ecx -> i = 0; red highlight
    +
    +# For each row
    +rowLoop:
    +  movl $0,%r8d # column number j in r8d -> j = 0; pink highlight
    +  cmpl %edx,%ecx # loop as long as i - N < 0
    +  jqe doneWithRows # yellow highlight
    +
    +# For each cell of this row
    +colLoop:
    +  cmpl %edx,%r8d # loop as long as j - N < 0
    +  jge doneWithCells # blue highlight
    +
    +# Compute the address of current cell that is copied from A to C
    +# since this function is a leaf function no need to save caller-saved registers r10 and r11
    +# Memory computation: A[i][j] = A + (i*C*L) + (j*L) = A + L * ((i*c)+j)
    +#     if C and R => N = A+L * (i*N+j)
    +  movl %edx,%r10d # r10d = N
    +  imull %ecx,%r10d # r10d = i*N
    +  addl %r8d,%r10d # i*N + j
    +  imull $1,%r10d # r10 = L*(i*N+j) -> L is char (1 Byte); highlighted
    +  movq %r10,%r11 # r11 = L*(i*N+j)
    +  addq %rdi,%r10 # r10 = A+L*(i*N+j)
    +  addq %rsi,%r11 # r11 = C+L*(i*N+j)
    +
    +# Copy A[L*(i*N+j)] to C[L*(i*N+j)]
    +  movb (%r10),%r9b # temp = A[L*(i*N+j)]
    +  movb %r9b,(%r11) # C[L*(i*N+j)] = temp
    +
    +  incl %r8d # column number j++ (in r8d)
    +  jmp colLoop # go to the next cell
    +
    +# Go to next row
    +doneWithCells: # blue highlight
    +  incl %ecx # row number i++ (in ecx)
    +  jmp rowLoop # Play it again, Sam!
    +
    +doneWithRows: # bye bye!; yellow highlight
    +  ret
    +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/demo/demo/index.html b/_site/melody/cmpt-295/demo/demo/index.html new file mode 100644 index 0000000..220b883 --- /dev/null +++ b/_site/melody/cmpt-295/demo/demo/index.html @@ -0,0 +1,67 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    Demo

    + +
      .global copy
    +copy:
    +# A in rdi, C in rsi, N in edx
    +  xorl %eax,%eax # set eax to 0
    +# since this is a leaf function, no need to save caller-saved registers rcx and r8
    +  xorl %ecx,%ecx # row number i is in ecx -> i = 0; red highlight
    +
    +# For each row
    +rowLoop:
    +  movl $0,%r8d # column number j in r8d -> j = 0; pink highlight
    +  cmpl %edx,%ecx # loop as long as i - N < 0
    +  jqe doneWithRows # yellow highlight
    +
    +# For each cell of this row
    +colLoop:
    +  cmpl %edx,%r8d # loop as long as j - N < 0
    +  jge doneWithCells # blue highlight
    +
    +# Compute the address of current cell that is copied from A to C
    +# since this function is a leaf function no need to save caller-saved registers r10 and r11
    +# Memory computation: A[i][j] = A + (i*C*L) + (j*L) = A + L * ((i*c)+j)
    +#     if C and R => N = A+L * (i*N+j)
    +  movl %edx,%r10d # r10d = N
    +  imull %ecx,%r10d # r10d = i*N
    +  addl %r8d,%r10d # i*N + j
    +  imull $1,%r10d # r10 = L*(i*N+j) -> L is char (1 Byte); highlighted
    +  movq %r10,%r11 # r11 = L*(i*N+j)
    +  addq %rdi,%r10 # r10 = A+L*(i*N+j)
    +  addq %rsi,%r11 # r11 = C+L*(i*N+j)
    +
    +# Copy A[L*(i*N+j)] to C[L*(i*N+j)]
    +  movb (%r10),%r9b # temp = A[L*(i*N+j)]
    +  movb %r9b,(%r11) # C[L*(i*N+j)] = temp
    +
    +  incl %r8d # column number j++ (in r8d)
    +  jmp colLoop # go to the next cell
    +
    +# Go to next row
    +doneWithCells: # blue highlight
    +  incl %ecx # row number i++ (in ecx)
    +  jmp rowLoop # Play it again, Sam!
    +
    +doneWithRows: # bye bye!; yellow highlight
    +  ret
    +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/midterm-solutions.zip b/_site/melody/cmpt-295/midterm-solutions.zip new file mode 100644 index 0000000..fcada7e Binary files /dev/null and b/_site/melody/cmpt-295/midterm-solutions.zip differ diff --git a/_site/melody/cmpt-295/midterm-solutions/Cmpt295_Midterm_1_Fall_2021_SOLUTION.pdf b/_site/melody/cmpt-295/midterm-solutions/Cmpt295_Midterm_1_Fall_2021_SOLUTION.pdf new file mode 100644 index 0000000..505f8c6 Binary files /dev/null and b/_site/melody/cmpt-295/midterm-solutions/Cmpt295_Midterm_1_Fall_2021_SOLUTION.pdf differ diff --git a/_site/melody/cmpt-295/midterm-solutions/midterm-solutions.html b/_site/melody/cmpt-295/midterm-solutions/midterm-solutions.html new file mode 100644 index 0000000..4d37723 --- /dev/null +++ b/_site/melody/cmpt-295/midterm-solutions/midterm-solutions.html @@ -0,0 +1,901 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    SOLUTION

    + +
      +
    • Simon Fraser University
    • +
    • Computing Science 295
    • +
    • Fall 2021
    • +
    • Friday Oct. 15 2021
    • +
    • Midterm Examination 1
    • +
    • Time: 45 minutes
    • +
    + +

    First Name: _________

    + +

    Last Name: _________

    + +

    Student ID: _________

    + +

    This examination has 11 pages.

    + +

    Read each question carefully before answering it.

    + +
      +
    • No textbooks, cheat sheets, calculators, computers, cell phones or other materials may +be used.
    • +
    • All assembly code must be x86-64 assembly code.
    • +
    • Hand in your scrap sheet(s) along with your examination paper. The scrap sheet(s) will +not be marked.
    • +
    • The marks for each question are given in [ ]. Use this to manage your time: +
        +
      • One (1) mark corresponds to one (1) minute of work.
      • +
      • Do not spend more time on a question than the number of marks assigned to it.
      • +
      +
    • +
    + +

    Good luck!

    + +

    Part 1

    + +

    Each question is 2 marks – There are no part marks given!

    + +

    Answer the following multiple choice questions on the bubble sheet at the back of this +examination paper.

    + +
      +
    1. Consider the following syntactically correct C code fragment: +
      float aFloat = 3.1415;
      +int sum = (int) aFloat + 0xFFFFFFFE;
      +
      +

      Which value does the variable sum contain when the above C code fragment has +executed on our target machine?

      +
        +
      • a. 1.1415
      • +
      • b. -1
      • +
      • c. 0x00000001 (CORRECT)
      • +
      +
        +
      • aFloat = 3.1415
      • +
      • 0xFFFFFFFE = -2
      • +
      • (int) aFloat = 3
      • +
      • sum = 3 + (-2) = 1 or 0x00000001 (int in hex) + * d. 5 + * e. None of the above
      • +
      +
    2. +
    3. Which step in the compilation process transforms our C code into assembly +instructions? +
        +
      • a. The step called the preprocessor
      • +
      • b. The step called the compiler – See Lecture 8 Slide 7 (CORRECT)
      • +
      • c. The step called the assembler
      • +
      • d. The step called the linker
      • +
      • e. None of the above
      • +
      +
    4. +
    5. Consider the following syntactically correct C function: +
      char mystery( char someParam ) {
      +  char result = 0;
      +  if ( someParam > 0 ) result = someParam;
      +  else result = -someParam;
      +  return result;
      +}
      +
      +

      What will it return once it has executed on our target machine with the parameter +someParam set to the value -128?

      +
        +
      • a. 127
      • +
      • b. 128
      • +
      • c. -127
      • +
      • d. -128 (CORRECT) Explaination
      • +
      • e. None of the above
      • +
      +
    6. +
    7. Consider the following syntactically correct C code fragment: +
      short count = 0xACE;
      +printf( "count = %hhi\n", (char) count );
      +
      +

      What is printed on the computer monitor screen when the above C code fragment +has executed on our target machine?

      +
        +
      • a. count = -50 (CORRECT) Explaination
      • +
      • b. count = 0xCE
      • +
      • c. count = 206
      • +
      • d. count = 0xACE
      • +
      • e. None of the above
      • +
      +
    8. +
    9. Consider the following syntactically correct C code fragment: +
      short aShort = -2;
      +char aChar = aShort;
      +short sumS = 0xABBB + (short) aChar + 1;
      +
      +

      Which statement below is true about the above C code fragment once it has executed +on our target machine, but has not yet exited the scope of the variables aShort, +aChar and sumS?

      +
        +
      • a. sumS contains the hex value 0xABBA
      • +
      • b. aChar == aShort
      • +
      • c. Statements a. and b. are true. (CORRECT) Explaination
      • +
      • d. Only the statement b. is true.
      • +
      • e. None of the above
      • +
      +
    10. +
    11. Consider the following C code fragment: +
      char char1 = 101;
      +char char2 = _______ ;
      +char sumOfChar = char1 + char2;
      +
      +

      Which value must be assigned to char2 in order for the sum of char1 and char2 +to create a positive overflow?

      +
        +
      • a. No numbers would create a positive overflow when added to 101.
      • +
      • b. 42 (CORRECT) Explaination
      • +
      • c. 26
      • +
      • d. -230
      • +
      • e. None of the above
      • +
      +
    12. +
    13. Consider the following syntactically correct C code fragment: +
      unsigned int x = 0xDECAF000;
      +unsigned short y = 0xCAFE;
      +if ( x > y ) printf("Caf ");
      +if ( x < (signed short) y ) printf("Decaf ");
      +if ( (unsigned char) x > y ) printf("Latte ");
      +
      +

      What is printed on the computer monitor screen when the above C code fragment +has executed on our target machine?

      +
        +
      • a. Caf Decaf Latte
      • +
      • b. Caf Latte
      • +
      • c. Caf
      • +
      • d. Decaf
      • +
      • e. None of the above (CORRECT) Explaination
      • +
      +
    14. +
    15. Which range of values can be stored in the variable y declared in the C fragment +code of Question 7 above? +
        +
      • a. [0 .. 2162^{16}]
      • +
      • b. [-128 .. 127]
      • +
      • c. [0 .. 21612^{16}-1]
      • +
      • d. [0 .. 21512^{15} – 1]
      • +
      • e. None of the above (CORRECT) Explaination
      • +
      +
    16. +
    + +

    Part 2

    + +

    The weight of each question is indicated in [ ] – Write your answer below each question unless instructed otherwise.

    + +

    1

    + +

    [Total marks: 15] Consider the following function mystery written in x86-64 +assembly code, where the parameter x is in the register %edi and the parameter y is +in the register %esi:

    +
    .globl power # mystery is crossed out and replaced with power; line 1
    +power: # x -> %edi, y -> %esi; mystery replaced with power; line 2
    +  xorl %eax,%eax # line 3
    +  movl $1,%r8d # line 4
    +loop: # loop replaces .L3 # line 5
    +  addl $1,%eax # line 6
    +  imull %edi,%r8d # line 7
    +  cmpl %eax,%esi # line 8
    +  jne loop # loop replaces .L3; line 9
    +  movl %r8d,%eax # line 10
    +  ret # line 11
    +
    + +
      +
    • a. [3 marks] If we call this function as follows: mystery( x, y ) where x = 2 +and y = 3, what value will it return? +
        +
      • Answer: 8
      • +
      +
    • +
    • b. [2 marks] Replace the label .L3 with a more descriptive label name. Do this +replacement in the above code. +
        +
      • Possible answer: loop
      • +
      +
    • +
    • c. [2 marks] Rename this function with a more descriptive name. Do this renaming in +the above code. +
        +
      • Possible answer: power or pow; xyx^y -> x is raised to the power y
      • +
      +
    • +
    • d. [2 marks] What is the data type of the parameters and the return value of this +function? Express your answer using C data types. +
        +
      • Answer: int or unsigned int
      • +
      +
    • +
    • e. [2 marks] Replace Line 3 with another equivalent x86-64 instruction, i.e., an +x86-64 instruction that will produce the same result, but is not a xor* +instruction. +
        +
      • Answer: Line 3: xorl %eax,%eax
      • +
      • The purpose of this instruction is to “zero” the register %eax.
      • +
      • Possible replacement: Line 3: movl $0,%eax
      • +
      +
    • +
    • f. [2 marks] Replace Line 6 with another equivalent x86-64 instruction, i.e., an +x86-64 instruction that will produce the same result, but is not an add* +instruction. +
        +
      • Answer: Line 6: addl $1,%eax
      • +
      • The purpose of this instruction is to increment the value of the +register %eax by 1.
      • +
      • Possible replacements: +
          +
        • Line 6: incl %eax
        • +
        • Line 6: leal 1(%eax),%eax
        • +
        +
      • +
      +
    • +
    • g. [2 marks] On Line 6 and Line 8, the register %eax is used for a different +purpose than holding the return value. For what purpose is the register %eax used +on those two lines?
    • +
    • Answer: On Line 6 and Line 8, the register %eax is used as a loop +increment or a loop counter, expressing the number of times the loop +executes. At every loop iteration, %eax is incremented by 1 (Line 6) +then compare to y (Line 8). The loop terminates when %eax equals +the value of y (the power to which we are raising x).
    • +
    + +

    2

    + +

    [Total marks: 14] Consider a floating point number encoding scheme based on the +IEEE floating point format and defined as follows:

    +
      +
    • It uses 7 bits.
    • +
    • There is one sign bit s.
    • +
    • The exp can be any number in the range [0 .. 31].
    • +
    + +

    From the above, we gather:

    + +
      +
    • the format is: s exp frac
    • +
    • exp is 5 bits (k = 5) and
    • +
    • frac is 1 bit1 (1 bit for s + 5 bits for exp + 1 for frac = 7 bits)
    • +
    • since this floating point number encoding scheme is based on the +IEEE floating point format, the following equations hold: +
        +
      • V = (1)sM2E(-1)^s M 2^E
      • +
      • E= exp – bias (for normalized numbers)
      • +
      • M = 1 + frac (for normalized numbers)
      • +
      • E= 1 – bias (for denormalized numbers)
      • +
      • M = frac (for denormalized numbers)
      • +
      • bias = 2k112^{k-1} – 1 (for normalized and denormalized numbers)
      • +
      +
    • +
    • a. [2 marks] Compute the bias of this IEEE-like floating point number encoding +scheme described above and show your work: +
        +
      • Answer: k = 5
      • +
      • bias = 2k11=2511=241=161=152^{k-1}–1 = 2^{5-1} – 1 = 2^4 – 1 = 16 – 1 = 15
      • +
      +
    • +
    • b. [7 marks] Encode the value 24.510 using this IEEE-like floating point number +representation described in this question. Show all your work. Clearly show the +resulting bit pattern and label its three sections s,exp,frac. +
        +
      • Step 1) 24.510 is a positive number so s = 0
      • +
      • Step 2) R2B(24.510) +
          +
        • => 24 – 16 (242^4) = 8 and 0.5 - 0.5 (212^{-1}) = 0
        • +
        • 8-8 (232^3) = 0
        • +
        • 24.510\text{24.5}_{10} => 11000.12\text{11000.1}_{2}
        • +
        +
      • +
      • Step 3) normalize 11000.12\text{11000.1}_{2} => 1.100012×24\text{1.10001}_{2} \times 2^4
      • +
      • Step 4) Using V=(-1)sM2EV = \text{(-1)}^s M 2^E +
          +
        • 1) E= exp – bias => exp = E + bias +
            +
          • => exp = 410 + 1510 = 1910 since E = 410
          • +
          • => U2B(1910\text{19}_{10}) = 100112\text{10011}_2 (k = 5)
          • +
          +
        • +
        • 2) M = 1 + frac => frac = M - 1 +
            +
          • => frac = 1.100012\text{1.10001}_2 – 1 => 0.100012\text{0.10001}_2 since M=1.100012M = \text{1.10001}_2
          • +
          • => frac = 100012\text{10001}_2 (ignoring “0.”) +but since frac only has 1 bit, 100012 +cannot be stored in frac, so we need to +round frac .100012\text{.10001}_2 => .12\text{.1}_2
          • +
          • => MSBit is the rounding position (in blue)
          • +
          • => since the value of the rest of the bits +(00012\text{0001}_2 = 0.03125 (252^{-5}) - see table below) < 1/21/2 +the worth of rounding position (1/21/2 of 0.5 = +0.25), then we round down which means we +only discard the bits 00012\text{0001}_2 from .100012\text{.10001}_2)
          • +
          +
        • +
        • Step 5) +
            +
          • Using the format: s exp frac +the resulting bit pattern encoding 24.510\text{24.5}_{10} +in the IEEE-like floating point number +representation described in this question is: s=0 exp=10011 frac=1
          • +
          +
        • +
        +
      • +
      +
    • +
    • c. [2 marks] Write the “range” (non-contiguous) of real numbers (excluding +/infinity and NAN) that can be encoded using this IEEE-like floating-point +representation described in this question. Express this range using the bit patterns +(not the actual real numbers). +
        +
      • Answer: “range” (non-contiguous) of real numbers (excluding +/- infinity and NAN) that can be encoded +using +IEEE-like floating-point +representation +described +in +this +question +(expressed using the bit patterns): [ 1 11110 1 .. 0 11110 1 ]
      • +
      +
    • +
    • d. [3 marks] Can 6553610 be encoded as a normalized number in this IEEE-like +floating point representation? Briefly explain why/why not. +Hint: Use your range in the above question and the table below. +
        +
      • Answer: +6553610\text{65536}_{10} cannot be encoded as a normalized number +in this IEEE-like floating point representation +because expressed as V=(-1)sM2EV = \text{(-1)}^s M 2^E +6553610 is V=(-1)01.0216=216V = \text{(-1)}^0 1.0 2^{16} = 2^{16} (see table below) +
          +
        • E= exp – bias => exp = E + bias
        • +
        • exp = 1610\text{16}_{10} + 1510\text{15}_{10} = 3110\text{31}_{10} since E = 1610\text{16}_{10} +and U2B(3110\text{31}_{10}) = 111112\text{11111}_2 +(k = 5) which is outside +the range for exp as indicated in the range given +as the answer to the above question: +[ 1 11110 1 .. 0 11110 1 ]
        • +
        • When exp=111112\text{exp} = \text{11111}_{2} for k = 5, it indicates overflow, i.e., one of the special cases.
        • +
        +
      • +
      +
    • +
    + +

    Table Powers of 2:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Power of 2x2^xValue
    -101/1024 = 0.0009765625
    -91/512 = 0.001953125
    -81/256 = 0.00390625
    -71/128 = 0.0078125
    -61/64 = 0.015625
    -51/32 = 0.03125
    -41/16 = 0.0625
    -31/8 = 0.125
    -21/4 = 0.25
    -11/2 = 0.5
    01
    12
    24
    38
    416
    532
    664
    7128
    8256
    9512
    101024
    112048
    124096
    138192
    1416384
    1532768
    1665536
    + +

    Table of x86-64 Jumps:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    InstructionConditionDescription
    jmpalwaysUnconditional jump
    je/jzZFJump if equal / zero
    jne/jnz~ZFJump if not equal / not zero
    jsSFJump if negative
    jns~SFJump if nonnegative
    joOFJump if overflow
    jno~OFJump if not overflow
    jq/jnle~(SF ^ OF) & ~ZFJump if greater (signed >)
    jge/jnl~(SF ^ OF)Jump if greater or equal (signed >=)
    jl/jngeSF ^ OFJump if less (signed <)
    jle/jng(SF ^ OF) | ZFJump if less or equal (signed <=)
    ja/jnbe~CF & ~ZFJumP if greater (unsigned >)
    jae/jnb~CFJump if greater or equal (unsigned >=)
    jb/jnae/jcCFJump if less (unsigned <)
    jbe/jna/jncCF | ZFJump if less or equal (unsigned <=)
    + +

    Table of x86-64 Registers

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    64-bit (quad)32-bit (double)16-bit (word)8-bit (byte)8-bit (byte) 
    63..031..015..015..87..0 
    raxeaxaxahalReturn value
    rbxebxbxbhblCallee served
    rcxecxcxchcl4th arg
    rdxedxdxdhdl3rd arg
    rsiesisi sil2nd arg
    rdiedidi dil1st arg
    rbpebpbp bplCallee served
    rspespsp splStack position
    r8r8dr8w r8b5th arg
    r9r9dr9w r9b6th arg
    r10r10dr10w r10bCallee served
    r11r11dr11w r11bCallee served
    r12r12dr12w r12bCallee served
    r13r13dr13w r13bCallee served
    r14r14dr14w r14bCallee served
    r15r15dr15w r15bCallee served
    + +

    Explainations:

    + +

    3

    + +

    Back to answer

    + +
    char mystery( -128 ) {
    +  char result = 0;
    +  if ( -128 > 0 ) result = someParam;
    +  else result = -(-128);
    +
    + +

    So, it seems that result = 128 +What is the bit pattern of 128? +Interpreting 128 as an unsigned char we get: +B2U(10000000) -> 272^7 -> 128 +but we cannot interpret 128 as a signed char +because 128 is outside the range of signed char +-> [-128 .. 127], so the bit pattern 10000000 +interpreted as a signed char is -128 +Therefore even though it seems that +result = 128 (10000000) +It is actually the case that +result = -128 +return result i.e., -128

    + +

    EXTRA

    + +

    What is -128 as a bit pattern?

    + +

    -128 -> T2B(X) -> (~(U2B(|X|)))+1 and X = -128

    +
      +
    • See Lecture 3 Slide 11 Method 1
    • +
    • (~(U2B(|-128|)))+1
    • +
    • (~(U2B(128)))+1
    • +
    • (~(10000000))+1
    • +
    • -> someParam is a char -> w = 8 bits
    • +
    • (01111111) + 1 = 10000000
    • +
    • Check: B2T(10000000) -> -27 -> -128
    • +
    • -128 -> T2B(X) -> U2B(X + 2w2^w)
    • +
    • See Lecture 3 Slide 11 Method 2
    • +
    • U2B(-128 + 2w) -> U2B(-128 + 28)
    • +
    • -> U2B(-128 + 256)
    • +
    • -> U2B(128) -> 10000000
    • +
    + +

    4

    + +

    Back to answer

    + +
      +
    • count = 0xACE = 0000 1010 1100 1110
    • +
    • (char) count = 0xCE = 1100 1110
    • +
    • hhi -> signed char numerical output +
        +
      • -> 1100 1110 = 27-2^7 + 262^6 + 232^3 + 222^2 + 212^1 = -128 + 64 + 8 + 4 + 2 = -50
      • +
      +
    • +
    • See Lecture_4_Demo.c
    • +
    + +

    5

    + +

    Back to answer

    + +
      +
    • aShort = -2 = 0xFFFE
    • +
    • aChar = aShort -> aChar = -2 = 0xFE -> 1111 1110
    • +
    • 1111 1110 = 27-2^7 + 262^6 + 252^5 + 242^4 + 232^3 + 222^2 + 212^1 = -128 + 64 + 32 + 16 + 8 + 4 + 2 = -2
    • +
    • So, Statement a. => aChar (-2) == aShort (-2) is TRUE
    • +
    • (short) aChar = 0xFFFE -> 1111 1111 1111 1110 = -2
    • +
    • (short) aChar + 1 = -2 + 1 = -1 -> 0xFFFF
    • +
    • 0xFFFF = 1111 1111 1111 1111
    • +
    • 0xABBB (1010 1011 1011 1011) + 0xFFFF -> 0xABBA
    • +
    • short sumS = 0xABBB + (short) aChar + 1;
    • +
    • So, Statement b. => sumS contains the hex value 0xABBA is TRUE
    • +
    • So, Statements a. and b. are true.
    • +
    + +

    6

    + +

    Back to answer

    + +
      +
    • char1 = 101;
    • +
    • range of char -> [-128 .. 127]
    • +
    • For char1 + char2 > 127 i.e., create a positive overflow, char2 > 127 – char1 (101) -> char2 > 26
    • +
    • So char2 = 42 satisfies the above condition
    • +
    • char1 (101) + char2 (42) = 143 > 127
    • +
    • char1 (101) + char2 (26) = 127 -> still within the range (on positive side of range)
    • +
    • char1 (101) + char2 (-203) = -102 -> still within the range (on negative side of range)
    • +
    + +

    7

    + +

    Back to answer

    + +
    unsigned int x = 0xDECAF000;
    +unsigned short y = 0xCAFE;
    +if ( x > y ) printf("Caf ");
    +
    + +

    Promoting y to 32 bits as an unsigned i.e. padding with 0’s: 0xDECAF000 > 0x0000CAFE without a calculator, we can see that these 32 +bits 0xDECAF000, interpreted as an unsigned +value, will be > than 0x0000CAFE, also +interpreted as an unsigned value. +So, Caf is printed on the computer monitor screen +of target machine.

    + +
    if ( x < (signed short) y ) printf("Decaf ");
    +
    + +

    Casting y to 16 bits as a signed i.e. interpreting 0xCAFE as a signed value and promoting it to 32 bits still as a signed i.e. +padding with 1’s: 0xFFFFCAFE +0xDECAF000 < 0xFFFFCAFE +without a calculator, we can see that these 32 +bits 0xDECAF000, interpreted as a signed value, +will represent a larger negative value than +0xFFFFCAFE, also interpreted as a signed value +Remember from previous questions that 0xFE = +0xFFFE = 0xFFFFFFFE = -2 +So, Decaf is printed on the computer monitor +screen of target machine.

    + +
    if ( (unsigned char) x > y ) printf("Latte ");
    +
    + +

    Casting x (0xDECAF000) to a char -> 8 bits, we +get 0x00 = 0 (unsigned value) +promoting it to 16 bits still gives us 0 +0x0000 > 0xCAFE +without a calculator, we can see that this is not +the case. +So, Latte is NOT printed on the computer monitor +screen of target machine.

    + +

    The answer (Caf Decaf) is not one of the options.

    + +

    8

    + +

    Return to answer

    + +
    unsigned short y = 0xCAFE;
    +
    + +

    y is declared as an unsigned short -> 16 bits +so range of unsigned short is [0 .. 216 -1] +which is not one of the options sbove.

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/midterm-solutions/midterm-solutions/index.html b/_site/melody/cmpt-295/midterm-solutions/midterm-solutions/index.html new file mode 100644 index 0000000..4d37723 --- /dev/null +++ b/_site/melody/cmpt-295/midterm-solutions/midterm-solutions/index.html @@ -0,0 +1,901 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    SOLUTION

    + +
      +
    • Simon Fraser University
    • +
    • Computing Science 295
    • +
    • Fall 2021
    • +
    • Friday Oct. 15 2021
    • +
    • Midterm Examination 1
    • +
    • Time: 45 minutes
    • +
    + +

    First Name: _________

    + +

    Last Name: _________

    + +

    Student ID: _________

    + +

    This examination has 11 pages.

    + +

    Read each question carefully before answering it.

    + +
      +
    • No textbooks, cheat sheets, calculators, computers, cell phones or other materials may +be used.
    • +
    • All assembly code must be x86-64 assembly code.
    • +
    • Hand in your scrap sheet(s) along with your examination paper. The scrap sheet(s) will +not be marked.
    • +
    • The marks for each question are given in [ ]. Use this to manage your time: +
        +
      • One (1) mark corresponds to one (1) minute of work.
      • +
      • Do not spend more time on a question than the number of marks assigned to it.
      • +
      +
    • +
    + +

    Good luck!

    + +

    Part 1

    + +

    Each question is 2 marks – There are no part marks given!

    + +

    Answer the following multiple choice questions on the bubble sheet at the back of this +examination paper.

    + +
      +
    1. Consider the following syntactically correct C code fragment: +
      float aFloat = 3.1415;
      +int sum = (int) aFloat + 0xFFFFFFFE;
      +
      +

      Which value does the variable sum contain when the above C code fragment has +executed on our target machine?

      +
        +
      • a. 1.1415
      • +
      • b. -1
      • +
      • c. 0x00000001 (CORRECT)
      • +
      +
        +
      • aFloat = 3.1415
      • +
      • 0xFFFFFFFE = -2
      • +
      • (int) aFloat = 3
      • +
      • sum = 3 + (-2) = 1 or 0x00000001 (int in hex) + * d. 5 + * e. None of the above
      • +
      +
    2. +
    3. Which step in the compilation process transforms our C code into assembly +instructions? +
        +
      • a. The step called the preprocessor
      • +
      • b. The step called the compiler – See Lecture 8 Slide 7 (CORRECT)
      • +
      • c. The step called the assembler
      • +
      • d. The step called the linker
      • +
      • e. None of the above
      • +
      +
    4. +
    5. Consider the following syntactically correct C function: +
      char mystery( char someParam ) {
      +  char result = 0;
      +  if ( someParam > 0 ) result = someParam;
      +  else result = -someParam;
      +  return result;
      +}
      +
      +

      What will it return once it has executed on our target machine with the parameter +someParam set to the value -128?

      +
        +
      • a. 127
      • +
      • b. 128
      • +
      • c. -127
      • +
      • d. -128 (CORRECT) Explaination
      • +
      • e. None of the above
      • +
      +
    6. +
    7. Consider the following syntactically correct C code fragment: +
      short count = 0xACE;
      +printf( "count = %hhi\n", (char) count );
      +
      +

      What is printed on the computer monitor screen when the above C code fragment +has executed on our target machine?

      +
        +
      • a. count = -50 (CORRECT) Explaination
      • +
      • b. count = 0xCE
      • +
      • c. count = 206
      • +
      • d. count = 0xACE
      • +
      • e. None of the above
      • +
      +
    8. +
    9. Consider the following syntactically correct C code fragment: +
      short aShort = -2;
      +char aChar = aShort;
      +short sumS = 0xABBB + (short) aChar + 1;
      +
      +

      Which statement below is true about the above C code fragment once it has executed +on our target machine, but has not yet exited the scope of the variables aShort, +aChar and sumS?

      +
        +
      • a. sumS contains the hex value 0xABBA
      • +
      • b. aChar == aShort
      • +
      • c. Statements a. and b. are true. (CORRECT) Explaination
      • +
      • d. Only the statement b. is true.
      • +
      • e. None of the above
      • +
      +
    10. +
    11. Consider the following C code fragment: +
      char char1 = 101;
      +char char2 = _______ ;
      +char sumOfChar = char1 + char2;
      +
      +

      Which value must be assigned to char2 in order for the sum of char1 and char2 +to create a positive overflow?

      +
        +
      • a. No numbers would create a positive overflow when added to 101.
      • +
      • b. 42 (CORRECT) Explaination
      • +
      • c. 26
      • +
      • d. -230
      • +
      • e. None of the above
      • +
      +
    12. +
    13. Consider the following syntactically correct C code fragment: +
      unsigned int x = 0xDECAF000;
      +unsigned short y = 0xCAFE;
      +if ( x > y ) printf("Caf ");
      +if ( x < (signed short) y ) printf("Decaf ");
      +if ( (unsigned char) x > y ) printf("Latte ");
      +
      +

      What is printed on the computer monitor screen when the above C code fragment +has executed on our target machine?

      +
        +
      • a. Caf Decaf Latte
      • +
      • b. Caf Latte
      • +
      • c. Caf
      • +
      • d. Decaf
      • +
      • e. None of the above (CORRECT) Explaination
      • +
      +
    14. +
    15. Which range of values can be stored in the variable y declared in the C fragment +code of Question 7 above? +
        +
      • a. [0 .. 2162^{16}]
      • +
      • b. [-128 .. 127]
      • +
      • c. [0 .. 21612^{16}-1]
      • +
      • d. [0 .. 21512^{15} – 1]
      • +
      • e. None of the above (CORRECT) Explaination
      • +
      +
    16. +
    + +

    Part 2

    + +

    The weight of each question is indicated in [ ] – Write your answer below each question unless instructed otherwise.

    + +

    1

    + +

    [Total marks: 15] Consider the following function mystery written in x86-64 +assembly code, where the parameter x is in the register %edi and the parameter y is +in the register %esi:

    +
    .globl power # mystery is crossed out and replaced with power; line 1
    +power: # x -> %edi, y -> %esi; mystery replaced with power; line 2
    +  xorl %eax,%eax # line 3
    +  movl $1,%r8d # line 4
    +loop: # loop replaces .L3 # line 5
    +  addl $1,%eax # line 6
    +  imull %edi,%r8d # line 7
    +  cmpl %eax,%esi # line 8
    +  jne loop # loop replaces .L3; line 9
    +  movl %r8d,%eax # line 10
    +  ret # line 11
    +
    + +
      +
    • a. [3 marks] If we call this function as follows: mystery( x, y ) where x = 2 +and y = 3, what value will it return? +
        +
      • Answer: 8
      • +
      +
    • +
    • b. [2 marks] Replace the label .L3 with a more descriptive label name. Do this +replacement in the above code. +
        +
      • Possible answer: loop
      • +
      +
    • +
    • c. [2 marks] Rename this function with a more descriptive name. Do this renaming in +the above code. +
        +
      • Possible answer: power or pow; xyx^y -> x is raised to the power y
      • +
      +
    • +
    • d. [2 marks] What is the data type of the parameters and the return value of this +function? Express your answer using C data types. +
        +
      • Answer: int or unsigned int
      • +
      +
    • +
    • e. [2 marks] Replace Line 3 with another equivalent x86-64 instruction, i.e., an +x86-64 instruction that will produce the same result, but is not a xor* +instruction. +
        +
      • Answer: Line 3: xorl %eax,%eax
      • +
      • The purpose of this instruction is to “zero” the register %eax.
      • +
      • Possible replacement: Line 3: movl $0,%eax
      • +
      +
    • +
    • f. [2 marks] Replace Line 6 with another equivalent x86-64 instruction, i.e., an +x86-64 instruction that will produce the same result, but is not an add* +instruction. +
        +
      • Answer: Line 6: addl $1,%eax
      • +
      • The purpose of this instruction is to increment the value of the +register %eax by 1.
      • +
      • Possible replacements: +
          +
        • Line 6: incl %eax
        • +
        • Line 6: leal 1(%eax),%eax
        • +
        +
      • +
      +
    • +
    • g. [2 marks] On Line 6 and Line 8, the register %eax is used for a different +purpose than holding the return value. For what purpose is the register %eax used +on those two lines?
    • +
    • Answer: On Line 6 and Line 8, the register %eax is used as a loop +increment or a loop counter, expressing the number of times the loop +executes. At every loop iteration, %eax is incremented by 1 (Line 6) +then compare to y (Line 8). The loop terminates when %eax equals +the value of y (the power to which we are raising x).
    • +
    + +

    2

    + +

    [Total marks: 14] Consider a floating point number encoding scheme based on the +IEEE floating point format and defined as follows:

    +
      +
    • It uses 7 bits.
    • +
    • There is one sign bit s.
    • +
    • The exp can be any number in the range [0 .. 31].
    • +
    + +

    From the above, we gather:

    + +
      +
    • the format is: s exp frac
    • +
    • exp is 5 bits (k = 5) and
    • +
    • frac is 1 bit1 (1 bit for s + 5 bits for exp + 1 for frac = 7 bits)
    • +
    • since this floating point number encoding scheme is based on the +IEEE floating point format, the following equations hold: +
        +
      • V = (1)sM2E(-1)^s M 2^E
      • +
      • E= exp – bias (for normalized numbers)
      • +
      • M = 1 + frac (for normalized numbers)
      • +
      • E= 1 – bias (for denormalized numbers)
      • +
      • M = frac (for denormalized numbers)
      • +
      • bias = 2k112^{k-1} – 1 (for normalized and denormalized numbers)
      • +
      +
    • +
    • a. [2 marks] Compute the bias of this IEEE-like floating point number encoding +scheme described above and show your work: +
        +
      • Answer: k = 5
      • +
      • bias = 2k11=2511=241=161=152^{k-1}–1 = 2^{5-1} – 1 = 2^4 – 1 = 16 – 1 = 15
      • +
      +
    • +
    • b. [7 marks] Encode the value 24.510 using this IEEE-like floating point number +representation described in this question. Show all your work. Clearly show the +resulting bit pattern and label its three sections s,exp,frac. +
        +
      • Step 1) 24.510 is a positive number so s = 0
      • +
      • Step 2) R2B(24.510) +
          +
        • => 24 – 16 (242^4) = 8 and 0.5 - 0.5 (212^{-1}) = 0
        • +
        • 8-8 (232^3) = 0
        • +
        • 24.510\text{24.5}_{10} => 11000.12\text{11000.1}_{2}
        • +
        +
      • +
      • Step 3) normalize 11000.12\text{11000.1}_{2} => 1.100012×24\text{1.10001}_{2} \times 2^4
      • +
      • Step 4) Using V=(-1)sM2EV = \text{(-1)}^s M 2^E +
          +
        • 1) E= exp – bias => exp = E + bias +
            +
          • => exp = 410 + 1510 = 1910 since E = 410
          • +
          • => U2B(1910\text{19}_{10}) = 100112\text{10011}_2 (k = 5)
          • +
          +
        • +
        • 2) M = 1 + frac => frac = M - 1 +
            +
          • => frac = 1.100012\text{1.10001}_2 – 1 => 0.100012\text{0.10001}_2 since M=1.100012M = \text{1.10001}_2
          • +
          • => frac = 100012\text{10001}_2 (ignoring “0.”) +but since frac only has 1 bit, 100012 +cannot be stored in frac, so we need to +round frac .100012\text{.10001}_2 => .12\text{.1}_2
          • +
          • => MSBit is the rounding position (in blue)
          • +
          • => since the value of the rest of the bits +(00012\text{0001}_2 = 0.03125 (252^{-5}) - see table below) < 1/21/2 +the worth of rounding position (1/21/2 of 0.5 = +0.25), then we round down which means we +only discard the bits 00012\text{0001}_2 from .100012\text{.10001}_2)
          • +
          +
        • +
        • Step 5) +
            +
          • Using the format: s exp frac +the resulting bit pattern encoding 24.510\text{24.5}_{10} +in the IEEE-like floating point number +representation described in this question is: s=0 exp=10011 frac=1
          • +
          +
        • +
        +
      • +
      +
    • +
    • c. [2 marks] Write the “range” (non-contiguous) of real numbers (excluding +/infinity and NAN) that can be encoded using this IEEE-like floating-point +representation described in this question. Express this range using the bit patterns +(not the actual real numbers). +
        +
      • Answer: “range” (non-contiguous) of real numbers (excluding +/- infinity and NAN) that can be encoded +using +IEEE-like floating-point +representation +described +in +this +question +(expressed using the bit patterns): [ 1 11110 1 .. 0 11110 1 ]
      • +
      +
    • +
    • d. [3 marks] Can 6553610 be encoded as a normalized number in this IEEE-like +floating point representation? Briefly explain why/why not. +Hint: Use your range in the above question and the table below. +
        +
      • Answer: +6553610\text{65536}_{10} cannot be encoded as a normalized number +in this IEEE-like floating point representation +because expressed as V=(-1)sM2EV = \text{(-1)}^s M 2^E +6553610 is V=(-1)01.0216=216V = \text{(-1)}^0 1.0 2^{16} = 2^{16} (see table below) +
          +
        • E= exp – bias => exp = E + bias
        • +
        • exp = 1610\text{16}_{10} + 1510\text{15}_{10} = 3110\text{31}_{10} since E = 1610\text{16}_{10} +and U2B(3110\text{31}_{10}) = 111112\text{11111}_2 +(k = 5) which is outside +the range for exp as indicated in the range given +as the answer to the above question: +[ 1 11110 1 .. 0 11110 1 ]
        • +
        • When exp=111112\text{exp} = \text{11111}_{2} for k = 5, it indicates overflow, i.e., one of the special cases.
        • +
        +
      • +
      +
    • +
    + +

    Table Powers of 2:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Power of 2x2^xValue
    -101/1024 = 0.0009765625
    -91/512 = 0.001953125
    -81/256 = 0.00390625
    -71/128 = 0.0078125
    -61/64 = 0.015625
    -51/32 = 0.03125
    -41/16 = 0.0625
    -31/8 = 0.125
    -21/4 = 0.25
    -11/2 = 0.5
    01
    12
    24
    38
    416
    532
    664
    7128
    8256
    9512
    101024
    112048
    124096
    138192
    1416384
    1532768
    1665536
    + +

    Table of x86-64 Jumps:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    InstructionConditionDescription
    jmpalwaysUnconditional jump
    je/jzZFJump if equal / zero
    jne/jnz~ZFJump if not equal / not zero
    jsSFJump if negative
    jns~SFJump if nonnegative
    joOFJump if overflow
    jno~OFJump if not overflow
    jq/jnle~(SF ^ OF) & ~ZFJump if greater (signed >)
    jge/jnl~(SF ^ OF)Jump if greater or equal (signed >=)
    jl/jngeSF ^ OFJump if less (signed <)
    jle/jng(SF ^ OF) | ZFJump if less or equal (signed <=)
    ja/jnbe~CF & ~ZFJumP if greater (unsigned >)
    jae/jnb~CFJump if greater or equal (unsigned >=)
    jb/jnae/jcCFJump if less (unsigned <)
    jbe/jna/jncCF | ZFJump if less or equal (unsigned <=)
    + +

    Table of x86-64 Registers

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    64-bit (quad)32-bit (double)16-bit (word)8-bit (byte)8-bit (byte) 
    63..031..015..015..87..0 
    raxeaxaxahalReturn value
    rbxebxbxbhblCallee served
    rcxecxcxchcl4th arg
    rdxedxdxdhdl3rd arg
    rsiesisi sil2nd arg
    rdiedidi dil1st arg
    rbpebpbp bplCallee served
    rspespsp splStack position
    r8r8dr8w r8b5th arg
    r9r9dr9w r9b6th arg
    r10r10dr10w r10bCallee served
    r11r11dr11w r11bCallee served
    r12r12dr12w r12bCallee served
    r13r13dr13w r13bCallee served
    r14r14dr14w r14bCallee served
    r15r15dr15w r15bCallee served
    + +

    Explainations:

    + +

    3

    + +

    Back to answer

    + +
    char mystery( -128 ) {
    +  char result = 0;
    +  if ( -128 > 0 ) result = someParam;
    +  else result = -(-128);
    +
    + +

    So, it seems that result = 128 +What is the bit pattern of 128? +Interpreting 128 as an unsigned char we get: +B2U(10000000) -> 272^7 -> 128 +but we cannot interpret 128 as a signed char +because 128 is outside the range of signed char +-> [-128 .. 127], so the bit pattern 10000000 +interpreted as a signed char is -128 +Therefore even though it seems that +result = 128 (10000000) +It is actually the case that +result = -128 +return result i.e., -128

    + +

    EXTRA

    + +

    What is -128 as a bit pattern?

    + +

    -128 -> T2B(X) -> (~(U2B(|X|)))+1 and X = -128

    +
      +
    • See Lecture 3 Slide 11 Method 1
    • +
    • (~(U2B(|-128|)))+1
    • +
    • (~(U2B(128)))+1
    • +
    • (~(10000000))+1
    • +
    • -> someParam is a char -> w = 8 bits
    • +
    • (01111111) + 1 = 10000000
    • +
    • Check: B2T(10000000) -> -27 -> -128
    • +
    • -128 -> T2B(X) -> U2B(X + 2w2^w)
    • +
    • See Lecture 3 Slide 11 Method 2
    • +
    • U2B(-128 + 2w) -> U2B(-128 + 28)
    • +
    • -> U2B(-128 + 256)
    • +
    • -> U2B(128) -> 10000000
    • +
    + +

    4

    + +

    Back to answer

    + +
      +
    • count = 0xACE = 0000 1010 1100 1110
    • +
    • (char) count = 0xCE = 1100 1110
    • +
    • hhi -> signed char numerical output +
        +
      • -> 1100 1110 = 27-2^7 + 262^6 + 232^3 + 222^2 + 212^1 = -128 + 64 + 8 + 4 + 2 = -50
      • +
      +
    • +
    • See Lecture_4_Demo.c
    • +
    + +

    5

    + +

    Back to answer

    + +
      +
    • aShort = -2 = 0xFFFE
    • +
    • aChar = aShort -> aChar = -2 = 0xFE -> 1111 1110
    • +
    • 1111 1110 = 27-2^7 + 262^6 + 252^5 + 242^4 + 232^3 + 222^2 + 212^1 = -128 + 64 + 32 + 16 + 8 + 4 + 2 = -2
    • +
    • So, Statement a. => aChar (-2) == aShort (-2) is TRUE
    • +
    • (short) aChar = 0xFFFE -> 1111 1111 1111 1110 = -2
    • +
    • (short) aChar + 1 = -2 + 1 = -1 -> 0xFFFF
    • +
    • 0xFFFF = 1111 1111 1111 1111
    • +
    • 0xABBB (1010 1011 1011 1011) + 0xFFFF -> 0xABBA
    • +
    • short sumS = 0xABBB + (short) aChar + 1;
    • +
    • So, Statement b. => sumS contains the hex value 0xABBA is TRUE
    • +
    • So, Statements a. and b. are true.
    • +
    + +

    6

    + +

    Back to answer

    + +
      +
    • char1 = 101;
    • +
    • range of char -> [-128 .. 127]
    • +
    • For char1 + char2 > 127 i.e., create a positive overflow, char2 > 127 – char1 (101) -> char2 > 26
    • +
    • So char2 = 42 satisfies the above condition
    • +
    • char1 (101) + char2 (42) = 143 > 127
    • +
    • char1 (101) + char2 (26) = 127 -> still within the range (on positive side of range)
    • +
    • char1 (101) + char2 (-203) = -102 -> still within the range (on negative side of range)
    • +
    + +

    7

    + +

    Back to answer

    + +
    unsigned int x = 0xDECAF000;
    +unsigned short y = 0xCAFE;
    +if ( x > y ) printf("Caf ");
    +
    + +

    Promoting y to 32 bits as an unsigned i.e. padding with 0’s: 0xDECAF000 > 0x0000CAFE without a calculator, we can see that these 32 +bits 0xDECAF000, interpreted as an unsigned +value, will be > than 0x0000CAFE, also +interpreted as an unsigned value. +So, Caf is printed on the computer monitor screen +of target machine.

    + +
    if ( x < (signed short) y ) printf("Decaf ");
    +
    + +

    Casting y to 16 bits as a signed i.e. interpreting 0xCAFE as a signed value and promoting it to 32 bits still as a signed i.e. +padding with 1’s: 0xFFFFCAFE +0xDECAF000 < 0xFFFFCAFE +without a calculator, we can see that these 32 +bits 0xDECAF000, interpreted as a signed value, +will represent a larger negative value than +0xFFFFCAFE, also interpreted as a signed value +Remember from previous questions that 0xFE = +0xFFFE = 0xFFFFFFFE = -2 +So, Decaf is printed on the computer monitor +screen of target machine.

    + +
    if ( (unsigned char) x > y ) printf("Latte ");
    +
    + +

    Casting x (0xDECAF000) to a char -> 8 bits, we +get 0x00 = 0 (unsigned value) +promoting it to 16 bits still gives us 0 +0x0000 > 0xCAFE +without a calculator, we can see that this is not +the case. +So, Latte is NOT printed on the computer monitor +screen of target machine.

    + +

    The answer (Caf Decaf) is not one of the options.

    + +

    8

    + +

    Return to answer

    + +
    unsigned short y = 0xCAFE;
    +
    + +

    y is declared as an unsigned short -> 16 bits +so range of unsigned short is [0 .. 216 -1] +which is not one of the options sbove.

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/midterm/about/About_our_Midterm_1.pdf b/_site/melody/cmpt-295/midterm/about/About_our_Midterm_1.pdf new file mode 100644 index 0000000..7383afd Binary files /dev/null and b/_site/melody/cmpt-295/midterm/about/About_our_Midterm_1.pdf differ diff --git a/_site/melody/cmpt-295/midterm/about/about.html b/_site/melody/cmpt-295/midterm/about/about.html new file mode 100644 index 0000000..11cff9d --- /dev/null +++ b/_site/melody/cmpt-295/midterm/about/about.html @@ -0,0 +1,447 @@ + + + + + | tait.tech + + + + +
    +
    +

    CMPT 295 – About our Midterm 1

    + +

    Midterm 1

    + +
      +
    • When: Friday, Oct. 15 9:30am – 10:20am
    • +
    • Where: same classroom
    • +
    • Duration: 45 minutes (+ 5 minutes to organize ourselves)
    • +
    + +

    What to study?

    + +

    Everything we covered up until (and including) +our lecture on Friday, Oct. 8 is subject to +examination

    + +
      +
    • All material from Lecture 1 until and including Lecture 14 (partial +lecture notes + annotated lecture notes + your own lecture +notes + recorded lectures)
    • +
    • Labs 1 to 3, Assignments 1, 2, 3 and 4 (even though Assignment 4 +is due after our Midterm 1)
    • +
    • All demo code posted on our course web site
    • +
    • Textbook Readings + Practice Problems: +
        +
      • Chapter 2, Section 2.1 to 2.4 (skip 2.3.7) + Practice Problems 2.1 to 2.34, 2. 38 to 2.41, 2.44, 2.45, 2.47, 2.48, 2.50, 2.52, 2. 54
      • +
      • Chapter 3, Section 3.2 to 3.6 (skip 3.4.4 for now, skip 3.5.5 and 3.6.8) + Practice Problems 3.1 to 3.11, 3.13 to 3.16 B. (not A.), 3.18, 3.20, +3.21 to 3.26, 3.28
      • +
      +
    • +
    + +

    Format of Midterm 1

    + +
      +
    • First section: questions that require short answers. For example: +
        +
      • “Yes/no” or
      • +
      • “fill in the blank” or
      • +
      • a few words
      • +
      • etc…
      • +
      +
    • +
    • Second section: questions that require written answers. For example: +
        +
      • Calculating – we always show every step of our work
      • +
      • Writing/completing C or assembly code
      • +
      • Hand tracing code
      • +
      • Writing an answer (in English)
      • +
      • etc…
      • +
      +
    • +
    + +

    Appendix

    + +

    As part of Midterm 1’s appendix, we will +be given the following information:

    + +

    Table of x86-64 Jumps:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    InstructionConditionDescription
    jmpalwaysUnconditional jump
    je/jzZFJump if equal / zero
    jne/jnz~ZFJump if not equal / not zero
    jsSFJump if negative
    jns~SFJump if nonnegative
    joOFJump if overflow
    jno~OFJump if not overflow
    jq/jnle~(SF ^ OF) & ~ZFJump if greater (signed >)
    jge/jnl~(SF ^ OF)Jump if greater or equal (signed >=)
    jl/jngeSF ^ OFJump if less (signed <)
    jle/jng(SF ^ OF) | ZFJump if less or equal (signed <=)
    ja/jnbe~CF & ~ZFJumP if greater (unsigned >)
    jae/jnb~CFJump if greater or equal (unsigned >=)
    jb/jnae/jcCFJump if less (unsigned <)
    jbe/jna/jncCF | ZFJump if less or equal (unsigned <=)
    + +

    Table of x86-64 Registers

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    64-bit (quad)32-bit (double)16-bit (word)8-bit (byte)8-bit (byte) 
    63..031..015..015..87..0 
    raxeaxaxahalReturn value
    rbxebxbxbhblCallee served
    rcxecxcxchcl4th arg
    rdxedxdxdhdl3rd arg
    rsiesisi sil2nd arg
    rdiedidi dil1st arg
    rbpebpbp bplCallee served
    rspespsp splStack position
    r8r8dr8w r8b5th arg
    r9r9dr9w r9b6th arg
    r10r10dr10w r10bCallee served
    r11r11dr11w r11bCallee served
    r12r12dr12w r12bCallee served
    r13r13dr13w r13bCallee served
    r14r14dr14w r14bCallee served
    r15r15dr15w r15bCallee served
    + +

    and the powers of 2 from 2102^{-10} to 2102^{10} in decimal

    + +

    Exam Seating Arrangement

    + +
      +
    • S represents a student seated at a seat
    • +
    • X represents a vacant seat
    • +
    + +

    Front of classroom is the bottom of the table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    + +

    EVERYTHING HERE (the front of the class) (included cell phones, computer and calculator)! +Keep pens, pencils, eraser with you!

    + +

    Before the midterm

    + +
      +
    • Once you have received your midterm, leave the midterm face up on your table
    • +
    • Read its instructions on its first page
    • +
    • Please, do not touch, leaf through, or write your name on the midterm +
        +
      • Why? Because it is unfair to all other students who have not yet received a midterm
      • +
      +
    • +
    • When all students have received a midterm, you will be instructed to start the midterm
    • +
    + +

    During the midterm

    + +

    If you have a question

    + +
      +
    1. Have a look at the instructions on Page 1 for an answer
    2. +
    3. If the answer to your question is not on Page 1, +raise your hand and the instructor will attend to +your question – the instructor is the only +person answering questions (TAs will not be +answering questions)
    4. +
    + +

    At the end of midterm

    + +

    When you are told to: Please, stop writing! +please, do stop writing and hand in our +midterm as instructed

    + +

    Why? If you continue writing, it is unfair to all +other students who have stopped writing

    + +

    Questions?

    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/midterm/about/about/index.html b/_site/melody/cmpt-295/midterm/about/about/index.html new file mode 100644 index 0000000..55c0744 --- /dev/null +++ b/_site/melody/cmpt-295/midterm/about/about/index.html @@ -0,0 +1,449 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    CMPT 295 – About our Midterm 1

    + +

    Midterm 1

    + +
      +
    • When: Friday, Oct. 15 9:30am – 10:20am
    • +
    • Where: same classroom
    • +
    • Duration: 45 minutes (+ 5 minutes to organize ourselves)
    • +
    + +

    What to study?

    + +

    Everything we covered up until (and including) +our lecture on Friday, Oct. 8 is subject to +examination

    + +
      +
    • All material from Lecture 1 until and including Lecture 14 (partial +lecture notes + annotated lecture notes + your own lecture +notes + recorded lectures)
    • +
    • Labs 1 to 3, Assignments 1, 2, 3 and 4 (even though Assignment 4 +is due after our Midterm 1)
    • +
    • All demo code posted on our course web site
    • +
    • Textbook Readings + Practice Problems: +
        +
      • Chapter 2, Section 2.1 to 2.4 (skip 2.3.7) + Practice Problems 2.1 to 2.34, 2. 38 to 2.41, 2.44, 2.45, 2.47, 2.48, 2.50, 2.52, 2. 54
      • +
      • Chapter 3, Section 3.2 to 3.6 (skip 3.4.4 for now, skip 3.5.5 and 3.6.8) + Practice Problems 3.1 to 3.11, 3.13 to 3.16 B. (not A.), 3.18, 3.20, +3.21 to 3.26, 3.28
      • +
      +
    • +
    + +

    Format of Midterm 1

    + +
      +
    • First section: questions that require short answers. For example: +
        +
      • “Yes/no” or
      • +
      • “fill in the blank” or
      • +
      • a few words
      • +
      • etc…
      • +
      +
    • +
    • Second section: questions that require written answers. For example: +
        +
      • Calculating – we always show every step of our work
      • +
      • Writing/completing C or assembly code
      • +
      • Hand tracing code
      • +
      • Writing an answer (in English)
      • +
      • etc…
      • +
      +
    • +
    + +

    Appendix

    + +

    As part of Midterm 1’s appendix, we will +be given the following information:

    + +

    Table of x86-64 Jumps:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    InstructionConditionDescription
    jmpalwaysUnconditional jump
    je/jzZFJump if equal / zero
    jne/jnz~ZFJump if not equal / not zero
    jsSFJump if negative
    jns~SFJump if nonnegative
    joOFJump if overflow
    jno~OFJump if not overflow
    jq/jnle~(SF ^ OF) & ~ZFJump if greater (signed >)
    jge/jnl~(SF ^ OF)Jump if greater or equal (signed >=)
    jl/jngeSF ^ OFJump if less (signed <)
    jle/jng(SF ^ OF) | ZFJump if less or equal (signed <=)
    ja/jnbe~CF & ~ZFJumP if greater (unsigned >)
    jae/jnb~CFJump if greater or equal (unsigned >=)
    jb/jnae/jcCFJump if less (unsigned <)
    jbe/jna/jncCF | ZFJump if less or equal (unsigned <=)
    + +

    Table of x86-64 Registers

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    64-bit (quad)32-bit (double)16-bit (word)8-bit (byte)8-bit (byte) 
    63..031..015..015..87..0 
    raxeaxaxahalReturn value
    rbxebxbxbhblCallee served
    rcxecxcxchcl4th arg
    rdxedxdxdhdl3rd arg
    rsiesisi sil2nd arg
    rdiedidi dil1st arg
    rbpebpbp bplCallee served
    rspespsp splStack position
    r8r8dr8w r8b5th arg
    r9r9dr9w r9b6th arg
    r10r10dr10w r10bCallee served
    r11r11dr11w r11bCallee served
    r12r12dr12w r12bCallee served
    r13r13dr13w r13bCallee served
    r14r14dr14w r14bCallee served
    r15r15dr15w r15bCallee served
    + +

    and the powers of 2 from 2102^{-10} to 2102^{10} in decimal

    + +

    Exam Seating Arrangement

    + +
      +
    • S represents a student seated at a seat
    • +
    • X represents a vacant seat
    • +
    + +

    Front of classroom is the bottom of the table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    + +

    EVERYTHING HERE (the front of the class) (included cell phones, computer and calculator)! +Keep pens, pencils, eraser with you!

    + +

    Before the midterm

    + +
      +
    • Once you have received your midterm, leave the midterm face up on your table
    • +
    • Read its instructions on its first page
    • +
    • Please, do not touch, leaf through, or write your name on the midterm +
        +
      • Why? Because it is unfair to all other students who have not yet received a midterm
      • +
      +
    • +
    • When all students have received a midterm, you will be instructed to start the midterm
    • +
    + +

    During the midterm

    + +

    If you have a question

    + +
      +
    1. Have a look at the instructions on Page 1 for an answer
    2. +
    3. If the answer to your question is not on Page 1, +raise your hand and the instructor will attend to +your question – the instructor is the only +person answering questions (TAs will not be +answering questions)
    4. +
    + +

    At the end of midterm

    + +

    When you are told to: Please, stop writing! +please, do stop writing and hand in our +midterm as instructed

    + +

    Why? If you continue writing, it is unfair to all +other students who have stopped writing

    + +

    Questions?

    + + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/midterm/cmpt-295-about-midterm.zip b/_site/melody/cmpt-295/midterm/cmpt-295-about-midterm.zip new file mode 100644 index 0000000..534aad2 Binary files /dev/null and b/_site/melody/cmpt-295/midterm/cmpt-295-about-midterm.zip differ diff --git a/_site/melody/cmpt-295/midterm2/About_our_Midterm_2.pdf b/_site/melody/cmpt-295/midterm2/About_our_Midterm_2.pdf new file mode 100644 index 0000000..69f760d Binary files /dev/null and b/_site/melody/cmpt-295/midterm2/About_our_Midterm_2.pdf differ diff --git a/_site/melody/cmpt-295/midterm2/midterm2.html b/_site/melody/cmpt-295/midterm2/midterm2.html new file mode 100644 index 0000000..99d5140 --- /dev/null +++ b/_site/melody/cmpt-295/midterm2/midterm2.html @@ -0,0 +1,461 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    CMPT 295 – About our Midterm 2

    + +

    Midterm 2

    + +
      +
    • When: Friday, Nov. 12, 9:30am – 10:20am
    • +
    • Where: C9001 (same classroom as our lectures)
    • +
    • Duration: 45 minutes (+ 5 minutes to organize ourselves)
    • +
    + +

    What to study?

    + +

    Everything we covered up until (and including) our +lecture on Friday, Nov. 5 is subject to examination

    + +
      +
    • All material from Lecture 1 until and including Lecture 25 (partial +lecture notes + annotated lecture notes + your own lecture notes + +recorded lectures)
    • +
    • Labs 1 to 4, Assignments 1 to 7 (even though Assignment 7 is due after +our Midterm 2)
    • +
    • All demo code posted on our course web site
    • +
    • All Readings + Practice Problems: +
        +
      • Chapters 2 and 3 + Practice Problems (see Midterm 1 Instructions for +listing)
      • +
      • Chapter 3, Section 3.7, 3.8 (skip 3.8.5 and 3.9), 3.10 (skip 3.10.5 and +3.11), +
          +
        • Practice Problems 3.32 to 3.40, 3.46, 3.48
        • +
        +
      • +
      +
    • +
    • Emphasis on the material seen from Lectures 17 to 25
    • +
    + +

    Format of Midterm 2

    + +
      +
    • First section: questions that require short answers. For example: +
        +
      • “Yes/no” or
      • +
      • “fill in the blank” or
      • +
      • a few words/numbers
      • +
      • etc…
      • +
      +
    • +
    • Second section: questions that require written +answers. For example: +
        +
      • Calculating – we always show every step of our work
      • +
      • Writing/completing C or assembly code
      • +
      • Drawing
      • +
      • Hand tracing code
      • +
      • Writing an answer (in English)
      • +
      • etc…
      • +
      +
    • +
    + +

    Appendix

    + +

    As part of Midterm 2’s appendix, we will +be given the following information:

    + +

    Table of x86-64 Jumps:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    InstructionConditionDescription
    jmpalwaysUnconditional jump
    je/jzZFJump if equal / zero
    jne/jnz~ZFJump if not equal / not zero
    jsSFJump if negative
    jns~SFJump if nonnegative
    joOFJump if overflow
    jno~OFJump if not overflow
    jq/jnle~(SF ^ OF) & ~ZFJump if greater (signed >)
    jge/jnl~(SF ^ OF)Jump if greater or equal (signed >=)
    jl/jngeSF ^ OFJump if less (signed <)
    jle/jng(SF ^ OF) | ZFJump if less or equal (signed <=)
    ja/jnbe~CF & ~ZFJumP if greater (unsigned >)
    jae/jnb~CFJump if greater or equal (unsigned >=)
    jb/jnae/jcCFJump if less (unsigned <)
    jbe/jna/jncCF | ZFJump if less or equal (unsigned <=)
    + +

    Table of x86-64 Registers

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    64-bit (quad)32-bit (double)16-bit (word)8-bit (byte)8-bit (byte) 
    63..031..015..015..87..0 
    raxeaxaxahalReturn value
    rbxebxbxbhblCallee served
    rcxecxcxchcl4th arg
    rdxedxdxdhdl3rd arg
    rsiesisi sil2nd arg
    rdiedidi dil1st arg
    rbpebpbp bplCallee served
    rspespsp splStack position
    r8r8dr8w r8b5th arg
    r9r9dr9w r9b6th arg
    r10r10dr10w r10bCallee served
    r11r11dr11w r11bCallee served
    r12r12dr12w r12bCallee served
    r13r13dr13w r13bCallee served
    r14r14dr14w r14bCallee served
    r15r15dr15w r15bCallee served
    + +

    and the powers of 2 from 2102^{-10} to 2162^{16} in decimal.

    + +

    Exam Seating Arrangement

    + +
      +
    • S represents a student seated at a seat
    • +
    • X represents a vacant seat
    • +
    + +

    Front of classroom is the bottom of the table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    + +

    EVERYTHING HERE (the front of the class) (included cell phones, computer and calculator)! +Keep pens, pencils, eraser with you!

    + +

    Before the midterm

    + +
      +
    • Once you have received your midterm, leave the +midterm face up on your table
    • +
    • Read its instructions on its first page
    • +
    • Please, do not touch, leaf through, or write your +name on the midterm +
        +
      • Why? Because it is unfair to all other students who have +not yet received a midterm
      • +
      +
    • +
    • When all students have received a midterm, you will +be instructed to start the midterm
    • +
    + +

    During the midterm

    + +

    If you have a question

    + +
      +
    1. Have a look at the instructions on Page 1 for an +answer
    2. +
    3. If the answer to your question is not on Page 1, +raise your hand and the instructor will attend to +your question – the instructor is the only +person answering questions (TAs will not be +answering questions)
    4. +
    + +

    At the end of midterm

    + +
      +
    • When you are told to Please, stop writing! please, do stop writing and hand in our midterm as instructed
    • +
    • Why? If you continue writing, it is unfair to all +other students who have stopped writing
    • +
    + +

    Questions?

    + +

    (Transcriber’s note: rest of slide is blank)

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt-295/midterm2/midterm2/index.html b/_site/melody/cmpt-295/midterm2/midterm2/index.html new file mode 100644 index 0000000..99d5140 --- /dev/null +++ b/_site/melody/cmpt-295/midterm2/midterm2/index.html @@ -0,0 +1,461 @@ + + + + + | tait.tech + + + + + + +
    +
    +

    CMPT 295 – About our Midterm 2

    + +

    Midterm 2

    + +
      +
    • When: Friday, Nov. 12, 9:30am – 10:20am
    • +
    • Where: C9001 (same classroom as our lectures)
    • +
    • Duration: 45 minutes (+ 5 minutes to organize ourselves)
    • +
    + +

    What to study?

    + +

    Everything we covered up until (and including) our +lecture on Friday, Nov. 5 is subject to examination

    + +
      +
    • All material from Lecture 1 until and including Lecture 25 (partial +lecture notes + annotated lecture notes + your own lecture notes + +recorded lectures)
    • +
    • Labs 1 to 4, Assignments 1 to 7 (even though Assignment 7 is due after +our Midterm 2)
    • +
    • All demo code posted on our course web site
    • +
    • All Readings + Practice Problems: +
        +
      • Chapters 2 and 3 + Practice Problems (see Midterm 1 Instructions for +listing)
      • +
      • Chapter 3, Section 3.7, 3.8 (skip 3.8.5 and 3.9), 3.10 (skip 3.10.5 and +3.11), +
          +
        • Practice Problems 3.32 to 3.40, 3.46, 3.48
        • +
        +
      • +
      +
    • +
    • Emphasis on the material seen from Lectures 17 to 25
    • +
    + +

    Format of Midterm 2

    + +
      +
    • First section: questions that require short answers. For example: +
        +
      • “Yes/no” or
      • +
      • “fill in the blank” or
      • +
      • a few words/numbers
      • +
      • etc…
      • +
      +
    • +
    • Second section: questions that require written +answers. For example: +
        +
      • Calculating – we always show every step of our work
      • +
      • Writing/completing C or assembly code
      • +
      • Drawing
      • +
      • Hand tracing code
      • +
      • Writing an answer (in English)
      • +
      • etc…
      • +
      +
    • +
    + +

    Appendix

    + +

    As part of Midterm 2’s appendix, we will +be given the following information:

    + +

    Table of x86-64 Jumps:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    InstructionConditionDescription
    jmpalwaysUnconditional jump
    je/jzZFJump if equal / zero
    jne/jnz~ZFJump if not equal / not zero
    jsSFJump if negative
    jns~SFJump if nonnegative
    joOFJump if overflow
    jno~OFJump if not overflow
    jq/jnle~(SF ^ OF) & ~ZFJump if greater (signed >)
    jge/jnl~(SF ^ OF)Jump if greater or equal (signed >=)
    jl/jngeSF ^ OFJump if less (signed <)
    jle/jng(SF ^ OF) | ZFJump if less or equal (signed <=)
    ja/jnbe~CF & ~ZFJumP if greater (unsigned >)
    jae/jnb~CFJump if greater or equal (unsigned >=)
    jb/jnae/jcCFJump if less (unsigned <)
    jbe/jna/jncCF | ZFJump if less or equal (unsigned <=)
    + +

    Table of x86-64 Registers

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    64-bit (quad)32-bit (double)16-bit (word)8-bit (byte)8-bit (byte) 
    63..031..015..015..87..0 
    raxeaxaxahalReturn value
    rbxebxbxbhblCallee served
    rcxecxcxchcl4th arg
    rdxedxdxdhdl3rd arg
    rsiesisi sil2nd arg
    rdiedidi dil1st arg
    rbpebpbp bplCallee served
    rspespsp splStack position
    r8r8dr8w r8b5th arg
    r9r9dr9w r9b6th arg
    r10r10dr10w r10bCallee served
    r11r11dr11w r11bCallee served
    r12r12dr12w r12bCallee served
    r13r13dr13w r13bCallee served
    r14r14dr14w r14bCallee served
    r15r15dr15w r15bCallee served
    + +

    and the powers of 2 from 2102^{-10} to 2162^{16} in decimal.

    + +

    Exam Seating Arrangement

    + +
      +
    • S represents a student seated at a seat
    • +
    • X represents a vacant seat
    • +
    + +

    Front of classroom is the bottom of the table:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    SXSXSXS
    + +

    EVERYTHING HERE (the front of the class) (included cell phones, computer and calculator)! +Keep pens, pencils, eraser with you!

    + +

    Before the midterm

    + +
      +
    • Once you have received your midterm, leave the +midterm face up on your table
    • +
    • Read its instructions on its first page
    • +
    • Please, do not touch, leaf through, or write your +name on the midterm +
        +
      • Why? Because it is unfair to all other students who have +not yet received a midterm
      • +
      +
    • +
    • When all students have received a midterm, you will +be instructed to start the midterm
    • +
    + +

    During the midterm

    + +

    If you have a question

    + +
      +
    1. Have a look at the instructions on Page 1 for an +answer
    2. +
    3. If the answer to your question is not on Page 1, +raise your hand and the instructor will attend to +your question – the instructor is the only +person answering questions (TAs will not be +answering questions)
    4. +
    + +

    At the end of midterm

    + +
      +
    • When you are told to Please, stop writing! please, do stop writing and hand in our midterm as instructed
    • +
    • Why? If you continue writing, it is unfair to all +other students who have stopped writing
    • +
    + +

    Questions?

    + +

    (Transcriber’s note: rest of slide is blank)

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt/03/index.html b/_site/melody/cmpt/03/index.html new file mode 100644 index 0000000..c2763ee --- /dev/null +++ b/_site/melody/cmpt/03/index.html @@ -0,0 +1,859 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295

    + +

    Unit - Data Representation

    + +

    Lecture 3 – Representing integral numbers in memory - unsigned and signed

    + +

    Last Lecture

    +
      +
    • Von Neumann architecture +
        +
      • Architecture of most computers
      • +
      • Its components: CPU, memory, input and ouput, bus
      • +
      • One of its characteristics: Data and code (programs) both stored in memory
      • +
      +
    • +
    • A look at memory: defined byte-addressable memory, diagram of (compressed) memory +
        +
      • Word size (w): size of a series of bits (or bit vector) we manipulate, also size of machine words (see Section 2.1.2)
      • +
      +
    • +
    • A look at bits in memory +
        +
      • Why binary numeral system (0 and 1 -> two values) is used to represent information in memory
      • +
      • Algorithm for converting binary to hexadecimal (hex) +
          +
        1. Partition bit vector into groups of 4 bits, starting from right, i.e., least significant byte (LSB) +
            +
          • If most significant “byte” (MSB) does not have 8 bits, pad it: add 0’s to its left
          • +
          +
        2. +
        3. Translate each group of 4 bits into its hex value
        4. +
        +
      • +
      • What do bits represent? Encoding scheme gives meaning to bits
      • +
      • Order of bytes in memory: little endian versus big endian
      • +
      +
    • +
    • Bit manipulation – regardless of what bit vectors represent +
        +
      • Boolean algebra: bitwise operations => AND (&), OR (|), XOR (^), NOT (~)
      • +
      • Shift operations: left shift, right logical shift and right arithmetic shift +
          +
        • Logical shift: Fill x with y 0’s on left
        • +
        • Arithmetic shift: Fill x with y copies of x‘s sign bit on left
        • +
        • Sign bit: Most significant bit (MSb) before shifting occurred
        • +
        +
      • +
      +
    • +
    + +

    NOTE: C logical operators and C bitwise (bit-level) operators behave differently! Watch out for && versus &, || versus |, …

    + +

    Today’s Menu

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    Warm up exercise!

    + +

    As a warm up exercise, fill in the blanks!

    + +
      +
    • If the context is C (on our target machine) +
        +
      • char => _____ bits/ _____ byte
      • +
      • short => _____ bits/ _____ bytes
      • +
      • int => _____ bits/ _____ bytes
      • +
      • long => _____ bits/ _____ bytes
      • +
      • float => _____ bits/ _____ bytes
      • +
      • double => _____ bits/ _____ bytes
      • +
      • pointer (e.g. char *) => _____ bits/ _____ bytes
      • +
      +
    • +
    + +

    Unsigned integral numbers

    + +

    Remember:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-1 
     
    0x000301000010
    0x000201101001
    0x000101110100
    0x000001110011
    + +
      +
    • What if the byte at M[0x0002] represented an unsigned integral number, what would be its value? +x = a series of bits = bit vector +w = width of bit vector
    • +
    • + X=011010012,w=8X = 01101001_{2}, w=8 +
    • +
    • Let’s apply the encoding scheme:
    • +
    + +

    B2U(X)=i=0w1Xi×2i +\it \text{B2U}(X) = \sum_{i=0}^{w-1} X_{i} \times 2^i +

    + +

    Example: 0×27+1×26+1×25+0×24+1×23+0×22+1×20=0 \times 2^7 + 1 \times 2^6 + 1 \times 2^5 + 0 \times 2^4 + 1 \times 2^3 + 0 \times 2^2 + 1 \times 2^0 =

    + +
      +
    • For w = 8, range of possible unsigned values: [ blank ]
    • +
    • For any w, range of possible unsigned values: [ blank ]
    • +
    • Conclusion: w bits can only represent a fixed # of possible values, but these w bits represent these values exactly
    • +
    + +

    B2U(X) Conversion (Encoding scheme)

    +
      +
    • Positional notation: expand and sum all terms
    • +
    + +

    Decimal:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    did_{i}10i10^{i}
    di1d_{i-1}10i110_{i-1}
    d2d_{2}100
    d1d_{1}10
    d0d_{0}1
    + +

    Binary:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    bib_{i}2i2^{i}
    bi1b_{i-1}2i12^{i-1}
    b2b_{2}4
    b1b_{1}2
    b0b_{0}1
    + +

    Remember: +B2U(X)=i=0w1Xi×2i +\it \text{B2U}(X) = \sum_{i=0}^{w-1} X_{i} \times 2^i +

    + +

    Example: 24610=2×102+4×101+6×100246_{10} = 2 \times 10^{2} + 4 \times 10^{1} + 6 \times 10^{0}

    + +

    Range of possible values?

    + +
      +
    • If the context is C (on our target machine) +
        +
      • unsigned char?
      • +
      • unsigned short?
      • +
      • unsigned int?
      • +
      • unsigned long?
      • +
      +
    • +
    + +

    Examples of “Show your work”

    + +

    U2B(X) Conversion (into 8-bit binary # => w = 8)

    + +

    Method 1 - Using subtraction: subtracting decreasing power of 2 until reach 0:

    + +

    Starting number: 246

    + +
      +
    • 246128=118246 – 128 = 118 -> 128=1×27128 = 1 \times 2^{7}
    • +
    • 11864=54118 – 64 = 54 -> 64=1×2664 = 1 \times 2^{6}
    • +
    • 5432=2254 – 32 = 22 -> 32=1×2532 = 1 \times 2^{5}
    • +
    • 2216=622 – 16 = 6 -> 16=1×2416 = 1 \times 2^{4}
    • +
    • 68=nop!6 – 8 = \text{nop!} -> 8=0×238 = 0 \times 2^{3}
    • +
    • 64=26 – 4 = 2 -> 4=1×224 = 1 \times 2^{2}
    • +
    • 22=02 – 2 = 0 -> 2=1×212 = 1 \times 2^{1}
    • +
    • 01=nop!0 – 1 = \text{nop!} -> 1=0×201 = 0 \times 2^{0}
    • +
    • + 24610=111101102246_{10} = 11110110_{2} +
    • +
    + +

    Method 2 - Using division: dividing by 2 until reach 0

    + +

    Start with 246

    + +
      +
    • + 246÷2=123,R=0246 \div 2 = 123,R=0 +
    • +
    • + 123÷2=61,R=1123 \div 2 = 61,R=1 +
    • +
    • + 61÷2=30,R=161 \div 2 = 30,R=1 +
    • +
    • + 30÷2=15,R=030 \div 2 = 15,R=0 +
    • +
    • + 15÷2=7,R=115 \div 2 = 7,R=1 +
    • +
    • + 7÷2=3,R=17 \div 2 = 3,R=1 +
    • +
    • + 3÷2=1,R=13 \div 2 = 1,R=1 +
    • +
    • + 1÷2=0,R=11 \div 2 = 0,R=1 +
    • +
    • + 24610=111101102246_{10} = 11110110_{2} +
    • +
    + +

    U2B(X) Conversion – A few tricks

    +
      +
    • Decimal -> binary +
        +
      • Trick: When decimal number is 2n, then its binary representation is 1 followed by n zero’s
      • +
      • Let’s try: if X = 32 => X = 25, then n = 5 => 100002 (w = 5) What if w = 8? Check: 1 x 24 = 32
      • +
      +
    • +
    • Decimal -> hex +
        +
      • Trick: When decimal number is 2n, then its hexadecimal representation is 2i followed by j zero’s, where n = i + 4j and 0 <= i <=3
      • +
      • Let try: if X = 8192 => X = 213, then n = 13 and 13 = i + 4j => 1 + 4 x 3 +=> 0x2000. Convert 0x2000 into a binary number: Check: 2 x 163 = 2 x 4096 = 8192
      • +
      +
    • +
    + +

    Signed integral numbers

    + +

    Remember:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    AddressM[]
    size-1 
     
    0x000301000010
    0x000201101001
    0x000101110100
    0x000001110011
    + +
      +
    • What if the byte at M[0x0001] represented a signed integral number, what would be its value?
    • +
    • X = 111101002 w = 8
    • +
    • T => Two’s Complement, w => width of the bit vector (annotation: first part of equaltion [everything before the plus sign] is the “sign bit”)
    • +
    • Let’s apply the encoding scheme:
    • +
    + +

    B2T(X)=xw1×2w1+i=0w2xi×2i +\it {B2T}(X) = -x_{w-1} \times 2^{w-1} + \sum_{i=0}^{w-2} x_{i} \times 2^{i} +

    + +

    Example: 1×27+1×26+1×25+1×24+0×23+1×22+0×21+0×20=?-1 \times 2^{7} + 1 \times 2^{6} + 1 \times 2^{5} + 1 \times 2^{4} + 0 \times 2^{3} + 1 \times 2^{2} + 0 \times 2^{1} + 0 \times 2^{0} = ?

    + +
      +
    • What would be the bit pattern of the … +
        +
      • Most negative value:
      • +
      • Most positive value:
      • +
      +
    • +
    • For w = 8, range of possible signed values: [ blank ]
    • +
    • For any w, range of possible signed values: [ blank ]
    • +
    • Conclusion: same as for unsigned integral numbers
    • +
    + +

    Examples of “Show your work”

    + +

    T2B(X) Conversion -> Two’s Complement

    + +

    Annotation: w = 8

    + +

    Method 1: If X < 0, (~(U2B(|X|)))+1

    + +

    If X = -14 (and 8 bit binary #s)

    + +
      +
    1. + X=>14=\lvert X\rvert => \lvert -14 \vert = +
    2. +
    3. U2B(14)\text{U2B}(14) =>
    4. +
    5. (first symbol is a tilde) (000011102)\sim(00001110_{2}) =>
    6. +
    7. (111100012)+1(11110001_{2})+1 =>
    8. +
    + +

    Binary addition:

    +
    +  11110001
    ++ 00000001
    += ????????
    +
    + +

    Method 2: If X = -14 (and 8 bit binary #s)

    + +
      +
    1. + X+2w=>14+X + 2^{w} => -14 + +
    2. +
    3. U2B(242)\text{U2B}(242) =>
    4. +
    + +

    Using subtraction:

    + +
      +
    1. 242128=114242 - 128 = 114 -> 1×271 \times 2^{7}
    2. +
    3. 11464=50114 - 64 = 50 -> 1×261 \times 2^{6}
    4. +
    5. 5032=1850 – 32 = 18 -> 1×251 \times 2^{5}
    6. +
    7. 1816=218 – 16 = 2 -> 1×241 \times 2^{4}
    8. +
    9. 28=nop!2 – 8 = \text{nop!} -> 0×230 \times 2^{3}
    10. +
    11. 24=nop!2 – 4 = \text{nop!} -> 0×220 \times 2^{2}
    12. +
    13. 22=02 – 2 = 0 -> 1×211 \times 2^{1}
    14. +
    15. 01=nop!0 – 1 = \text{nop!} -> 0×200 \times 2^{0}
    16. +
    + +

    Properties of unsigned & signed conversions

    + +

    Annotation: w = 4

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    XB2U(X)B2T(X)
    000000
    000111
    001022
    001133
    010044
    010155
    011066
    011177
    10008-8
    10019-7
    101010-6
    101111-5
    110012-4
    110113-3
    111014-2
    111115-1
    + +
      +
    • Equivalence +
        +
      • Both encoding schemes (B2U and B2T ) produce the same bit patterns for nonnegative values
      • +
      +
    • +
    • Uniqueness +
        +
      • Every bit pattern produced by these encoding schemes (B2U and B2T) represents a unique (and exact) integer value
      • +
      • Each representable integer has unique bit pattern
      • +
      +
    • +
    + +

    Converting between signed & unsigned of same size (same data type)

    + +
      +
    • Unsigned +
        +
      • w=8
      • +
      • if unsigned ux = 12910
      • +
      • U2T(X) = B2T(U2B(X))
      • +
      • then x = ???
      • +
      • Maintain Same Bit Pattern
      • +
      +
    • +
    • Signed (Two’s Complement) +
        +
      • w=4
      • +
      • if signed (2’s C) x=510x = -5_{10}
      • +
      • T2U(X) = B2U(T2B(X))
      • +
      • then unsigned ux = ???
      • +
      • Maintain Same Bit Pattern
      • +
      +
    • +
    • Conclusion - Converting between unsigned and signed numbers: +Both have same bit pattern, however, this bit pattern may be interpreted differently, i.e., producing a different value
    • +
    + +

    Converting signed to unsigned (and back) with w=4w = 4

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    SignedBitsUnsignedNote
    000000All rows from 0-7 inclusive can be converted from signed to unsigned with T2U(X), and unsigned to signed with U2T(X).
    100011 
    200102 
    300113 
    401004 
    501015 
    601106 
    701117 
    -810008All rows from here to 15 inclusive can be converted to the other like so: T2U(signed + 16/242^{4}) -> unsigned, U2T(unsigned - 16/242^{4}) -> signed.
    -710019 
    -6101010 
    -5101111 
    -4110012 
    -3110113 
    -2111014 
    -1111115 
    + +

    Visualizing the relationship between signed & unsigned

    +

    If w=4,24=16w = 4, 2^{4} = 16

    + +
      +
    • Signed (2’s Complement) Range: TMin to TMax (0 is the center)
    • +
    • Unsigned range: 0 to UMax (TMax is the center)
    • +
    + +

    Sign extension

    +
      +
    • Converting unsigned (or signed) of different sizes (different data types) +
        +
      1. Small data type -> larger +
          +
        • Sign extension +
            +
          • Unsigned: zero extension
          • +
          • Signed: sign bit extension
          • +
          +
        • +
        +
      2. +
      +
    • +
    • Conclusion: Value unchanged
    • +
    • Let’s try: +
        +
      • Going from a data type that has a width of 3 bits (w = 3) to a data type that has a width of 5 bits (w = 5)
      • +
      +
    • +
    • Unsigned: X=3=0112,w=3X = 3 = 011_{2},w=3, X=4=1002,w=3X = 4 = 100_{2},w = 3 +
        +
      • New: X=?=?2,w=5X = ? = ?_{2},w=5, X=?+?2,w=5X = ? + ?_{2},w=5
      • +
      +
    • +
    • Signed: X=3=0112,w=3X = 3 = 011_{2},w=3, X=3=1012,w=3X=-3 = 101_{2},w=3 +
        +
      • New: X=?=?2,w=5X = ? = ?_{2},w=5, x=?=?2,w=5x = ? = ?_{2}, w=5
      • +
      +
    • +
    + +

    Truncation

    + +
      +
    • Converting unsigned (or signed) of different sizes(different data types) +
        +
      1. Large data type -> smaller +
          +
        • Truncation
        • +
        +
      2. +
      +
    • +
    • Conclusion: Value may be altered +
        +
      • A form of overflow
      • +
      +
    • +
    • Let’s try: +
        +
      • Going from a data type that has a width of 5 bits (w = 5) to a data type that has a width of 3 bits (w = 3)
      • +
      +
    • +
    • Unsigned: X=27=110112,w=5X = 27 = 11011_{2},w = 5 +
        +
      • New: X=?=?2,w=3X = ? = ?_{2},w=3
      • +
      +
    • +
    • Signed: X=15=100012,w=3X = -15 = 10001_{2},w=3, X=1=111112,w=5X = -1 = 11111_{2}, w=5 +
        +
      • New: X=?=?2,w=3X = ? = ?_{2}, w=3, X=?=?2,w=3X = ? = ?_{2}, w=3
      • +
      +
    • +
    + +

    Summary

    + +
      +
    • Interpretation of bit pattern B into either unsigned value U or signed value T +
        +
      • B2U(X) and U2B(X) encoding schemes (conversion)
      • +
      • B2T(X) and T2B(X) encoding schemes (conversion) +
          +
        • Signed value expressed as two’s complement => T
        • +
        +
      • +
      +
    • +
    • Conversions from unsigned <-> signed values +
        +
      • U2T(X) and T2U(X) => adding or subtracting 2w2^{w}
      • +
      +
    • +
    • Implication in C: when converting (implicitly via promotion and explicitly via casting): +
        +
      • Sign: +
          +
        • Unsigned <-> signed (of same size) -> Both have same bit pattern, however, this bit pattern may be interpreted differently +
            +
          • Can have unexpected effects -> producing a different value
          • +
          +
        • +
        +
      • +
      • Size: +
          +
        • Small -> large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int) +
            +
          • sign extension: For unsigned -> zeros extension, for signed -> sign bit extension
          • +
          • Both yield expected result –> resulting value unchanged
          • +
          +
        • +
        • Large -> small (e.g., unsigned int to unsigned short) +
            +
          • truncation: Unsigned/signed -> most significant bits are truncated (discarded)
          • +
          • May not yield expected results -> original value may be altered
          • +
          +
        • +
        +
      • +
      • Both (sign and size): 1) size conversion is first done then 2) sign conversion is done
      • +
      +
    • +
    + +

    Next Lecture

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +19 +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt/04/index.html b/_site/melody/cmpt/04/index.html new file mode 100644 index 0000000..599076b --- /dev/null +++ b/_site/melody/cmpt/04/index.html @@ -0,0 +1,586 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295

    + +

    Unit - Data Representation

    + +

    Lecture 4 – Representing integral numbers in memory – Arithmetic operations

    + +

    Warm up question

    +
      +
    • What is the value of … +
        +
      • TMin (in hex) for signed char in C: _________________
      • +
      • TMax (in hex) for signed int in C: _________________
      • +
      • TMin (in hex) for signed short in C: ________________
      • +
      +
    • +
    + +

    Last Lecture

    +
      +
    • Interpretation of bit pattern B into either unsigned value U or signed value T +
        +
      • B2U(X) and U2B(X) encoding schemes (conversion)
      • +
      • B2T(X) and T2B(X) encoding schemes (conversion) +
          +
        • Signed value expressed as two’s complement => T
        • +
        +
      • +
      +
    • +
    • Conversions from unsigned <-> signed values +
        +
      • U2T(X) and T2U(X) => adding or subtracting 2w
      • +
      +
    • +
    • Implication in C: when converting (implicitly via promotion and explicitly via casting): +
        +
      • Sign: +
          +
        • Unsigned <-> signed (of same size) -> Both have same bit pattern, however, this bit pattern may be interpreted differently +
            +
          • Can have unexpected effects -> producing a different value
          • +
          +
        • +
        +
      • +
      • Size: +
          +
        • Small -> large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int) +
            +
          • sign extension: For unsigned -> zeros extension, for signed -> sign bit extension
          • +
          • Both yield expected result –> resulting value unchanged
          • +
          +
        • +
        • Large -> small (for signed, e.g., int to short and for unsigned, e.g., unsigned int to unsigned short) +
            +
          • truncation: Unsigned/signed -> most significant bits are truncated (discarded)
          • +
          • May not yield expected results -> original value may be altered
          • +
          +
        • +
        +
      • +
      • Both (sign and size): 1) size conversion is first done then 2) sign conversion is done
      • +
      +
    • +
    + +

    Today’s Menu

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    Let’s first illustrate what we covered last lecture with a demo!

    + +

    Demo – Looking at size and sign conversions in C

    + +
      +
    • What does the demo illustrate? +
        +
      • Size conversion: +
          +
        • Converting to a larger (wider) data type -> Converting short to int
        • +
        • Converting to a smaller (narrower) data type -> Converting short to char
        • +
        +
      • +
      • Sign conversion: +
          +
        • Converting from signed to unsigned -> Converting short to unsigned short
        • +
        • Converting from unsigned to signed -> Converting unsigned short to short
        • +
        +
      • +
      • Size and Sign conversion: +
          +
        • Converting from signed to unsigned larger (wider) data type -> Converting short to unsigned int
        • +
        • Converting from signed to unsigned smaller (narrower) data type -> +Converting short to unsigned char
        • +
        +
      • +
      +
    • +
    • This demo (code and results) posted on our course web site
    • +
    + +

    Integer addition (unlimited space)

    + +
      +
    • +

      What happens when we add two decimal numbers? (Highlights show carring the one to complete the equation) +10710+93810=104510 +107_{10} + 938_{10} = 1045_{10}

      +
    • +
    • +

      Same thing happens when we add two binary numbers:

      +
    • +
    + +

    1011002+1011102=10110102 +101100_{2} + 101110_{2} = 1011010_{2} +

    + +

    Unsigned addition (limited space, i.e., fixed size in memory)

    + +

    What happens when we add two unsigned values?

    + +w=8w=8 + +

    a)

    + +

    Binary:

    + +

    001110112+010110102=? +00111011_{2} + 01011010_{2} = ? +

    + +

    Decimal:

    + +

    5910+9010=14910 +59_{10} + 90_{10} = 149_{10} +

    + +

    b)

    + +

    Binary:

    + +

    101011102+110010112=? +10101110_{2} + 11001011_{2} = ? +

    + +

    Decimal:

    + +

    17410+20310=37710 +174_{10} + 203_{10} = 377_{10} +

    + +

    Unsigned addition (+wu+^{u}_{w}) and overflow

    + +
      +
    • True/expected sum is the result of integer addition with unlimited space.
    • +
    • Actual sum is the result of unsigned addition with limited space. Discarding the carry out bit.
    • +
    • Discarding carry out bit has same effect as applying modular arithmetic
    • +
    + +

    s=U+wuv=(u+v)mod2w +s = U+^{u}_{w}v = (u + v) \mod{2^{w}} +

    + +
      +
    • Operands: w bits
    • +
    • True Sum: w+1 bits
    • +
    + +

    Closer look at unsigned addition overflow

    + +
      +
    • w = 8 -> [0..255]
    • +
    • + 25510=111111112255_{10} = 11111111_{2} +
    • +
    • + 9010=01011010290_{10} = 01011010_{2} +
    • +
    • + 4510=00101101245_{10} = 00101101_{2} +
    • +
    + +

    Example 1:

    + +

    Decimal (carry out the 1 in 135):

    + +

    9010+4510=13510 +90_{10} + 45_{10} = 135_{10} +

    + +

    Binary (carry out the 1 in 10000111210000111_{2}):

    + +

    010110102+001011012=100001112 +01011010_{2} + 00101101_{2} = 10000111_{2} +

    + +
      +
    • True sum, w=8: 10000111210000111_{2}
    • +
    • Actual (overflowed) sum, w=8: 100001112=1351010000111_{2} = 135_{10}
    • +
    + +

    Example 2:

    + +

    Decimal (carry 1 to the 3 in 300):

    + +

    25510+4510=30010 +255_{10} + 45_{10} = 300_{10} +

    + +

    Binary (carry the 1 at the beginning of the result):

    + +

    111111112+001011012=1001011002 +11111111_{2} + 00101101_{2} = 100101100_{2} +

    + +
      +
    • True sum, w=9: 1001011002=30010100101100_{2} = 300_{10}
    • +
    • Actual (overflowed) sum, w=8: 001011002=441000101100_{2} = 44_{10}
    • +
    + +

    Comparing integer addition with Overflow: Effect of unsigned addition (w = 4)

    + +

    Annotation: with unlimited space:

    + +

    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):

    + +

    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)

    + +

    An overflow occurs when there is a carry out

    + +

    For example: 15 (111121111_{2}) + 15 (111121111_{2}) = 30 (11110211110_{2}) as a true sum, and 14 (11110211110_{2}) as an actual sum.

    + +

    Signed addition (limited space, i.e., fixed size in memory)

    + +

    What happens when we add two signed values:

    + +

    w=8

    + +

    a)

    + +

    Binary: +001110112+010110102=?00111011_{2} + 01011010_{2} = ?

    + +

    Decimal: +5910+9010=1491059_{10} + 90_{10} = 149_{10}

    + +

    b)

    + +

    Binary: 101011102+110010112=?10101110_{2} + 11001011_{2} = ?

    + +

    Decimal: 8210+5310=13510-82_{10} + -53_{10} = -135_{10}

    + +

    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

    + +

    Signed addition (+wt+^{t}_{w}) and overflow

    + +

    True sum would be the result of integer addition with unlimited space: +Actual sum is the result of signed addition with limited space:

    + +

    Operands: w bits +True Sum: w+1 bits

    + +

    After discarduing carry out bit: w bits (overflow)

    + +
      +
    • Discarding carry out bit has same effect as applying modular arithmetic
    • +
    + +

    s=u+wtv=U2Tw[(u+v)mod2w] +s = u +^{t}_{w} v = \text{U2T}_{w}[(u + v) \mod 2^{w}] +

    + +

    Negative overflow and positive overflows are possible. +Diagram showing negative overflows becoming positive, and positive overflows becoming negative.

    + +

    Closer look at signed addition overflow

    +
      +
    • w = 8 -> [-128..127]
    • +
    • + 9010=01011010290_{10} = 01011010_{2} +
    • +
    • + 4510=00101101245_{10} = 00101101_{2} +
    • +
    • + 4510=110100112-45_{10} = 11010011_{2} +
    • +
    • + 9010=101001102-90_{10} = 10100110_{2} +
    • +
    + +

    Example 1:

    + +

    Decimal: 9010+4510=1351090_{10} + 45_{10} = 135_{10}

    + +

    Binary: 010110102+001011012=010000111201011010_{2} + 00101101_{2} = 010000111_{2}

    + +

    The binary result is -121

    + +

    Example 2:

    + +

    Decimal: 9010+4510=13510-90_{10} + -45_{10} = -135_{10}

    + +

    Binary: 101001102+110100112=101111001210100110_{2} + 11010011_{2} = 101111001_{2}

    + +

    Binary result is 121

    + +

    Example 3:

    + +

    Decimal: 9010+4510=4510-90_{10} + 45_{10} = -45_{10}

    + +

    Binary: 101001102+001011012=011010011210100110_{2} + 00101101_{2} = 011010011_{2}

    + +

    Example 4:

    + +

    Decimal: 9010+4510=451090_{10} + -45_{10} = 45_{10}

    + +

    Binary: 010110102+110100112=100101101201011010_{2} + 11010011_{2} = 100101101_{2}

    + +

    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.

    + +

    Visualizing signed addition overflow (w = 4)

    + +

    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

    + +

    Positive Overflow

    + +

    For example: 7 (011120111_{2}) + 1 (000120001_{2}) = 8 (100021000_{2}) is the true sum and = -8 (100021000_{2}) is the actual sum

    + +

    What about subtraction? -> Addition

    + +

    x + (-x) = 0

    + +
      +
    • Subtracting a number is equivalent to adding its additive inverse +
        +
      • Instead of subtracting a positive number, we could add its negative version:
      • +
      +
    • +
    + +

    107118=11 +107 - 118 = -11 +

    + +

    becomes:

    + +

    107+(118)=18 +107 + (-118) = -18 +

    + +
      +
    • Let‘s try:
    • +
    + +

    Decimal:

    + +1071011810=11107_{10} - 118_{10} = -11 + +

    Binary:

    + +011010112011101102=01101011_{2} - 01110110_{2} = + +

    Binary subtraction by addition:

    + +011010112+100010102=11110101201101011_{2} + 10001010_{2} = 11110101_{2} + +

    Binary subtraction by addition is equal to -11

    + +

    Check: 128+64+32+16+4+1=1110-128 + 64 + 32 + 16 + 4 + 1 = -11_{10}

    + +

    T2B(X) conversion:

    +
      +
    1. + ( (U2B(X)))+1(~(\text{U2B}(|X|)))+1 +
    2. +
    3. + ( (U2B(118)))+1(~(\text{U2B}(|-118|)))+1 +
    4. +
    5. + ( (U2B(118)))+1(~(\text{U2B}(118)))+1 +
    6. +
    7. + ((011101102))+1(\sim(01110110_{2}))+1 +
    8. +
    9. + (100010012)+1(10001001_{2})+1 +
    10. +
    11. + 10001010210001010_{2} +
    12. +
    + +

    Multiplication (wu,wt*^{u}_{w}, *^{t}_{w}) and overflow

    + +

    True product would be the result of integer multiplication with unlimited space: expected product +Actual product is the result of multiplication with limited space.

    + +
      +
    • Operands: w bits
    • +
    • True Product: 2w bits u×vu \times v
    • +
    • +

      Discard: w bits

      +
    • +
    • Discarding high order w bits has same effect as applying modular arithmetic
    • +
    + +p=uwuv=(u×v)mod2wp = u *^{u}_{w}v = (u \times v) \mod 2^{w} + +p=uwtv=U2Tw[(u×v)mod2w]p = u *^{t}_{w}v = \text{U2T}_{w}[(u \times v) \mod 2^{w}] + +
      +
    • Example: w = 4
    • +
    + +

    Decimal:

    + +510×510=25105_{10} \times 5_{10} = 25_{10} + +

    Binary:

    + +01012×01012=001100120101_{2} \times 0101_{2} = \sout{001}1001_{2} + +

    Multiplication with power-of-2 versus shifting

    + +
      +
    • If x×yx \times y where y=2ky = 2^{k} then x << k +
        +
      • For both signed and unsigned
      • +
      +
    • +
    • Example: +
        +
      • x×8=x×23x \times 8 = x \times 2^{3} = x << 3
      • +
      • x×24=(x×25)(x×23)=(x×32)(x×8)x \times 24 = (x \times 25) – (x \times 23) = (x \times 32) – (x \times 8) = (x « 5) – (x « 3) (decompose 24 in powers of 2 => 32 – 8)
      • +
      +
    • +
    • Most machines shift and add faster than multiply +
        +
      • We’ll soon see that compiler generates this code automatically +17
      • +
      +
    • +
    + +

    Summary

    + +
      +
    • Demo of size and sign conversion in C: code and results posted!
    • +
    • Addition: +
        +
      • Unsigned/signed: +
          +
        • Behave the same way at the bit level
        • +
        • Interpretation of resulting bit vector (sum) may differ
        • +
        +
      • +
      • Unsigned addition -> may overflow, i.e., (w+1)th bit is set +
          +
        • If so, then actual sum obtained => (x+y)mod2w(x + y) \mod 2^{w}
        • +
        +
      • +
      • Signed addition -> may overflow, i.e., (w+1)th bit is set +
          +
        • If so, then true sum may be too +ve -> positive overflow OR too -ve -> negative overflow
        • +
        • Then actual sum obtained => U2Tw[(x+y)mod2w]\text{U2T}_{w}[(x + y) \mod 2^{w}]
        • +
        +
      • +
      +
    • +
    • Subtraction +
        +
      • Becomes an addition where negative operands are transformed into their additive inverse (in two’s complement)
      • +
      +
    • +
    • Multiplication: +
        +
      • Unsigned: actual product obtained -> (x×y)mod2w(x \times y) \mod 2^{w}
      • +
      • Signed: actual product obtained -> $$\text{U2T}_{w}[(x \times y) \mod 2^{w}]
      • +
      • Can be replaced by additions and shifts
      • +
      +
    • +
    + +

    Next lecture

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +

    We’ll illustrate what we covered today by having a demo!

    + +
    +
    +
    +
    + + diff --git a/_site/melody/cmpt/test1/index.html b/_site/melody/cmpt/test1/index.html new file mode 100644 index 0000000..fa1042f --- /dev/null +++ b/_site/melody/cmpt/test1/index.html @@ -0,0 +1,24 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    00112&11102=00102 +0011_{2} \And 1110_{2} = 0010_{2} +

    + +
    +
    +
    +
    + + diff --git a/_site/melody/melodys-transcriptions.zip b/_site/melody/melodys-transcriptions.zip new file mode 100644 index 0000000..427a33d Binary files /dev/null and b/_site/melody/melodys-transcriptions.zip differ diff --git a/_site/melody/melodys-transcriptions/cmpt225-05/225 L5 - Dynamic DS - LL.html b/_site/melody/melodys-transcriptions/cmpt225-05/225 L5 - Dynamic DS - LL.html new file mode 100644 index 0000000..837e445 --- /dev/null +++ b/_site/melody/melodys-transcriptions/cmpt225-05/225 L5 - Dynamic DS - LL.html @@ -0,0 +1,364 @@ + + + + + | tait.tech + + + + + +
    +
    +

    Dynamic Data Structures - Linked Lists

    + +

    Transcriber’s 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”, it’s not just a pointer to the value.

    + +

    Dynamic Data Structures

    + +
      +
    • Basic types (e.g. int, char, bool, …), and arrays of these store a fixed amount of data.
    • +
    • We want implementations of ADTs like stacks + queues to grow & shrink (their memory use) as needed. +
        +
      • e.g. like Vector, Array-something? (can’t read), String classes
      • +
      +
    • +
    + +

    Basic Idea:

    + +

    Store data in a collection of (simple) objects. +add/delete these as needed; link them all together to make the main object.

    + +

    Linked Lists

    + +
      +
    • A sequence of simple objects (nodes): each storing one datum (plus a link…) linked together in a chain
    • +
    • E.g., to store the list <3, 5, 7>:
    • +
    + + + + + + + + + + + + + + + + + + + + + + +
    DatumLink
    3link
    5link
    7link to nowhere
    + +
      +
    • These objects have no names, (in contrast to declared values) +
        +
      • we access them by following links +
          +
        • in Java, references (implemented as pointers)
        • +
        • in C++, pointers
        • +
        +
      • +
      +
    • +
    + +

    Need one named place to start like so:

    + +

    A normal variable of type “pointer to a node”: First

    + + + + + + + + + + + + + + + + + + + + + + +
    DataLink
    3pointer
    5pointer
    7null pointer
    + +

    Implementing a Stack with a Linked List (By example)

    + +

    Code to run, followed by the current stack.

    + +

    stack s;

    + +

    +node

    + +

    s.push(3)

    + +

    front

    + + + + + + + + + + + + + + +
    DataPointer
    3null pointer
    + +

    etc…

    + +

    + +

    s.push(5)

    + +

    front

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    DataPointer
    5pointer
    8pointer
    … (pointer)
    3null pointer
    + +

    s.push(7)

    + +

    front

    + + + + + + + + + + + + + + + + + + + + + + + + +
    ValuePointer
    7pointer
    5pointer
    8pointer
    + +

    To perform the push(7):

    + +
      +
    1. Make a new node to store the 7
    2. +
    3. modify links to insert it correctly
    4. +
    + +

    Original:

    + +

    front

    + + + + + + + + + + + + + + + + + + +
    DataPointer
    5pointer
    8
    + +

    After operation:

    + +

    front now points at node with value 7; node with value 7’s pointer now points to the old front

    + + + + + + + + + + + + + + + + + + + + + + +
    ValuePointer
    7pointer
    5pointer
    8
    + +

    s.pop()

    + +

    front

    + + + + + + + + + + + + + + + + + + +
    ValuePointer
    5pointer
    8
    + +

    How to perform the pop():

    +
      +
    1. Change the “top” link
    2. +
    3. Return the old top node
    4. +
    + +

    Linked list after running operation (note that two links go to the same node):

    + +

    top now points to second row

    + + + + + + + + + + + + + + + + + + + + + + +
    ValuePointer
    7 (return this value)pointer
    5pointer
    8
    + +
      +
    • Caveat 1: don’t lose the old top value
    • +
    • Caveat 2: don’t ignore the old top node (it still consumes space)
    • +
    + +

    “Improved” pop():

    + +
      +
    1. Store the old top value in “temp”
    2. +
    3. make top link to the new top node
    4. +
    5. free the space for the old top node
    6. +
    7. return temp
    8. +
    + +

    The List Class (A doubly-linked list implementation of a list)

    + +

    See list.cpp

    + +

    <…5,6,7,…> in a double linked list:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    PrevValueNext
    5next
    prev6next
    prev7
    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/melodys-transcriptions/cmpt225-05/225 L5 - Dynamic DS - LL.pdf b/_site/melody/melodys-transcriptions/cmpt225-05/225 L5 - Dynamic DS - LL.pdf new file mode 100644 index 0000000..5f40097 Binary files /dev/null and b/_site/melody/melodys-transcriptions/cmpt225-05/225 L5 - Dynamic DS - LL.pdf differ diff --git a/_site/melody/melodys-transcriptions/cmpt225-05/225 L5 - Dynamic DS - LL/index.html b/_site/melody/melodys-transcriptions/cmpt225-05/225 L5 - Dynamic DS - LL/index.html new file mode 100644 index 0000000..9d3919d --- /dev/null +++ b/_site/melody/melodys-transcriptions/cmpt225-05/225 L5 - Dynamic DS - LL/index.html @@ -0,0 +1,366 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    Dynamic Data Structures - Linked Lists

    + +

    Transcriber’s 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”, it’s not just a pointer to the value.

    + +

    Dynamic Data Structures

    + +
      +
    • Basic types (e.g. int, char, bool, …), and arrays of these store a fixed amount of data.
    • +
    • We want implementations of ADTs like stacks + queues to grow & shrink (their memory use) as needed. +
        +
      • e.g. like Vector, Array-something? (can’t read), String classes
      • +
      +
    • +
    + +

    Basic Idea:

    + +

    Store data in a collection of (simple) objects. +add/delete these as needed; link them all together to make the main object.

    + +

    Linked Lists

    + +
      +
    • A sequence of simple objects (nodes): each storing one datum (plus a link…) linked together in a chain
    • +
    • E.g., to store the list <3, 5, 7>:
    • +
    + + + + + + + + + + + + + + + + + + + + + + +
    DatumLink
    3link
    5link
    7link to nowhere
    + +
      +
    • These objects have no names, (in contrast to declared values) +
        +
      • we access them by following links +
          +
        • in Java, references (implemented as pointers)
        • +
        • in C++, pointers
        • +
        +
      • +
      +
    • +
    + +

    Need one named place to start like so:

    + +

    A normal variable of type “pointer to a node”: First

    + + + + + + + + + + + + + + + + + + + + + + +
    DataLink
    3pointer
    5pointer
    7null pointer
    + +

    Implementing a Stack with a Linked List (By example)

    + +

    Code to run, followed by the current stack.

    + +

    stack s;

    + +

    +node

    + +

    s.push(3)

    + +

    front

    + + + + + + + + + + + + + + +
    DataPointer
    3null pointer
    + +

    etc…

    + +

    + +

    s.push(5)

    + +

    front

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    DataPointer
    5pointer
    8pointer
    … (pointer)
    3null pointer
    + +

    s.push(7)

    + +

    front

    + + + + + + + + + + + + + + + + + + + + + + + + +
    ValuePointer
    7pointer
    5pointer
    8pointer
    + +

    To perform the push(7):

    + +
      +
    1. Make a new node to store the 7
    2. +
    3. modify links to insert it correctly
    4. +
    + +

    Original:

    + +

    front

    + + + + + + + + + + + + + + + + + + +
    DataPointer
    5pointer
    8
    + +

    After operation:

    + +

    front now points at node with value 7; node with value 7’s pointer now points to the old front

    + + + + + + + + + + + + + + + + + + + + + + +
    ValuePointer
    7pointer
    5pointer
    8
    + +

    s.pop()

    + +

    front

    + + + + + + + + + + + + + + + + + + +
    ValuePointer
    5pointer
    8
    + +

    How to perform the pop():

    +
      +
    1. Change the “top” link
    2. +
    3. Return the old top node
    4. +
    + +

    Linked list after running operation (note that two links go to the same node):

    + +

    top now points to second row

    + + + + + + + + + + + + + + + + + + + + + + +
    ValuePointer
    7 (return this value)pointer
    5pointer
    8
    + +
      +
    • Caveat 1: don’t lose the old top value
    • +
    • Caveat 2: don’t ignore the old top node (it still consumes space)
    • +
    + +

    “Improved” pop():

    + +
      +
    1. Store the old top value in “temp”
    2. +
    3. make top link to the new top node
    4. +
    5. free the space for the old top node
    6. +
    7. return temp
    8. +
    + +

    The List Class (A doubly-linked list implementation of a list)

    + +

    See list.cpp

    + +

    <…5,6,7,…> in a double linked list:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    PrevValueNext
    5next
    prev6next
    prev7
    + +

    End

    + +
    +
    +
    +
    + + diff --git a/_site/melody/melodys-transcriptions/cmpt225-05/list.cpp b/_site/melody/melodys-transcriptions/cmpt225-05/list.cpp new file mode 100644 index 0000000..c229e17 --- /dev/null +++ b/_site/melody/melodys-transcriptions/cmpt225-05/list.cpp @@ -0,0 +1,22 @@ +#include // not in original, needed for compilation + +template +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 diff --git a/_site/melody/melodys-transcriptions/cmpt295-05/Lecture_05_Data_Representation_Fractional_Numbers.html b/_site/melody/melodys-transcriptions/cmpt295-05/Lecture_05_Data_Representation_Fractional_Numbers.html new file mode 100644 index 0000000..f02fbbe --- /dev/null +++ b/_site/melody/melodys-transcriptions/cmpt295-05/Lecture_05_Data_Representation_Fractional_Numbers.html @@ -0,0 +1,766 @@ + + + + + | tait.tech + + + + + +
    +
    +

    CMPT 295: Unit - Data Representation

    + +

    Lecture 5

    + +
      +
    • Representing fractional numbers in memory
    • +
    • IEEE floating point representation
    • +
    + +

    Last Lecture

    +
      +
    • Demo of size and sign conversion in C: code and results posted!
    • +
    • Addition: +
        +
      • Unsigned/signed: +
          +
        • Behave the same way at the bit level
        • +
        • Interpretation of resulting bit vector (sum) may differ
        • +
        +
      • +
      • Unsigned addition -> true sum may overflow its w bits in memory (annotation with diagram of unisnged overflows, see lecture 04 to see full description of diagram) +
          +
        • If so, then actual sum = (x+y)mod2w(x + y) \mod 2^{w} (equivalent to subtracting 2w2^{w} from true sum (x+y)(x + y))
        • +
        +
      • +
      • Signed addition -> true sum may overflow its w bits in memory (annotation attached displaying diagram of negative and positive overflows, see Lecture 04 for detailed description of diagram) +
          +
        • If so then … +
            +
          • actual sum = U2Tw[(x+y)mod2w]\text{U2T}_{w}[(x + y) \mod 2^{w}]
          • +
          • true sum may be too +ve -> positive overflow OR too –ve -> negative overflow
          • +
          +
        • +
        +
      • +
      +
    • +
    • Subtraction +
        +
      • Becomes an addition where the 2nd operand is transformed into its additive inverse in two’s complement
      • +
      +
    • +
    • Multiplication: +
        +
      • Unsigned: actual product = (x×y)mod2w(x \times y) \mod 2^{w}
      • +
      • Signed: actual product = U2Tw[(x×y)mod2w]\text{U2T}_{w}[(x \times y) \mod 2^{w}]
      • +
      • Can be replaced by additions and shifts
      • +
      +
    • +
    + +

    Conclusion: the same bit pattern is interpreted differently.

    + +

    Questions

    + +
      +
    • Why are we learning this?
    • +
    • What can we do in our program when we suspect that overflow may occur?
    • +
    + +

    Demo – Looking at integer additions in C

    +
      +
    • What does the demo illustrate? +
        +
      • Unsigned addition +
          +
        • Without overflow
        • +
        • With overflow
        • +
        • Can overflow be predicted?
        • +
        +
      • +
      • Signed addition +
          +
        • Without overflow
        • +
        • With positive overflow and negative overflow
        • +
        • Can overflow be predicted?
        • +
        +
      • +
      +
    • +
    • This demo (code and results) posted on our course web site +4
    • +
    + +

    Today’s Menu

    +
      +
    • (greyed out) Representing data in memory – Most of this is review +
        +
      • (greyed out) “Under the Hood” - Von Neumann architecture
      • +
      • (greyed out) Bits and bytes in memory +
          +
        • (greyed out) How to diagram memory -> Used in this course and other references
        • +
        • (greyed out) How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • (greyed out) What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • (greyed out) Order of bytes in memory -> Endian
        • +
        +
      • +
      • (greyed out) Bit manipulation – bitwise operations +
          +
        • (greyed out) Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • (greyed out) Representing integral numbers in memory +
        +
      • (greyed out) Unsigned and signed
      • +
      • (greyed out) Converting, expanding and truncating
      • +
      • (greyed out) Arithmetic operations
      • +
      +
    • +
    • (highlighted) Representing real numbers in memory
    • +
    • (highlighted) IEEE floating point representation
    • +
    • (greyed out) Floating point in C – casting, rounding, addition, …
    • +
    + +

    We’ll illustrate what we covered today by having a demo!

    + +

    Converting a fractional decimal number into a binary number (bit vector) [R2B(X)]

    + +
      +
    • How would 346.625 (34658346 \frac{5}{8}) be represented as a binary number?
    • +
    • Expanding the subtraction method we have already seen:
    • +
    + +

    Starting number: 346.625

    + +

    Whole:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueAttempted Addition (subtraction) valueResultBinary ImplicationNote
    346-256901×281 \times 2^{8}MSb
    90-1280×270 \times 2^{7} 
    90-64261×261 \times 2^{6} 
    26-320×250 \times 2^{5} 
    26-16101×241 \times 2^{4} 
    10-821×231 \times 2^{3} 
    2-40×220 \times 2^{2} 
    2-201×211 \times 2^{1} 
    0-10×210 \times 2^{1}LSb
    + +

    Fractional:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueAttempted addition (subtraction) valueresultbinary implicationnotes
    .625-0.5.1251×211 \times 2^{-1}MSb
    .125-0.250×220 \times 2^{-2} 
    .125-0.12501×231 \times 2^{-3}LSb
    + +

    Binary representation is: 101011010.1012\text{101011010.101}_{2}

    + +

    First, last binary digit before the period are MSb, LSb respectively. +First, last binary digit after the period are also MSb, LSb respectively.

    + +

    Negative Powers of 2:

    + +
      +
    • 212^{-1} = 0.5
    • +
    • 222^{−2} = 0.25
    • +
    • 232^{−3} = 0.125
    • +
    • 242^{−4} = 0.0625
    • +
    • 252^{−5} = 0.03125
    • +
    + +

    Converting a binary number into a fractional decimal number [R2B(X)]

    + +
      +
    • How would 1011.1012\text{1011.101}_{2} be represented as a fractional decimal number?
    • +
    + +

    Review: Fractional decimal numbers

    + +

    Positional notation:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NotationValue
    did_{i}10i\text{10}^{i}
    di1d_{i-1}10i1\text{10}^{i-1}
    d2d_{2}100
    d1d_{1}10
    d0d_{0}1
    d1d_{-1}110\frac{1}{10}
    d2d_{-2}1100\frac{1}{100}
    d3d_{-3}11000\frac{1}{1000}
    djd_{-j}10j\text{10}^{-j}
    + +

    Example: 2.345

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Digit in numberNote
    2100\text{10}^{0}
    . 
    3101\text{10}^{-1}
    4102\text{10}^{-2}
    5103\text{10}^{-3}
    + +

    2.345=2×100+3×101+4×102+5×103 +2.345 = 2 \times \text{10}^{0} + 3 \times \text{10}^{−1} + 4 \times \text{10}^{−2} + 5 \times \text{10}^{−3} +

    + +

    Converting a binary number into a fractional decimal number [B2R(X)]

    + +

    Positional notation: can this be a possible encoding scheme?

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    bib_{i}2i2^{i}
    bi1b_{i-1}2i12^{i-1}
    b2b_{2}4
    b1b_{1}2
    b0b_{0}1
    b1b_{-1}12\frac{1}{2}
    b2b_{-2}14\frac{1}{4}
    b3b_{-3}18\frac{1}{8}
    bjb_{-j}2j2^{-j}
    + +

    Converting a binary number into a fractional decimal number [B2R(X)]

    + +
      +
    • How would 1011.1012\text{1011.101}_{2} be represented as a fractional decimal number?
    • +
    • Using the positional encoding scheme:
    • +
    + +

    1011.1012=(10112=1×23+1×21+1×20=1110)+(.1012=1×21+1×23=0.5+0.125=0.62510) +\begin{aligned} +&\text{1011.101}_{2} = \\ +&(\text{1011}_{2} = 1 \times 2^{3} + 1 \times 2^{1} + 1 \times 2^{0} = \text{11}_{10}) +\\ +&(\text{.101}_{2} = 1 \times 2^{-1} + 1 \times 2^{-3} = 0.5 + 0.125 = \text{0.625}_{10}) +\end{aligned} +

    + +

    Result: ____

    + +

    Negative Powers of 2

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    212^{−1}0.5
    222^{−2}0.25
    232^{−3}0.125
    242^{−4}0.0625
    252^{−5}0.03125
    262^{−6}0.015625
    272^{−7}0.0078125
    282^{−8}0.00390625
    + +

    Positional notation as encoding scheme?

    + +
      +
    • One way to answer this question is to investigate whether the encoding scheme allows for arithmetic operations
    • +
    • Let’s see: Using the positional notation as an encoding scheme produces fractional binary numbers that can be +
        +
      • added
      • +
      • multiplied by 2 by shifting left
      • +
      • divided by 2 by shifting right (unsigned)
      • +
      +
    • +
    + +

    Example:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OperandBinaryFractionMakeup
     1011.1012\text{1011.101}_{2}115811\frac{5}{8}8+2+1+12+188 + 2 + 1 + \frac{1}{2} + \frac{1}{8}
    Divide by 2101.11012\text{101.1101}_{2}513165\frac{13}{16}4+1+12+14+1164 + 1 + \frac{1}{2} + \frac{1}{4} + \frac{1}{16}
    Divide by 210.111012\text{10.11101}_{2}229322\frac{29}{32}2+12+14+18+1322 + \frac{1}{2} + \frac{1}{4} + \frac{1}{8} + \frac{1}{32}
    Multiply by 41011.1012\text{1011.101}_{2}115811\frac{5}{8}8+2+1+12+188 + 2 + 1 + \frac{1}{2} + \frac{1}{8}
    Multiply by 210111.012\text{10111.01}_{2}231423\frac{1}{4}16+4+2+1+1416 + 4 + 2 + 1 + \frac{1}{4}
    + +

    So far so good!

    + +

    Positional notation as encoding scheme?

    + +
      +
    • Advantage (so far): +
        +
      • Straightforward arithmetic: can shift to multiply and divide, convert
      • +
      +
    • +
    • Disadvantage: +
        +
      • Cannot encode all fractional numbers: +
          +
        • Can only represent numbers of the form x÷2kx \div 2^{k} (what about 15\frac{1}{5} or -34.8)
        • +
        +
      • +
      • Only one setting of binary point within the w bits -> this limits the range of possible values +
          +
        • What is this range? +
            +
          • Example: w = 32 bits and binary point located at 16th bit: +
              +
            • Whole number range: [0 … 65535]
            • +
            • Fraction range: [0 … 1 - ε]
            • +
            • + 1bits=ϵ1 - \text{bits} = \epsilon +
            • +
            • Range: [0.0 … 65535.99998]
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    Not so good anymore!

    + +

    Representing fractional numbers in memory

    + +
      +
    • Here is another possible encoding scheme: IEEE floating point representation (IEEE Standard 754)
    • +
    • Overview: +
        +
      • Binary Numerical Form: V=(–1)sM2EV = \text{(–1)}^{s} M 2^{E} +
          +
        • s – Sign bit -> determines whether number is negative or positive
        • +
        • M – Significand (or Mantissa) -> fractional part of number
        • +
        • E – Exponent
        • +
        +
      • +
      +
    • +
    • Form of bit pattern (number of items not important, focus on scale): [s|eee|fffffffffffff] +
        +
      • Most significant bit (MSb) s (similar to sign-magnitude encoding)
      • +
      • exp (e) field encodes E (but is not equal to E)
      • +
      • frac (f) field encodes M (but is not equal to M)
      • +
      +
    • +
    + +

    IEEE Floating Point Representation – Precision options

    + +
      +
    • Single precision: 32 bits ≈ 7 decimal digits, range:10±38 (in C: float) +
        +
      • In C: diagram of memory showing: +
          +
        • 1 sign bit
        • +
        • 8 exp bits
        • +
        • 23 frac bits
        • +
        +
      • +
      +
    • +
    • Double precision: 64 bits ≈ 16 decimal digits, range:10±308 (in C: double) +
        +
      • In C: diagram of memory showing: +
          +
        • 1 sign bit
        • +
        • 11 exp bits
        • +
        • 52 frac bits
        • +
        +
      • +
      +
    • +
    + +

    IEEE Floating Point Representation – Three “kinds” of values

    + +

    Numerical Form: V=(–1)sM2EV = \text{(–1)}^{s} M 2^{E}

    + +
      +
    • 1 sign bit
    • +
    • k exp bits +
        +
      • denormalized: 00…00 (all 0’s)
      • +
      • normalized: exp != 0 and exp != 11..11
      • +
      • special cases: 11…11 (all 1’s)
      • +
      +
    • +
    • +

      n frac bits

      +
    • +
    • E = exp - bias, bias = 2k112^{k-1} -1
    • +
    • M = 1 + frac
    • +
    + +

    Why is E biased? Using single precision as an example:

    + +
      +
    • exp range: [00000001 .. 11111110] and bias = 28112^{8-1} – 1
    • +
    • E range: [-126 .. 127]
    • +
    • If no bias: E range: [1 .. 254] => 212^{1} to 22542^{254}
    • +
    + +

    Why adding 1 to frac? Because number V is first normalized before it is converted.

    + +

    Review: Scientific Notation and normalization

    + +
      +
    • From Wikipedia: +
        +
      • Scientific notation is a way of expressing numbers that are too large or too small (usually would result a long string of digits) to be conveniently written in decimal form.
      • +
      • In scientific notation, nonzero numbers are written in the form
      • +
      • In normalized notation, the exponent n is chosen so that the absolute value of the significand m is at least 1 but less than 10.
      • +
      +
    • +
    • Examples: +
        +
      • A proton’s mass is 0.0000000000000000000000000016726 kg -> 1.6726×10271.6726 \times \text{10}^{−27}kg
      • +
      • Speed of light is 299,792,458 m/s -> 2.99792,458×1082.99792,458 \times \text{10}^{8} m/s
      • +
      +
    • +
    + +

    Syntax:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NotationName
    +/−sign
    d0d_{0}, d1d_{-1}, d2d_{-2}, d3d_{-3}dnd_{-n}significand
    ×times
    bbase
    exp^{exp}exponent
    + +

    Let’s try: 101011010.1012\text{101011010.101}_{2} = ____

    + +

    Summary

    + +
      +
    • Representing integral numbers (signed/unsigned) in memory: +
        +
      • Encode schemes allow for small range of values exactly
      • +
      +
    • +
    • Representing fractional numbers in memory: +
        +
      1. Positional notation (advantages and disadvantages)
      2. +
      3. IEEE floating point representation: wider range, mostly approximately
      4. +
      +
    • +
    • Overview of IEEE Floating Point representation
    • +
    • + V=(-1)s×M×2EV = \text{(-1)}^{s} \times M \times 2^{E} +
    • +
    • Precision options
    • +
    • 3 kinds: normalized, denormalized and special values
    • +
    + +

    Today’s Menu

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +18 +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/melody/melodys-transcriptions/cmpt295-05/Lecture_05_Data_Representation_Fractional_Numbers.pdf b/_site/melody/melodys-transcriptions/cmpt295-05/Lecture_05_Data_Representation_Fractional_Numbers.pdf new file mode 100644 index 0000000..44cf646 Binary files /dev/null and b/_site/melody/melodys-transcriptions/cmpt295-05/Lecture_05_Data_Representation_Fractional_Numbers.pdf differ diff --git a/_site/melody/melodys-transcriptions/cmpt295-05/Lecture_05_Data_Representation_Fractional_Numbers/index.html b/_site/melody/melodys-transcriptions/cmpt295-05/Lecture_05_Data_Representation_Fractional_Numbers/index.html new file mode 100644 index 0000000..bc353e0 --- /dev/null +++ b/_site/melody/melodys-transcriptions/cmpt295-05/Lecture_05_Data_Representation_Fractional_Numbers/index.html @@ -0,0 +1,768 @@ + + + + + | tait.tech + + + + + + + +
    +
    +

    CMPT 295: Unit - Data Representation

    + +

    Lecture 5

    + +
      +
    • Representing fractional numbers in memory
    • +
    • IEEE floating point representation
    • +
    + +

    Last Lecture

    +
      +
    • Demo of size and sign conversion in C: code and results posted!
    • +
    • Addition: +
        +
      • Unsigned/signed: +
          +
        • Behave the same way at the bit level
        • +
        • Interpretation of resulting bit vector (sum) may differ
        • +
        +
      • +
      • Unsigned addition -> true sum may overflow its w bits in memory (annotation with diagram of unisnged overflows, see lecture 04 to see full description of diagram) +
          +
        • If so, then actual sum = (x+y)mod2w(x + y) \mod 2^{w} (equivalent to subtracting 2w2^{w} from true sum (x+y)(x + y))
        • +
        +
      • +
      • Signed addition -> true sum may overflow its w bits in memory (annotation attached displaying diagram of negative and positive overflows, see Lecture 04 for detailed description of diagram) +
          +
        • If so then … +
            +
          • actual sum = U2Tw[(x+y)mod2w]\text{U2T}_{w}[(x + y) \mod 2^{w}]
          • +
          • true sum may be too +ve -> positive overflow OR too –ve -> negative overflow
          • +
          +
        • +
        +
      • +
      +
    • +
    • Subtraction +
        +
      • Becomes an addition where the 2nd operand is transformed into its additive inverse in two’s complement
      • +
      +
    • +
    • Multiplication: +
        +
      • Unsigned: actual product = (x×y)mod2w(x \times y) \mod 2^{w}
      • +
      • Signed: actual product = U2Tw[(x×y)mod2w]\text{U2T}_{w}[(x \times y) \mod 2^{w}]
      • +
      • Can be replaced by additions and shifts
      • +
      +
    • +
    + +

    Conclusion: the same bit pattern is interpreted differently.

    + +

    Questions

    + +
      +
    • Why are we learning this?
    • +
    • What can we do in our program when we suspect that overflow may occur?
    • +
    + +

    Demo – Looking at integer additions in C

    +
      +
    • What does the demo illustrate? +
        +
      • Unsigned addition +
          +
        • Without overflow
        • +
        • With overflow
        • +
        • Can overflow be predicted?
        • +
        +
      • +
      • Signed addition +
          +
        • Without overflow
        • +
        • With positive overflow and negative overflow
        • +
        • Can overflow be predicted?
        • +
        +
      • +
      +
    • +
    • This demo (code and results) posted on our course web site +4
    • +
    + +

    Today’s Menu

    +
      +
    • (greyed out) Representing data in memory – Most of this is review +
        +
      • (greyed out) “Under the Hood” - Von Neumann architecture
      • +
      • (greyed out) Bits and bytes in memory +
          +
        • (greyed out) How to diagram memory -> Used in this course and other references
        • +
        • (greyed out) How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • (greyed out) What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • (greyed out) Order of bytes in memory -> Endian
        • +
        +
      • +
      • (greyed out) Bit manipulation – bitwise operations +
          +
        • (greyed out) Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • (greyed out) Representing integral numbers in memory +
        +
      • (greyed out) Unsigned and signed
      • +
      • (greyed out) Converting, expanding and truncating
      • +
      • (greyed out) Arithmetic operations
      • +
      +
    • +
    • (highlighted) Representing real numbers in memory
    • +
    • (highlighted) IEEE floating point representation
    • +
    • (greyed out) Floating point in C – casting, rounding, addition, …
    • +
    + +

    We’ll illustrate what we covered today by having a demo!

    + +

    Converting a fractional decimal number into a binary number (bit vector) [R2B(X)]

    + +
      +
    • How would 346.625 (34658346 \frac{5}{8}) be represented as a binary number?
    • +
    • Expanding the subtraction method we have already seen:
    • +
    + +

    Starting number: 346.625

    + +

    Whole:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueAttempted Addition (subtraction) valueResultBinary ImplicationNote
    346-256901×281 \times 2^{8}MSb
    90-1280×270 \times 2^{7} 
    90-64261×261 \times 2^{6} 
    26-320×250 \times 2^{5} 
    26-16101×241 \times 2^{4} 
    10-821×231 \times 2^{3} 
    2-40×220 \times 2^{2} 
    2-201×211 \times 2^{1} 
    0-10×210 \times 2^{1}LSb
    + +

    Fractional:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ValueAttempted addition (subtraction) valueresultbinary implicationnotes
    .625-0.5.1251×211 \times 2^{-1}MSb
    .125-0.250×220 \times 2^{-2} 
    .125-0.12501×231 \times 2^{-3}LSb
    + +

    Binary representation is: 101011010.1012\text{101011010.101}_{2}

    + +

    First, last binary digit before the period are MSb, LSb respectively. +First, last binary digit after the period are also MSb, LSb respectively.

    + +

    Negative Powers of 2:

    + +
      +
    • 212^{-1} = 0.5
    • +
    • 222^{−2} = 0.25
    • +
    • 232^{−3} = 0.125
    • +
    • 242^{−4} = 0.0625
    • +
    • 252^{−5} = 0.03125
    • +
    + +

    Converting a binary number into a fractional decimal number [R2B(X)]

    + +
      +
    • How would 1011.1012\text{1011.101}_{2} be represented as a fractional decimal number?
    • +
    + +

    Review: Fractional decimal numbers

    + +

    Positional notation:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NotationValue
    did_{i}10i\text{10}^{i}
    di1d_{i-1}10i1\text{10}^{i-1}
    d2d_{2}100
    d1d_{1}10
    d0d_{0}1
    d1d_{-1}110\frac{1}{10}
    d2d_{-2}1100\frac{1}{100}
    d3d_{-3}11000\frac{1}{1000}
    djd_{-j}10j\text{10}^{-j}
    + +

    Example: 2.345

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Digit in numberNote
    2100\text{10}^{0}
    . 
    3101\text{10}^{-1}
    4102\text{10}^{-2}
    5103\text{10}^{-3}
    + +

    2.345=2×100+3×101+4×102+5×103 +2.345 = 2 \times \text{10}^{0} + 3 \times \text{10}^{−1} + 4 \times \text{10}^{−2} + 5 \times \text{10}^{−3} +

    + +

    Converting a binary number into a fractional decimal number [B2R(X)]

    + +

    Positional notation: can this be a possible encoding scheme?

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    bib_{i}2i2^{i}
    bi1b_{i-1}2i12^{i-1}
    b2b_{2}4
    b1b_{1}2
    b0b_{0}1
    b1b_{-1}12\frac{1}{2}
    b2b_{-2}14\frac{1}{4}
    b3b_{-3}18\frac{1}{8}
    bjb_{-j}2j2^{-j}
    + +

    Converting a binary number into a fractional decimal number [B2R(X)]

    + +
      +
    • How would 1011.1012\text{1011.101}_{2} be represented as a fractional decimal number?
    • +
    • Using the positional encoding scheme:
    • +
    + +

    1011.1012=(10112=1×23+1×21+1×20=1110)+(.1012=1×21+1×23=0.5+0.125=0.62510) +\begin{aligned} +&\text{1011.101}_{2} = \\ +&(\text{1011}_{2} = 1 \times 2^{3} + 1 \times 2^{1} + 1 \times 2^{0} = \text{11}_{10}) +\\ +&(\text{.101}_{2} = 1 \times 2^{-1} + 1 \times 2^{-3} = 0.5 + 0.125 = \text{0.625}_{10}) +\end{aligned} +

    + +

    Result: ____

    + +

    Negative Powers of 2

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    212^{−1}0.5
    222^{−2}0.25
    232^{−3}0.125
    242^{−4}0.0625
    252^{−5}0.03125
    262^{−6}0.015625
    272^{−7}0.0078125
    282^{−8}0.00390625
    + +

    Positional notation as encoding scheme?

    + +
      +
    • One way to answer this question is to investigate whether the encoding scheme allows for arithmetic operations
    • +
    • Let’s see: Using the positional notation as an encoding scheme produces fractional binary numbers that can be +
        +
      • added
      • +
      • multiplied by 2 by shifting left
      • +
      • divided by 2 by shifting right (unsigned)
      • +
      +
    • +
    + +

    Example:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    OperandBinaryFractionMakeup
     1011.1012\text{1011.101}_{2}115811\frac{5}{8}8+2+1+12+188 + 2 + 1 + \frac{1}{2} + \frac{1}{8}
    Divide by 2101.11012\text{101.1101}_{2}513165\frac{13}{16}4+1+12+14+1164 + 1 + \frac{1}{2} + \frac{1}{4} + \frac{1}{16}
    Divide by 210.111012\text{10.11101}_{2}229322\frac{29}{32}2+12+14+18+1322 + \frac{1}{2} + \frac{1}{4} + \frac{1}{8} + \frac{1}{32}
    Multiply by 41011.1012\text{1011.101}_{2}115811\frac{5}{8}8+2+1+12+188 + 2 + 1 + \frac{1}{2} + \frac{1}{8}
    Multiply by 210111.012\text{10111.01}_{2}231423\frac{1}{4}16+4+2+1+1416 + 4 + 2 + 1 + \frac{1}{4}
    + +

    So far so good!

    + +

    Positional notation as encoding scheme?

    + +
      +
    • Advantage (so far): +
        +
      • Straightforward arithmetic: can shift to multiply and divide, convert
      • +
      +
    • +
    • Disadvantage: +
        +
      • Cannot encode all fractional numbers: +
          +
        • Can only represent numbers of the form x÷2kx \div 2^{k} (what about 15\frac{1}{5} or -34.8)
        • +
        +
      • +
      • Only one setting of binary point within the w bits -> this limits the range of possible values +
          +
        • What is this range? +
            +
          • Example: w = 32 bits and binary point located at 16th bit: +
              +
            • Whole number range: [0 … 65535]
            • +
            • Fraction range: [0 … 1 - ε]
            • +
            • + 1bits=ϵ1 - \text{bits} = \epsilon +
            • +
            • Range: [0.0 … 65535.99998]
            • +
            +
          • +
          +
        • +
        +
      • +
      +
    • +
    + +

    Not so good anymore!

    + +

    Representing fractional numbers in memory

    + +
      +
    • Here is another possible encoding scheme: IEEE floating point representation (IEEE Standard 754)
    • +
    • Overview: +
        +
      • Binary Numerical Form: V=(–1)sM2EV = \text{(–1)}^{s} M 2^{E} +
          +
        • s – Sign bit -> determines whether number is negative or positive
        • +
        • M – Significand (or Mantissa) -> fractional part of number
        • +
        • E – Exponent
        • +
        +
      • +
      +
    • +
    • Form of bit pattern (number of items not important, focus on scale): [s|eee|fffffffffffff] +
        +
      • Most significant bit (MSb) s (similar to sign-magnitude encoding)
      • +
      • exp (e) field encodes E (but is not equal to E)
      • +
      • frac (f) field encodes M (but is not equal to M)
      • +
      +
    • +
    + +

    IEEE Floating Point Representation – Precision options

    + +
      +
    • Single precision: 32 bits ≈ 7 decimal digits, range:10±38 (in C: float) +
        +
      • In C: diagram of memory showing: +
          +
        • 1 sign bit
        • +
        • 8 exp bits
        • +
        • 23 frac bits
        • +
        +
      • +
      +
    • +
    • Double precision: 64 bits ≈ 16 decimal digits, range:10±308 (in C: double) +
        +
      • In C: diagram of memory showing: +
          +
        • 1 sign bit
        • +
        • 11 exp bits
        • +
        • 52 frac bits
        • +
        +
      • +
      +
    • +
    + +

    IEEE Floating Point Representation – Three “kinds” of values

    + +

    Numerical Form: V=(–1)sM2EV = \text{(–1)}^{s} M 2^{E}

    + +
      +
    • 1 sign bit
    • +
    • k exp bits +
        +
      • denormalized: 00…00 (all 0’s)
      • +
      • normalized: exp != 0 and exp != 11..11
      • +
      • special cases: 11…11 (all 1’s)
      • +
      +
    • +
    • +

      n frac bits

      +
    • +
    • E = exp - bias, bias = 2k112^{k-1} -1
    • +
    • M = 1 + frac
    • +
    + +

    Why is E biased? Using single precision as an example:

    + +
      +
    • exp range: [00000001 .. 11111110] and bias = 28112^{8-1} – 1
    • +
    • E range: [-126 .. 127]
    • +
    • If no bias: E range: [1 .. 254] => 212^{1} to 22542^{254}
    • +
    + +

    Why adding 1 to frac? Because number V is first normalized before it is converted.

    + +

    Review: Scientific Notation and normalization

    + +
      +
    • From Wikipedia: +
        +
      • Scientific notation is a way of expressing numbers that are too large or too small (usually would result a long string of digits) to be conveniently written in decimal form.
      • +
      • In scientific notation, nonzero numbers are written in the form
      • +
      • In normalized notation, the exponent n is chosen so that the absolute value of the significand m is at least 1 but less than 10.
      • +
      +
    • +
    • Examples: +
        +
      • A proton’s mass is 0.0000000000000000000000000016726 kg -> 1.6726×10271.6726 \times \text{10}^{−27}kg
      • +
      • Speed of light is 299,792,458 m/s -> 2.99792,458×1082.99792,458 \times \text{10}^{8} m/s
      • +
      +
    • +
    + +

    Syntax:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NotationName
    +/−sign
    d0d_{0}, d1d_{-1}, d2d_{-2}, d3d_{-3}dnd_{-n}significand
    ×times
    bbase
    exp^{exp}exponent
    + +

    Let’s try: 101011010.1012\text{101011010.101}_{2} = ____

    + +

    Summary

    + +
      +
    • Representing integral numbers (signed/unsigned) in memory: +
        +
      • Encode schemes allow for small range of values exactly
      • +
      +
    • +
    • Representing fractional numbers in memory: +
        +
      1. Positional notation (advantages and disadvantages)
      2. +
      3. IEEE floating point representation: wider range, mostly approximately
      4. +
      +
    • +
    • Overview of IEEE Floating Point representation
    • +
    • + V=(-1)s×M×2EV = \text{(-1)}^{s} \times M \times 2^{E} +
    • +
    • Precision options
    • +
    • 3 kinds: normalized, denormalized and special values
    • +
    + +

    Today’s Menu

    +
      +
    • Representing data in memory – Most of this is review +
        +
      • “Under the Hood” - Von Neumann architecture
      • +
      • Bits and bytes in memory +
          +
        • How to diagram memory -> Used in this course and other references
        • +
        • How to represent series of bits -> In binary, in hexadecimal (conversion)
        • +
        • What kind of information (data) do series of bits represent -> Encoding scheme
        • +
        • Order of bytes in memory -> Endian
        • +
        +
      • +
      • Bit manipulation – bitwise operations +
          +
        • Boolean algebra + Shifting
        • +
        +
      • +
      +
    • +
    • Representing integral numbers in memory +
        +
      • Unsigned and signed
      • +
      • Converting, expanding and truncating
      • +
      • Arithmetic operations
      • +
      +
    • +
    • Representing real numbers in memory +18 +
        +
      • IEEE floating point representation
      • +
      • Floating point in C – casting, rounding, addition, …
      • +
      +
    • +
    + +
    +
    +
    +
    + + diff --git a/_site/public-key.asc b/_site/public-key.asc new file mode 100644 index 0000000..c9d83ac --- /dev/null +++ b/_site/public-key.asc @@ -0,0 +1,51 @@ +-----BEGIN PGP PUBLIC KEY BLOCK----- + +mQINBF6lmfMBEAC4KE5c8bQapokj8m5YAd+QFtsA35gjCBPMyzgTdz3yj8Imw7X0 +LfNvv5LVuh4ZhQU2Aln9GUPNzjRzfTL5pM3Dk+FI2YJqeyJZqaqCx5GxF1RyKG2y +SzS4pU8A0WEt3mGu4EdKg8ZH1mzdB9RdN8NMglbmUO3PI9lQMk9sSeJfs15y1G8F +RUgS7Ku3fBNPoNbkFt0gadn4Ab9ERa9mlY7FkJcjAKNFL0V8MJ1+MecRgNTrOMnp +LoreiLKW9vPLSAqxZrtUGmVXjn53RLaCPKcv6VLRl7SBNxic9VpPvtTAIJ+LdZvZ +3IHLShlLRp08SScQUSjp0iglswgJJ1/hWhh+hCv+00RHrNvfqX1oin4hdcL69mOx +oLVxA4n6mP+PahVyDdCxgnAQ5tnrZ3ovJa6Z8twW24Dc5/gh54ffB8+MNAg2vbMK +RMkQaZCRN+QcN0W86ZWlnE7LmyOSO9vRhsW9aoAmV11OLg6Bc7wiI2rkXQ0vBNK1 +Yx4OytZ1gVe/WhtNe5nDc7inKbmyp3rrAoIMvfALOUKM1Qh5p+R36YaVh+PNyip0 +l4y8AxFWy9OKYrAQRbvcQbgemHy7jUXhIxcdNTdP3dJsljPI5qrNRw6XopppNge0 +wcQ84rpzW0K5ZsvcHxSxg5nvSKGPPglxYoEIv2G0yX9g0vA3uB1IHlKU8wARAQAB +tBtUYWl0IEhveWVtIDx0YWl0QHRhaXQudGVjaD6JAk4EEwEIADgWIQTl82rniqd1 +3z1umQMTBN+dqU+PXAUCXqWZ8wIbAwULCQgHAgYVCgkICwIEFgIDAQIeAQIXgAAK +CRATBN+dqU+PXCrYD/9HNJpcZHNUS3oH9Ym1xTp1lgUdkcggUsBP4Pde2fOSqX+X +FIkHAqkb3AnM5fe0/JRzUkPDrQRJZBeilswo0+BJuC4cpIFSI/FLzgui8/uz8utc +G/O0lO2vOKOj5ZwWXwGfhiO7hsIWYy5ZCxZ/PRsymrQLMh76vqx4BfHa7LrjLert +AYlliklPh61V0A87uzSoHkqonmjz6UQaPEN9jZulF3Kx0joF6e5zHG4YMti23nt+ +zqMDiRVqhz4HgQFbyA8NLsq0ttWNR/UIz3rkqHrWDxk2+bzOLRfvcXbPjRG246Sc +pmwgkVC4UKMMK58PZUQogaK1suxwL356GGtIKL9cOfMS407PJDw5AD0LnUceCe0w +otjLTV9synm23peVxomCj+CWRlwHbDLC+rR6SNjWqVpF41+R1Hn56s06UK9wDeLb +xEVBYNkU/L/XCwq4Jt7GjVliuEtSTacTIS51cY6+WjIELAYq6E7N1iUDcYADAjO2 +1V/eHft8t4R2Zl8xZg4fJv4on/Ow5WITIMHt4yxnlvcq3QUjtgwxTC/HQJYPUMKH +ubdgmnuytMUFSrlzoVw7T1WzdzaYEypewIKt1SnmWE741eCborH+UGWkbNw1dT41 +pBHJx9HuYizT3E+n3JJEwg7ruIB01msOu2aFKb8SaCPeGg8SWD1NMYWMrRkNpLkC +DQRepZnzARAAzFE/nKk45pt/QbvWl2gE40U8gxshnLPcUKC0OighIhmEfx+4wibz +YDsw65m6NCILLc74yG4tZKZLUgNEJ41D9RHsZUR19bV24nJbFZh0br0MiNCnj1P8 +fUJ8lea/79KEdiowipXYA/AxBHQcUpQyBsicpzHDVbt80Ctd96InS0HQiOK/PstO +4RlObXWJa7+kiNKn2twGgYJEZLxypUYx0/wV3jxPbnvNuA1zmWrhIT7oTz+6DRKm +zSnZFP7I1OwpTocd/7BLumyy2Wi554ZUG9tTMz78p+Rt5Gtn+hbLhMmwjsKWSmY+ +wC+FQAgZO9U+C0Rodxy9Pu+TJiiKunAV5udfqvqOJK1BXOOiOUwFTBePYqgCcNdR +IkV9xc0+5GIVtnXrVhzZLpIxGHZ4st7T8nviqBcpusdrrvJssSXVkEakTeEgGiGq +lXOr8/EgxumT/huInnv6kxI1lHxyzis7rUaRrmLImHoS/ZtKJCegg7Q52Rx2N5uS +NaPctNxukk0OSiTtkvVY9HbmGER1ZffjoL+RlK9s8DgFQUh8bW8DpEsjGTkw/5Qb +v2UdkHLOPVan0Yb+7iomv56Ts90bDjxj4mwiWZPAfr5xYqR5LvAmSO6EygsrMW8F +9mEyyxxduiXNzapgh4lcINjoz7Bg4eGNBUB/KbnkGcayJHITU8/ak4sAEQEAAYkC +NgQYAQgAIBYhBOXzaueKp3XfPW6ZAxME352pT49cBQJepZnzAhsMAAoJEBME352p +T49crZoP/A5LaHbBxHm4KYJ1RHNFpoVF+mLjBVQS9CeqxBd+yE+4bXJM8UIx5A6/ +brJtaUxMrHYKTDzHqb+YeesAzgerU+sZ65hJBCSOvtnxmdY8DlP4IwHqkqad/Mgi +OqMFvgPjDrxaTMYYKzahujfNBRY7LNTT0SnE+zW1/nkKYjpZtaMF3+adc4gRxE6P +syf8aFFeDCpbtGnHe8Ws2Iq48HPkgjUc0apoTRimqm3c3Z3GKDIPbwm+ldS3dJTK +V/06vQSe+5A/0kLWExMDKCkoKrqrqxOVjA/q1eJo+umA1Ro/yEkp12B62yIwNuWs +u0WToHfinlrS3ooxf8YwUc9mywNYR8vpa21y2eqxeRWx4wtqQrVJ/vnQ0MjZKnEx +54VLJ+k8D9kiB+mL/ytWru7kG7JqRu7j2KiU35GwGSdi5bFaDGhjWJSTvV8mZ9Zs +XGitS00B6Rgq+OXcPQiqpJtPN2/5wX1ium9tqJFVvsjqcgCllZ0FUJaZmnl4MVqy +UZF2X34DtcoIUP3+hxd0mkFlkFx6clwIn+lxU/PT+D5VuyS50pKfH+jy/s3Mgzxb +elWWD2NCpg2qqsVdxDc617muUNF4t512UB+f5f3xQQim2wH9zpAHGNpkMVdghIC4 +fx25l0ddDmAupYtDIVpAj0+WNzPK1zuYHLl7nETmqn7fauQM+1wy +=wEUc +-----END PGP PUBLIC KEY BLOCK----- diff --git a/_site/resume/index.html b/_site/resume/index.html new file mode 100644 index 0000000..b442f31 --- /dev/null +++ b/_site/resume/index.html @@ -0,0 +1,207 @@ + + + + + Resume | tait.tech + + + + + + +
    +
    +

    Tait Hoyem

    + +

    Experience

    +
      + +
    • + Zone4 Systems Inc.Software Developer (June 2021-present) + +

      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..

      +
    • + +
    • + Bytetools Technologies Inc.Founder (2020-present) + +

      Providing small businesses with simple, secure software solutions.

      +
    • + +
    • + VOG App DevelopersContractor (Summer 2020) + +

      Identify seed health for machine learning algorithm bootstrapping.

      +
    • + +
    • + IndependentTutoring/Transcribing (2019-present) + +

      Working with computer science students explaining introductory to advanced concepts. Covering languages from C/C++ to Javascript to Python. I am skilled in working with students who have visual impairments: transcribing inaccessible computer code (from images or a screen-share) and presenting complex math equations in an accessible format.

      +
    • + +
    • + Total Impact SignsContractor (2014-2016) + +

      Implemented a semi-automatic payroll system replacing the manual sign in book used before.

      +
    • + +
    + +

    Education

    +
      + +
    • + Athabasca University — Degree / Bachelor of Science (2021-2023) + +

      Theoretical underpinnings to work towards my goal of a Master’s in cybersecurity.

      + +
    • + +
    • + Southern Alberta Institute of Technology — Diploma / Information Technology — Software Development Major (2019-2021) + +

      Full-stack two year software development diploma. Focusing on databases, interface design, systems administration, security and enterprise solutions.

      + +
    • + +
    • + Dartmouth College (Open Corseware) — Professional Certificate / C Programming with Linux (2020) + +

      A certification in C programming, the gcc compiler, and the make compilation system. 100% mark.

      + +
    • + +
    • + The Linux Foundation — Certification / Certified Linux System Administrator (2016) + +

      Certified in Linux system administration tasks. Running web servers, maintaining mass storage systems, running secure remote control tools. Certificate no longer valid.

      + +
    • + +
    + +

    Projects

    +
      + +
    • + Quote Retrieval System + +

      Written for a local roofing business to securely distribute quotes.

      +
    • + +
    • + Yggdrasil Screen Reader + +

      Some small pieces of code I've written have made it into a new screen reader for Linux, written in Rust.

      +
    • + +
    • + Lichess + +

      A patch for Lichess.org which vastly extends support for screen reader users. The website has millions of active players.

      +
    • + +
    • + epub-with-pinyin + +

      A program to add Pinyin above Chinese characters in .epub files to assist those learning Mandarin Chinese.

      +
    • + +
    • + Programming Tutorials For The Visually Impaired + +

      Amateur-level production quality videos with all file buffers, written text, and shell commands read out by Emacspeak, an Emacs extention for the blind.

      +
    • + +
    • + lamegames + +

      A little games website I made for some demonstrations of Django and websocket functionality. Very, very lame. Would not recommend.

      +
    • + +
    • + chess + +

      A command-line chess game in C++. It is compileable on nearly any system.

      +
    • + +
    • + Emacspeak C Tutorials + +

      C Tutorials for the Visually Impaired

      +
    • + +
    • + Simple Markdown Editor + +

      A simple online (and offline) browser-based markdown editor that supports the automatic creation of MathML.

      +
    • + +
    + +

    Skills

    + +

    Languages

    +
      + +
    • English (native)
    • + +
    • Mandarin (novice)
    • + +
    + +

    Programming Languages

    +
      + +
    • C/C++
    • + +
    • Python
    • + +
    • Java & Java EE
    • + +
    • HTML/CSS/JS (Frontend Web)
    • + +
    • Typescript, SCSS, Static Site Generation (Compiled Web)
    • + +
    • PHP
    • + +
    • POSIX-compliant Shell
    • + +
    • LaTeX, MathML
    • + +
    + +

    Frameworks/Environments

    +
      + +
    • Flask
    • + +
    • Django
    • + +
    • JQuery
    • + +
    • Jekyll
    • + +
    + +

    Miscellaneous/Hobbies

    +
      + +
    • 3D Printing
    • + +
    • Proxmox Virtualization
    • + +
    • Embedded Systems (RPi, ESP-compatible)
    • + +
    • Founder & Ex-President of SAIT's Free & Open-Source Software Club
    • + +
    + + +
    +
    +
    +
    + + diff --git a/_site/robots.txt b/_site/robots.txt new file mode 100644 index 0000000..e087884 --- /dev/null +++ b/_site/robots.txt @@ -0,0 +1 @@ +Sitemap: /sitemap.xml diff --git a/_site/scholarships/2020/cnib/index.html b/_site/scholarships/2020/cnib/index.html new file mode 100644 index 0000000..6b4c361 --- /dev/null +++ b/_site/scholarships/2020/cnib/index.html @@ -0,0 +1,76 @@ + + + + + CNIB Scholarship Essay 2020 | tait.tech + + + + + + +
    +
    +

    CNIB Scholarship Essay

    + +

    Intro

    +

    Question: +How will this scholarship change what it is to be blind for you as you pursue your post-secondary education?

    + +

    Answer: +I have taken every opportunity of my short life to deny being defined solely by any disability. +This essay will focus more on my educational goals and career aspirations on their own merit.

    + +

    I would love your support in accomplishing these goals!

    + +

    Goals

    + +

    My current educational path involves finishing a diploma program, +then transfering to a Bachelor’s of Technology program in cybersecurity and digital forensics at BCIT.

    + +

    Cybersecurity was not an overnight choice. +It became interesting to me over time as I saw ordinary people give up their privacy because they wanted their phone to tell them the weather—or some such other minor life enhancement.

    + +

    Of course, it is a exaggeration to say that this is the reason people give up privacy, +but consider the extreme case: +You have your phone location on at all times; +you own an Alexa which listens to everything you say at home; +you text and call others using a service that openly collects the contents of your messages.

    + +

    Would you want every single text you have sent to appear on the front page of the newspaper? +What about every thing you have said in the privacy of your own home? +Do you want just anyone to know where you were today?

    + +

    If any one person had access to all this information about you, it would likely make you uneasy. +I know it makes me a bit nervous at least.

    + +

    One of the primary goals of my career is to build tools that empower the individual. +Tools that make the individual more independent, not less. +These tools must be for the average consumer. +They must appeal to a mass market without compromising the secure, private foundations. +This is my goal: +Allow normal people, non-techies and techies alike, to just be able to send a text message without the world watching over their shoulder.

    + +

    The second goal I have is to make this technology accessible to everyone. +Security-focused apps need to be fast so they will run on less expensive phones. +They need to be simple so that screen readers can process them correctly.

    + +

    These are not hard things to do. +In fact, I would argue that designing software this way: independent of third parties, simple, able to run on cheap devices—is actually easier than designing it the way large companies seem to design software.

    + +

    Conclusion

    + +

    I have a vision for how to build open, secure, private and accessible applications. +Your help in moving that goal forward would be much appreciated.

    + +

    Without you I can do it.

    + +

    With you I can do it better.

    + + +
    +
    +
    +
    + + diff --git a/_site/sitemap.xml b/_site/sitemap.xml new file mode 100644 index 0000000..09a7986 --- /dev/null +++ b/_site/sitemap.xml @@ -0,0 +1,906 @@ + + + +/book-notes/canadian-history-for-dummies/ +2021-11-16T23:18:30-07:00 + + +/book-notes/industrial-society-and-its-future/ +2021-11-16T23:18:30-07:00 + + +/emacspeak-tutorials/arrays/ +2021-11-16T23:18:30-07:00 + + +/emacspeak-tutorials/functions/ +2021-11-16T23:18:30-07:00 + + +/emacspeak-tutorials/loops/ +2021-11-16T23:18:30-07:00 + + +/emacspeak-tutorials/pointers/ +2021-11-16T23:18:30-07:00 + + +/emacspeak-tutorials/printing/ +2021-11-16T23:18:30-07:00 + + +/emacspeak-tutorials/structs/ +2021-11-16T23:18:30-07:00 + + +/emacspeak-tutorials/user-input/ +2021-11-16T23:18:30-07:00 + + +/2020/01/22/padding-and-margin/ +2020-01-22T00:00:00-07:00 + + +/2020/01/26/rsa1/ +2020-01-26T00:00:00-07:00 + + +/2020/02/19/rsa2/ +2020-02-19T00:00:00-07:00 + + +/2020/04/02/rsa3/ +2020-04-02T00:00:00-06:00 + + +/2020/04/06/rsa4/ +2020-04-06T00:00:00-06:00 + + +/2020/04/12/nas1/ +2020-04-12T00:00:00-06:00 + + +/2020/04/21/rfi/ +2020-04-21T00:00:00-06:00 + + +/2020/04/25/xss/ +2020-04-25T00:00:00-06:00 + + +/2020/05/01/nginx-socket-io-projects/ +2020-05-01T00:00:00-06:00 + + +/2020/05/19/clue-announcement/ +2020-05-19T00:00:00-06:00 + + +/2020/06/04/site-update/ +2020-06-04T00:00:00-06:00 + + +/2020/06/25/tmux-minecraft/ +2020-06-25T00:00:00-06:00 + + +/2020/07/12/independence/ +2020-07-12T00:00:00-06:00 + + +/2020/07/19/multicraft-php-gentoo/ +2020-07-19T00:00:00-06:00 + + +/2020/07/30/canadian-parliament/ +2020-07-30T00:00:00-06:00 + + +/2020/08/15/openbsd1/ +2020-08-15T00:00:00-06:00 + + +/2020/08/18/django-deployment/ +2020-08-18T00:00:00-06:00 + + +/2020/09/09/lamegames/ +2020-09-09T00:00:00-06:00 + + +/2020/09/12/minesweeper/ +2020-09-12T00:00:00-06:00 + + +/2020/10/26/curiosity/ +2020-10-26T00:00:00-06:00 + + +/2020/11/15/nas2/ +2020-11-15T00:00:00-07:00 + + +/2020/12/01/pacaur-rpi/ +2020-12-01T00:00:00-07:00 + + +/2020/12/14/orca-raspberry-pi-manjaro/ +2020-12-14T00:00:00-07:00 + + +/2020/12/20/deploy-lichess/ +2020-12-20T00:00:00-07:00 + + +/2021/01/31/lichess/ +2021-01-31T00:00:00-07:00 + + +/2021/04/04/quiz-your-friends-xss/ +2021-04-04T00:00:00-06:00 + + +/2021/04/18/uefi-development-environment/ +2021-04-18T00:00:00-06:00 + + +/2021/06/02/pinebook-pro/ +2021-06-02T00:00:00-06:00 + + +/2021/06/21/uefi-audio/ +2021-06-21T00:00:00-06:00 + + +/2021/08/31/vpns-api/ +2021-08-31T00:00:00-06:00 + + +/2021/09/18/how-to-generate-proper-content-mathml-from-katex-or-latex/ +2021-09-18T00:00:00-06:00 + + +/melody/225/01-intro/ + + +/melody/cmpt-225/01/01-intro/ + + +/melody/cmpt-295/01/01/ + + +/melody/225/02-stacks-and-queues/ + + +/melody/cmpt-225/02/02-stacks-and-queues/ + + +/melody/cmpt-295/activities/02/02/ + + +/melody/cmpt-225/03/03-memory-and-pointers/ + + +/melody/cmpt/03/ + + +/melody/cmpt-295/03/03/ + + +/melody/cmpt-295/asses/ass3/03/ + + +/melody/cmpt-295/activities/04/04-solution/ + + +/melody/cmpt/04/ + + +/melody/cmpt-225/04/04/ + + +/melody/cmpt-295/activities/04/04/ + + +/melody/cmpt-295/04/04/ + + +/melody/cmpt-295/asses/ass5/05/ + + +/melody/cmpt-225/06/06/ + + +/melody/cmpt-295/06/06/ + + +/melody/cmpt-225/07/07/ + + +/melody/cmpt-295/07/07/ + + +/melody/cmpt-225/08/08/ + + +/melody/cmpt-295/08/08/ + + +/melody/cmpt-225/09/09/ + + +/melody/cmpt-295/09/09/ + + +/melody/cmpt-225/10/10/ + + +/melody/cmpt-295/10/10/ + + +/melody/cmpt-225/11/11/ + + +/melody/cmpt-295/11/11/ + + +/melody/cmpt-225/12/12/ + + +/melody/cmpt-295/12/12/ + + +/melody/cmpt-225/13/13/ + + +/melody/cmpt-295/13/13/ + + +/melody/cmpt-295/14/14-updated/ + + +/melody/cmpt-225/14/14/ + + +/melody/cmpt-295/14/14/ + + +/melody/cmpt-225/15/15/ + + +/melody/cmpt-295/15/15/ + + +/melody/cmpt-295/16/16/ + + +/melody/cmpt-295/17/17/ + + +/melody/cmpt-295/18/18/ + + +/melody/cmpt-295/19/19/ + + +/melody/cmpt-295/20/20/ + + +/melody/cmpt-295/21/21/ + + +/melody/cmpt-295/22/22/ + + +/melody/melodys-transcriptions/cmpt225-05/225%20L5%20-%20Dynamic%20DS%20-%20LL/ + + +/melody/cmpt-225/05/225%20L5%20-%20Dynamic%20DS%20-%20LL/ + + +/melody/cmpt-295/23/23/ + + +/melody/cmpt-295/24/24-annotated/ + + +/melody/cmpt-295/24/24/ + + +/melody/cmpt-295/25/25/ + + +/melody/cmpt-295/26/26/ + + +/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro_Annotated/ + + +/melody/melodys-transcriptions/cmpt295-05/Lecture_05_Data_Representation_Fractional_Numbers/ + + +/melody/cmpt-295/05/Lecture_05_Data_Representation_Fractional_Numbers/ + + +/melody/cmpt-295/midterm/about/about/ + + +/melody/cmpt-295/asses/ass2/ass2/ + + +/melody/cmpt-295/asses/ass4/ass4/ + + +/melody/cmpt-295/asses/ass6/ass6/ + + +/melody/cmpt-295/asses/ass7/ass7/ + + +/melody/cmpt-295/demo/demo/ + + +/blog/ + + +/ + + +/scholarships/2020/cnib/ + + +/tutoring/ + + +/ideas/ + + +/contact/ + + +/emacspeak-tutorials/ + + +/cover-letter-vcc/ + + +/md/ + + +/cover-letters/bloombase/ + + +/cover-letters/protonmail/ + + +/resume/ + + +/about/ + + +/links/ + + +/book-notes/ + + +/isaif/ + + +/tests/latex-cases/ + + +/tests/latex-grouping/ + + +/melody/cmpt-295/midterm-solutions/midterm-solutions/ + + +/melody/cmpt-295/midterm2/midterm2/ + + +/melody/cmpt/test1/ + + +/assets/pdf/bios-accessibility.pdf +2021-05-10T16:52:30-06:00 + + +/melody/cmpt-225/01/01-intro.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/02/02-stacks-and-queues.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/03/03-memory-and-pointers.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/04/04.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/04/225%20L4%20-%20Vector%20Implementation%20.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/05/225%20L5%20-%20Dynamic%20DS%20-%20LL.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/05/225%20L5%20-%20Dynamic%20DS%20-%20LL.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/06/06.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/06/225%20L6%20-%20Call%20Stack%20And%20Heap.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/07.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/07/07.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/07/225%20L7%20-%20LL%20Queue%20and%20Traversal.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/08/08.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/08/225%20L8%20-%20Rooted%20Trees.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/09/09.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/09/225%20L9%20-%20Recursion%20On%20Trees.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/10/10.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/10/225%20L10%20-%20Big-Oh.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/11/11.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/11/225%20L11%20-%20BST.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/12/12.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/12/Lecture_12_AVL.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/13/13.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/13/225%20L13%20-%20PQ%20and%20Heaps.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/14/14.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/14/225%20L14%20-%20Big%20Omega%20and%20Theta.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/15/15.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-225/15/225%20L15%20-%20Sorting.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/01/Lecture_01_Activity_Code.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/01/Lecture_01_Course_Overview.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/01/Lecture_01_Course_Overview_Annotated.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/01/Lecture_01_Course_Overview_annotated.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro_Annotated.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/02/Lecture_02_Data_Representation_Intro_Annotated.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/03/03.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/03/Lecture_03_Data_Representation_Integers.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/04/04.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/04/Lecture_04_Data_Representation_Integers.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/05/Lecture_05_Data_Representation_Fractional_Numbers.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/05/Lecture_05_Data_Representation_Fractional_Numbers.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/06/06.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/06/Lecture_06_Data_Representation_Fractional_Numbers.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/07/07.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/07/Lecture_07_Data_Representation_Fractional_Numbers_Rounding_Arithmetic_Operations.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/08/08.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/08/Lecture_08_Machine_Level_Prog_Intro.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/09/09.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/09/Lecture_09_Machine_Level_Prog_Data_Move_Annotated.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/10/10.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/10/Lecture_10_Machine_Level_Prog_Arithmetic_leaq_Instructions.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/11/11.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/11/Lecture_11_Machine_Level_Prog_Instructions_Practice_Demo.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/12/12.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/12/Lecture_12_Machine_Level_Prog_Control.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/13/13-updated.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/13/13.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/13/Lecture_13_Machine_Level_Prog_Loops.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/13/Lecture_13_Machine_Level_Prog_Loops_Annotated.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/14/14-updated.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/14/14.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/14/Lecture_14_Machine_Level_Prog_Fcn_Call_Stack.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/14/Lecture_14_Machine_Level_Prog_Fcn_Call_Stack_Annotated.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/15/15.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/15/Lecture_15_Machine_Level_Prog_Fcn_Call_Passing_Control.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/16/16.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/16/Lecture_16_Midterm_1_Review_Session.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/17/17.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/17/Lecture_17_Midterm_1_1211.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/18/18.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/18/Lecture_18_Machine_Level_Prog_Fcn_Call_Passing_Data.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/19/19.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/19/Lecture_19_Machine_Level_Prog_Fcn_Call_Mem_Mgmt.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/20/20.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/20/Lecture_20_Machine_Level_Prog_Array_1D.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/21/21.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/21/Lecture_21_Machine_Level_Prog_Array_2D.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/22/22.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/22/Lecture_22_Machine_Level_Prog_Buffer_Overflow_and_Fl_Pt_Annotated.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/23/23.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/23/Lecture_23_ISA_Intro_and_Design_Annotated.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/23/micro_graph.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/24/24-annotated.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/24/24.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/24/Lecture_24_ISA_MIPS.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/24/Lecture_24_ISA_MIPS_Annotated.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/25/25.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/25/Lecture_25_ISA_Evaluation.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/26/26.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/26/Lecture_26_Intro_to_Logic_Design.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/26/Lecture_26_Intro_to_Logic_Design_Annotated.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/26/micro_graph.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/Lecture_06_Data_Representation_Fractional_Numbers.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/Lecture_06_Data_Representation_Fractional_Numbers_Annotated.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/activities/02/02.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/activities/04/04-solution.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/activities/04/04.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/activities/04/Participation_Activity_4_1217_CAL.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/activities/04/Participation_Activity_4_1217_SOLUTION.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/asses/ass2/Assignment_2.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/asses/ass2/ass2.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/asses/ass3/Assignment_3.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/asses/ass3/ass3.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/asses/ass4/Assignment_4.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/asses/ass4/ass4.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/asses/ass5/05.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/asses/ass5/Assignment_5.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/asses/ass6/Assignment_6.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/asses/ass6/ass6.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/asses/ass7/Assignment_7.pdf +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/asses/ass7/ass7.html +2021-11-16T20:20:37-07:00 + + +/melody/cmpt-295/computer-bus/Computer_Bus.pdf +2021-11-16T20:20:38-07:00 + + +/melody/cmpt-295/demo/Demo_Copy_Annotated.pdf +2021-11-16T20:20:38-07:00 + + +/melody/cmpt-295/demo/demo.html +2021-11-16T20:20:38-07:00 + + +/melody/cmpt-295/midterm-solutions/Cmpt295_Midterm_1_Fall_2021_SOLUTION.pdf +2021-11-16T20:20:38-07:00 + + +/melody/cmpt-295/midterm-solutions/midterm-solutions.html +2021-11-16T20:20:38-07:00 + + +/melody/cmpt-295/midterm/about/About_our_Midterm_1.pdf +2021-11-16T20:20:38-07:00 + + +/melody/cmpt-295/midterm/about/about.html +2021-11-16T20:20:38-07:00 + + +/melody/cmpt-295/midterm2/About_our_Midterm_2.pdf +2021-11-16T20:20:38-07:00 + + +/melody/cmpt-295/midterm2/midterm2.html +2021-11-16T20:20:38-07:00 + + +/melody/melodys-transcriptions/cmpt225-05/225%20L5%20-%20Dynamic%20DS%20-%20LL.html +2021-11-16T20:20:38-07:00 + + +/melody/melodys-transcriptions/cmpt225-05/225%20L5%20-%20Dynamic%20DS%20-%20LL.pdf +2021-11-16T20:20:38-07:00 + + +/melody/melodys-transcriptions/cmpt295-05/Lecture_05_Data_Representation_Fractional_Numbers.html +2021-11-16T20:20:38-07:00 + + +/melody/melodys-transcriptions/cmpt295-05/Lecture_05_Data_Representation_Fractional_Numbers.pdf +2021-11-16T20:20:38-07:00 + + diff --git a/_site/tests/latex-cases/index.html b/_site/tests/latex-cases/index.html new file mode 100644 index 0000000..2d18b1d --- /dev/null +++ b/_site/tests/latex-cases/index.html @@ -0,0 +1,49 @@ + + + + + | tait.tech + + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    S(n)={0if n=0n+S(n1)if n>0 +S(n) = \begin{cases} +0 & \text{if } n=0\\ +n+S(n-1) & \text{if } n>0 +\end{cases} +

    + +
    +
    + +
    + + diff --git a/_site/tests/latex-grouping/index.html b/_site/tests/latex-grouping/index.html new file mode 100644 index 0000000..1c6a00f --- /dev/null +++ b/_site/tests/latex-grouping/index.html @@ -0,0 +1,55 @@ + + + + + | tait.tech + + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    Grouping V0:

    + +

    10012+01102=11112 +\text{1001}_{2} + \text{0110}_{2} = \text{1111}_{2} +

    + +

    Grouping V1: 51210=2109=1111111112\text{512}_{10} = 2^{9}_{10} = \text{111111111}_{2}

    + +

    Grouping V2: 51210=2109=1111111112\mathit{512}_{10} = 2^{9}_{10} = \mathit{111111111}_{2}

    + +

    Grouping V3: 51210=2109=1111111112\it{512}_{10} = 2^{9}_{10} = \it{111111111}_{2}

    + + +
    +
    + +
    + + diff --git a/_site/text/industrial-society-and-its-future.txt b/_site/text/industrial-society-and-its-future.txt new file mode 100644 index 0000000..ca6a8d5 --- /dev/null +++ b/_site/text/industrial-society-and-its-future.txt @@ -0,0 +1,1854 @@ +The Unabomber Manifesto + +Industrial Society and its Future +by Theodore Kaczynski + +Introduction +1. The Industrial Revolution and its consequences have been a disaster for the human race. They have greatly increased the +life-expectancy of those of us who live in “advanced” countries, but they have destabilized society, have made life unfulfilling, +have subjected human beings to indignities, have led to widespread psychological suffering (in the Third World to physical +suffering as well) and have inflicted severe damage on the natural world. The continued development of technology will +worsen the situation. It will certainly subject human being to greater indignities and inflict greater damage on the natural world, +it will probably lead to greater social disruption and psychological suffering, and it may lead to increased physical suffering +even in “advanced” countries. +2. The industrial-technological system may survive or it may break down. If it survives, it MAY eventually achieve a low level +of physical and psychological suffering, but only after passing through a long and very painful period of adjustment and only at +the cost of permanently reducing human beings and many other living organisms to engineered products and mere cogs in the +social machine. Furthermore, if the system survives, the consequences will be inevitable: There is no way of reforming or +modifying the system so as to prevent it from depriving people of dignity and autonomy. +3. If the system breaks down the consequences will still be very painful. But the bigger the system grows the more disastrous +the results of its breakdown will be, so if it is to break down it had best break down sooner rather than later. +4. We therefore advocate a revolution against the industrial system. This revolution may or may not make use of violence; it +may be sudden or it may be a relatively gradual process spanning a few decades. We can’t predict any of that. But we do +outline in a very general way the measures that those who hate the industrial system should take in order to prepare the way for +a revolution against that form of society. This is not to be a POLITICAL revolution. Its object will be to overthrow not +governments but the economic and technological basis of the present society. +5. In this article we give attention to only some of the negative developments that have grown out of the industrialtechnological system. Other such developments we mention only briefly or ignore altogether. This does not mean that we +regard these other developments as unimportant. For practical reasons we have to confine our discussion to areas that have +received insufficient public attention or in which we have something new to say. For example, since there are well-developed +environmental and wilderness movements, we have written very little about environmental degradation or the destruction of +wild nature, even though we consider these to be highly important. + +The Psychology of Modern Leftism + +6. Almost everyone will agree that we live in a deeply troubled society. One of the most widespread manifestations of the +craziness of our world is leftism, so a discussion of the psychology of leftism can serve as an introduction to the discussion of +the problems of modern society in general. +7. But what is leftism? During the first half of the 20th century leftism could have been practically identified with socialism. +Today the movement is fragmented and it is not clear who can properly be called a leftist. When we speak of leftists in this +article we have in mind mainly socialists, collectivists, “politically correct” types, feminists, gay and disability activists, animal +rights activists and the like. But not everyone who is associated with one of these movements is a leftist. What we are trying to +get at in discussing leftism is not so much movement or an ideology as a psychological type, or rather a collection of related +types. Thus, what we mean by “leftism” will emerge more clearly in the course of our discussion of leftist psychology. (Also, +see paragraphs 227-230.) +8. Even so, our conception of leftism will remain a good deal less clear than we would wish, but there doesn’t seem to be any +remedy for this. All we are trying to do here is indicate in a rough and approximate way the two psychological tendencies that +we believe are the main driving force of modern leftism. We by no means claim to be telling the WHOLE truth about leftist +psychology. Also, our discussion is meant to apply to modern leftism only. We leave open the question of the extent to which +our discussion could be applied to the leftists of the 19th and early 20th centuries. +9. The two psychological tendencies that underlie modern leftism we call “feelings of inferiority” and “oversocialization.” +Feelings of inferiority are characteristic of modern leftism as a whole, while oversocialization is characteristic only of a certain +segment of modern leftism; but this segment is highly influential. + +Feelings of Inferiority +10. By “feelings of inferiority” we mean not only inferiority feelings in the strict sense but a whole spectrum of related traits; +low self-esteem, feelings of powerlessness, depressive tendencies, defeatism, guilt, self-hatred, etc. We argue that modern +leftists tend to have some such feelings (possibly more or less repressed) and that these feelings are decisive in determining the +direction of modern leftism. +11. When someone interprets as derogatory almost anything that is said about him (or about groups with whom he identifies) +we conclude that he has inferiority feelings or low self-esteem. This tendency is pronounced among minority rights activists, +whether or not they belong to the minority groups whose rights they defend. They are hypersensitive about the words used to +designate minorities and about anything that is said concerning minorities. The terms “Negro,” “oriental,” “handicapped” or +“chick” for an African, an Asian, a disabled person or a woman originally had no derogatory connotation. “Broad” and “chick” +were merely the feminine equivalents of “guy,” “dude” or “fellow.” The negative connotations have been attached to these +terms by the activists themselves. Some animal rights activists have gone so far as to reject the word “pet” and insist on its +replacement by “animal companion.” Leftish anthropologists go to great lengths to avoid saying anything about primitive +peoples that could conceivably be interpreted as negative. They want to replace the word “primitive” by “nonliterate.” They +may seem almost paranoid about anything that might suggest that any primitive culture is inferior to ours. (We do not mean to +imply that primitive cultures ARE inferior to ours. We merely point out the hyper sensitivity of leftish anthropologists.) +12. Those who are most sensitive about “politically incorrect” terminology are not the average black ghetto-dweller, Asian +immigrant, abused woman or disabled person, but a minority of activists, many of whom do not even belong to any +“oppressed” group but come from privileged strata of society. Political correctness has its stronghold among university +professors, who have secure employment with comfortable salaries, and the majority of whom are heterosexual white males +from middle- to upper-middle-class families. +13. Many leftists have an intense identification with the problems of groups that have an image of being weak (women), +defeated (American Indians), repellent (homosexuals) or otherwise inferior. The leftists themselves feel that these groups are +inferior. They would never admit to themselves that they have such feelings, but it is precisely because they do see these +groups as inferior that they identify with their problems. (We do not mean to suggest that women, Indians, etc. ARE inferior; +we are only making a point about leftist psychology.) +14. Feminists are desperately anxious to prove that women are as strong and as capable as men. Clearly they are nagged by a +fear that women may NOT be as strong and as capable as men. + +15. Leftists tend to hate anything that has an image of being strong, good and successful. They hate America, they hate +Western civilization, they hate white males, they hate rationality. The reasons that leftists give for hating the West, etc. clearly +do not correspond with their real motives. They SAY they hate the West because it is warlike, imperialistic, sexist, ethnocentric +and so forth, but where these same faults appear in socialist countries or in primitive cultures, the leftist finds excuses for them, +or at best he GRUDGINGLY admits that they exist; whereas he ENTHUSIASTICALLY points out (and often greatly +exaggerates) these faults where they appear in Western civilization. Thus it is clear that these faults are not the leftist’s real +motive for hating America and the West. He hates America and the West because they are strong and successful. +16. Words like “self-confidence,” “self-reliance,” “initiative,” “enterprise,” “optimism,” etc., play little role in the liberal and +leftist vocabulary. The leftist is anti-individualistic, pro-collectivist. He wants society to solve every one’s problems for them, +satisfy everyone’s needs for them, take care of them. He is not the sort of person who has an inner sense of confidence in his +ability to solve his own problems and satisfy his own needs. The leftist is antagonistic to the concept of competition because, +deep inside, he feels like a loser. +17. Art forms that appeal to modern leftish intellectuals tend to focus on sordidness, defeat and despair, or else they take an +orgiastic tone, throwing off rational control as if there were no hope of accomplishing anything through rational calculation and +all that was left was to immerse oneself in the sensations of the moment. +18. Modern leftish philosophers tend to dismiss reason, science, objective reality and to insist that everything is culturally +relative. It is true that one can ask serious questions about the foundations of scientific knowledge and about how, if at all, the +concept of objective reality can be defined. But it is obvious that modern leftish philosophers are not simply cool-headed +logicians systematically analyzing the foundations of knowledge. They are deeply involved emotionally in their attack on truth +and reality. They attack these concepts because of their own psychological needs. For one thing, their attack is an outlet for +hostility, and, to the extent that it is successful, it satisfies the drive for power. More importantly, the leftist hates science and +rationality because they classify certain beliefs as true (i.e., successful, superior) and other beliefs as false (i.e., failed, inferior). +The leftist’s feelings of inferiority run so deep that he cannot tolerate any classification of some things as successful or superior +and other things as failed or inferior. This also underlies the rejection by many leftists of the concept of mental illness and of +the utility of IQ tests. Leftists are antagonistic to genetic explanations of human abilities or behavior because such explanations +tend to make some persons appear superior or inferior to others. Leftists prefer to give society the credit or blame for an +individual’s ability or lack of it. Thus if a person is “inferior” it is not his fault, but society’s, because he has not been brought +up properly. +19. The leftist is not typically the kind of person whose feelings of inferiority make him a braggart, an egotist, a bully, a selfpromoter, a ruthless competitor. This kind of person has not wholly lost faith in himself. He has a deficit in his sense of power +and self-worth, but he can still conceive of himself as having the capacity to be strong, and his efforts to make himself strong +produce his unpleasant behavior. [1] But the leftist is too far gone for that. His feelings of inferiority are so ingrained that he +cannot conceive of himself as individually strong and valuable. Hence the collectivism of the leftist. He can feel strong only as +a member of a large organization or a mass movement with which he identifies himself. +20. Notice the masochistic tendency of leftist tactics. Leftists protest by lying down in front of vehicles, they intentionally +provoke police or racists to abuse them, etc. These tactics may often be effective, but many leftists use them not as a means to +an end but because they PREFER masochistic tactics. Self-hatred is a leftist trait. +21. Leftists may claim that their activism is motivated by compassion or by moral principles, and moral principle does play a +role for the leftist of the oversocialized type. But compassion and moral principle cannot be the main motives for leftist +activism. Hostility is too prominent a component of leftist behavior; so is the drive for power. Moreover, much leftist behavior +is not rationally calculated to be of benefit to the people whom the leftists claim to be trying to help. For example, if one +believes that affirmative action is good for black people, does it make sense to demand affirmative action in hostile or +dogmatic terms? Obviously it would be more productive to take a diplomatic and conciliatory approach that would make at +least verbal and symbolic concessions to white people who think that affirmative action discriminates against them. But leftist +activists do not take such an approach because it would not satisfy their emotional needs. Helping black people is not their real +goal. Instead, race problems serve as an excuse for them to express their own hostility and frustrated need for power. In doing +so they actually harm black people, because the activists’ hostile attitude toward the white majority tends to intensify race +hatred. +22. If our society had no social problems at all, the leftists would have to INVENT problems in order to provide themselves +with an excuse for making a fuss. + +23. We emphasize that the foregoing does not pretend to be an accurate description of everyone who might be considered a +leftist. It is only a rough indication of a general tendency of leftism. + +Oversocialization +24. Psychologists use the term “socialization” to designate the process by which children are trained to think and act as society +demands. A person is said to be well socialized if he believes in and obeys the moral code of his society and fits in well as a +functioning +part of that society. It may seem senseless to say that many leftists are over-socialized, since the leftist is +perceived as a rebel. Nevertheless, the position can be defended. Many leftists are not such rebels as they seem. +25. The moral code of our society is so demanding that no one can think, feel and act in a completely moral way. For example, +we are not supposed to hate anyone, yet almost everyone hates somebody at some time or other, whether he admits it to himself +or not. Some people are so highly socialized that the attempt to think, feel and act morally imposes a severe burden on them. In +order to avoid feelings of guilt, they continually have to deceive themselves about their own motives and find moral +explanations for feelings and actions that in reality have a nonmoral origin. We use the term “oversocialized” to describe such +people. [2] +26. Oversocialization can lead to low self-esteem, a sense of powerlessness, defeatism, guilt, etc. One of the most important +means by which our society socializes children is by making them feel ashamed of behavior or speech that is contrary to +society’s expectations. If this is overdone, or if a particular child is especially susceptible to such feelings, he ends by feeling +ashamed of HIMSELF. Moreover the thought and the behavior of the oversocialized person are more restricted by society’s +expectations than are those of the lightly socialized person. The majority of people engage in a significant amount of naughty +behavior. They lie, they commit petty thefts, they break traffic laws, they goof off at work, they hate someone, they say spiteful +things or they use some underhanded trick to get ahead of the other guy. The oversocialized person cannot do these things, or if +he does do them he generates in himself a sense of shame and self-hatred. The oversocialized person cannot even experience, +without guilt, thoughts or feelings that are contrary to the accepted morality; he cannot think “unclean” thoughts. And +socialization is not just a matter of morality; we are socialized to conform to many norms of behavior that do not fall under the +heading of morality. Thus the oversocialized person is kept on a psychological leash and spends his life running on rails that +society has laid down for him. In many oversocialized people this results in a sense of constraint and powerlessness that can be +a severe hardship. We suggest that oversocialization is among the more serious cruelties that human being inflict on one +another. +27. We argue that a very important and influential segment of the modern left is oversocialized and that their oversocialization +is of great importance in determining the direction of modern leftism. Leftists of the oversocialized type tend to be intellectuals +or members of the upper-middle class. Notice that university intellectuals [3] constitute the most highly socialized segment of +our society and also the most leftwing segment. +28. The leftist of the oversocialized type tries to get off his psychological leash and assert his autonomy by rebelling. But +usually he is not strong enough to rebel against the most basic values of society. Generally speaking, the goals of today’s +leftists are NOT in conflict with the accepted morality. On the contrary, the left takes an accepted moral principle, adopts it as +its own, and then accuses mainstream society of violating that principle. Examples: racial equality, equality of the sexes, +helping poor people, peace as opposed to war, nonviolence generally, freedom of expression, kindness to animals. More +fundamentally, the duty of the individual to serve society and the duty of society to take care of the individual. All these have +been deeply rooted values of our society (or at least of its middle and upper classes [4] for a long time. These values are +explicitly or implicitly expressed or presupposed in most of the material presented to us by the mainstream communications +media and the educational system. Leftists, especially those of the oversocialized type, usually do not rebel against these +principles but justify their hostility to society by claiming (with some degree of truth) that society is not living up to these +principles. +29. Here is an illustration of the way in which the oversocialized leftist shows his real attachment to the conventional attitudes +of our society while pretending to be in rebellion against it. Many leftists push for affirmative action, for moving black people +into high-prestige jobs, for improved education in black schools and more money for such schools; the way of life of the black +“underclass” they regard as a social disgrace. They want to integrate the black man into the system, make him a business +executive, a lawyer, a scientist just like upper-middle-class white people. The leftists will reply that the last thing they want is +to make the black man into a copy of the white man; instead, they want to preserve African American culture. But in what does +this preservation of African American culture consist? It can hardly consist in anything more than eating black-style food, + +listening to black-style music, wearing black-style clothing and going to a black-style church or mosque. In other words, it can +express itself only in superficial matters. In all ESSENTIAL respects most leftists of the oversocialized type want to make the +black man conform to white, middle-class ideals. They want to make him study technical subjects, become an executive or a +scientist, spend his life climbing the status ladder to prove that black people are as good as white. They want to make black +fathers “responsible,” they want black gangs to become nonviolent, etc. But these are exactly the values of the industrialtechnological system. The system couldn’t care less what kind of music a man listens to, what kind of clothes he wears or what +religion he believes in as long as he studies in school, holds a respectable job, climbs the status ladder, is a “responsible” +parent, is nonviolent and so forth. In effect, however much he may deny it, the oversocialized leftist wants to integrate the +black man into the system and make him adopt its values. +30. We certainly do not claim that leftists, even of the oversocialized type, NEVER rebel against the fundamental values of our +society. Clearly they sometimes do. Some oversocialized leftists have gone so far as to rebel against one of modern society’s +most important principles by engaging in physical violence. By their own account, violence is for them a form of “liberation.” +In other words, by committing violence they break through the psychological restraints that have been trained into them. +Because they are oversocialized these restraints have been more confining for them than for others; hence their need to break +free of them. But they usually justify their rebellion in terms of mainstream values. If they engage in violence they claim to be +fighting against racism or the like. +31. We realize that many objections could be raised to the foregoing thumbnail sketch of leftist psychology. The real situation +is complex, and anything like a complete description of it would take several volumes even if the necessary data were +available. We claim only to have indicated very roughly the two most important tendencies in the psychology of modern +leftism. +32. The problems of the leftist are indicative of the problems of our society as a whole. Low self-esteem, depressive tendencies +and defeatism are not restricted to the left. Though they are especially noticeable in the left, they are widespread in our society. +And today’s society tries to socialize us to a greater extent than any previous society. We are even told by experts how to eat, +how to exercise, how to make love, how to raise our kids and so forth. + +The Power Process +33. Human beings have a need (probably based in biology) for something that we will call the “power process.” This is closely +related to the need for power (which is widely recognized) but is not quite the same thing. The power process has four +elements. The three most clear-cut of these we call goal, effort and attainment of goal. (Everyone needs to have goals whose +attainment requires effort, and needs to succeed in attaining at least some of his goals.) The fourth element is more difficult to +define and may not be necessary for everyone. We call it autonomy and will discuss it later (paragraphs 42-44). +34. Consider the hypothetical case of a man who can have anything he wants just by wishing for it. Such a man has power, but +he will develop serious psychological problems. At first he will have a lot of fun, but by and by he will become acutely bored +and demoralized. Eventually he may become clinically depressed. History shows that leisured aristocracies tend to become +decadent. This is not true of fighting aristocracies that have to struggle to maintain their power. But leisured, secure +aristocracies that have no need to exert themselves usually become bored, hedonistic and demoralized, even though they have +power. This shows that power is not enough. One must have goals toward which to exercise one’s power. +35. Everyone has goals; if nothing else, to obtain the physical necessities of life: food, water and whatever clothing and shelter +are made necessary by the climate. But the leisured aristocrat obtains these things without effort. Hence his boredom and +demoralization. +36. Nonattainment of important goals results in death if the goals are physical necessities, and in frustration if nonattainment of +the goals is compatible with survival. Consistent failure to attain goals throughout life results in defeatism, low self-esteem or +depression. +37. Thus, in order to avoid serious psychological problems, a human being needs goals whose attainment requires effort, and +he must have a reasonable rate of success in attaining his goals. + +Surrogate Activities + +38. But not every leisured aristocrat becomes bored and demoralized. For example, the emperor Hirohito, instead of sinking +into decadent hedonism, devoted himself to marine biology, a field in which he became distinguished. When people do not +have to exert themselves to satisfy their physical needs they often set up artificial goals for themselves. In many cases they then +pursue these goals with the same energy and emotional involvement that they otherwise would have put into the search for +physical necessities. Thus the aristocrats of the Roman Empire had their literary pretensions; many European aristocrats a few +centuries ago invested tremendous time and energy in hunting, though they certainly didn’t need the meat; other aristocracies +have competed for status through elaborate displays of wealth; and a few aristocrats, like Hirohito, have turned to science. +39. We use the term “surrogate activity” to designate an activity that is directed toward an artificial goal that people set up for +themselves merely in order to have some goal to work toward, or let us say, merely for the sake of the “fulfillment” that they +get from pursuing the goal. Here is a rule of thumb for the identification of surrogate activities. Given a person who devotes +much time and energy to the pursuit of goal X, ask yourself this: If he had to devote most of his time and energy to satisfying +his biological needs, and if that effort required him to use his physical and mental faculties in a varied and interesting way, +would he feel seriously deprived because he did not attain goal X? If the answer is no, then the person’s pursuit of goal X is a +surrogate activity. Hirohito’s studies in marine biology clearly constituted a surrogate activity, since it is pretty certain that if +Hirohito had had to spend his time working at interesting non-scientific tasks in order to obtain the necessities of life, he would +not have felt deprived because he didn’t know all about the anatomy and life-cycles of marine animals. On the other hand the +pursuit of sex and love (for example) is not a surrogate activity, because most people, even if their existence were otherwise +satisfactory, would feel deprived if they passed their lives without ever having a relationship with a member of the opposite +sex. (But pursuit of an excessive amount of sex, more than one really needs, can be a surrogate activity.) +40. In modern industrial society only minimal effort is necessary to satisfy one’s physical needs. It is enough to go through a +training program to acquire some petty technical skill, then come to work on time and exert the very modest effort needed to +hold a job. The only requirements are a moderate amount of intelligence and, most of all, simple OBEDIENCE. If one has +those, society takes care of one from cradle to grave. (Yes, there is an underclass that cannot take the physical necessities for +granted, but we are speaking here of mainstream society.) Thus it is not surprising that modern society is full of surrogate +activities. These include scientific work, athletic achievement, humanitarian work, artistic and literary creation, climbing the +corporate ladder, acquisition of money and material goods far beyond the point at which they cease to give any additional +physical satisfaction, and social activism when it addresses issues that are not important for the activist personally, as in the +case of white activists who work for the rights of nonwhite minorities. These are not always PURE surrogate activities, since +for many people they may be motivated in part by needs other than the need to have some goal to pursue. Scientific work may +be motivated in part by a drive for prestige, artistic creation by a need to express feelings, militant social activism by hostility. +But for most people who pursue them, these activities are in large part surrogate activities. For example, the majority of +scientists will probably agree that the “fulfillment” they get from their work is more important than the money and prestige +they earn. +41. For many if not most people, surrogate activities are less satisfying than the pursuit of real goals (that is, goals that people +would want to attain even if their need for the power process were already fulfilled). One indication of this is the fact that, in +many or most cases, people who are deeply involved in surrogate activities are never satisfied, never at rest. Thus the moneymaker constantly strives for more and more wealth. The scientist no sooner solves one problem than he moves on to the next. +The long-distance runner drives himself to run always farther and faster. Many people who pursue surrogate activities will say +that they get far more fulfillment from these activities than they do from the “mundane” business of satisfying their biological +needs, but that is because in our society the effort needed to satisfy the biological needs has been reduced to triviality. More +importantly, in our society people do not satisfy their biological needs AUTONOMOUSLY but by functioning as parts of an +immense social machine. In contrast, people generally have a great deal of autonomy in pursuing their surrogate activities. + +Autonomy +42. Autonomy as a part of the power process may not be necessary for every individual. But most people need a greater or +lesser degree of autonomy in working toward their goals. Their efforts must be undertaken on their own initiative and must be +under their own direction and control. Yet most people do not have to exert this initiative, direction and control as single +individuals. It is usually enough to act as a member of a SMALL group. Thus if half a dozen people discuss a goal among +themselves and make a successful joint effort to attain that goal, their need for the power process will be served. But if they +work under rigid orders handed down from above that leave them no room for autonomous decision and initiative, then their + +need for the power process will not be served. The same is true when decisions are made on a collective basis if the group +making the collective decision is so large that the role of each individual is insignificant. [5] +43. It is true that some individuals seem to have little need for autonomy. Either their drive for power is weak or they satisfy it +by identifying themselves with some powerful organization to which they belong. And then there are unthinking, animal types +who seem to be satisfied with a purely physical sense of power (the good combat soldier, who gets his sense of power by +developing fighting skills that he is quite content to use in blind obedience to his superiors). +44. But for most people it is through the power process having a goal, making an AUTONOMOUS effort and attaining the goal +-- that self-esteem, self-confidence and a sense of power are acquired. When one does not have adequate opportunity to go +through the power process the consequences are (depending on the individual and on the way the power process is disrupted) +boredom, demoralization, low self-esteem, inferiority feelings, defeatism, depression, anxiety, guilt, frustration, hostility, +spouse or child abuse, insatiable hedonism, abnormal sexual behavior, sleep disorders, eating disorders. etc. [6] + +Sources of Social Problems +45. Any of the foregoing symptoms can occur in any society, but in modern industrial society they are present on a massive +scale. We aren’t the first to mention that the world today seems to be going crazy. This sort of thing is not normal for human +societies. There is good reason to believe that primitive man suffered from less stress and frustration and was better satisfied +with his way of life than modern man is. It is true that not all was sweetness and light in primitive societies. Abuse of women +was common among the Australian aborigines, transexuality was fairly common among some of the American Indian tribes. +But it does appear that GENERALLY SPEAKING the kinds of problems that we have listed in the preceding paragraph were +far less common among primitive peoples than they are in modern society. +46. We attribute the social and psychological problems of modern society to the fact that that society requires people to live +under conditions radically different from those under which the human race evolved and to behave in ways that conflict with +the patterns of behavior that the human race developed while living under the earlier conditions. It is clear from what we have +already written that we consider lack of opportunity to properly experience the power process as the most important of the +abnormal conditions to which modern society subjects people. But it is not the only one. Before dealing with disruption of the +power process as a source of social problems we will discuss some of the other sources. +47. Among the abnormal conditions present in modern industrial society are excessive density of population, isolation of man +from nature, excessive rapidity of social change and the breakdown of natural small-scale communities such as the extended +family, the village or the tribe. +48. It is well known that crowding increases stress and aggression. The degree of crowding that exists today and the isolation +of man from nature are consequences of technological progress. All pre-industrial societies were predominantly rural. The +Industrial Revolution vastly increased the size of cities and the proportion of the population that lives in them, and modern +agricultural technology has made it possible for the Earth to support a far denser population than it ever did before. (Also, +technology exacerbates the effects of crowding because it puts increased disruptive powers in people’s hands. For example, a +variety of noise-making devices: power mowers, radios, motorcycles, etc. If the use of these devices is unrestricted, people +who want peace and quiet are frustrated by the noise. If their use is restricted, people who use the devices are frustrated by the +regulations. But if these machines had never been invented there would have been no conflict and no frustration generated by +them.) +49. For primitive societies the natural world (which usually changes only slowly) provided a stable framework and therefore a +sense of security. In the modern world it is human society that dominates nature rather than the other way around, and modern +society changes very rapidly owing to technological change. Thus there is no stable framework. +50. The conservatives are fools: They whine about the decay of traditional values, yet they enthusiastically support +technological progress and economic growth. Apparently it never occurs to them that you can’t make rapid, drastic changes in +the technology and the economy of a society without causing rapid changes in all other aspects of the society as well, and that +such rapid changes inevitably break down traditional values. +51. The breakdown of traditional values to some extent implies the breakdown of the bonds that hold together traditional smallscale social groups. The disintegration of small-scale social groups is also promoted by the fact that modern conditions often +require or tempt individuals to move to new locations, separating themselves from their communities. Beyond that, a +technological society HAS TO weaken family ties and local communities if it is to function efficiently. In modern society an +individual’s loyalty must be first to the system and only secondarily to a smallscale community, because if the internal loyalties +of small-scale communities were stronger than loyalty to the system, such communities would pursue their own advantage at +the expense of the system. +52. Suppose that a public official or a corporation executive appoints his cousin, his friend or his co-religionist to a position +rather than appointing the person best qualified for the job. He has permitted personal loyalty to supersede his loyalty to the +system, and that is “nepotism” or “discrimination,” both of which are terrible sins in modern society. Would-be industrial +societies that have done a poor job of subordinating personal or local loyalties to loyalty to the system are usually very +inefficient. (Look at Latin America.) Thus an advanced industrial society can tolerate only those small-scale communities that +are emasculated, tamed and made into tools of the system. [7] +53. Crowding, rapid change and the breakdown of communities have been widely recognized as sources of social problems. +But we do not believe they are enough to account for the extent of the problems that are seen today. +54. A few pre-industrial cities were very large and crowded, yet their inhabitants do not seem to have suffered from +psychological problems to the same extent as modern man. In America today there still are uncrowded rural areas, and we find +there the same problems as in urban areas, though the problems tend to be less acute in the rural areas. Thus crowding does not +seem to be the decisive factor. +55. On the growing edge of the American frontier during the 19th century, the mobility of the population probably broke down +extended families and small-scale social groups to at least the same extent as these are broken down today. In fact, many +nuclear families lived by choice in such isolation, having no neighbors within several miles, that they belonged to no +community at all, yet they do not seem to have developed problems as a result. +56. Furthermore, change in American frontier society was very rapid and deep. A man might be born and raised in a log cabin, +outside the reach of law and order and fed largely on wild meat; and by the time he arrived at old age he might be working at a +regular job and living in an ordered community with effective law enforcement. This was a deeper change than that which +typically occurs in the life of a modern individual, yet it does not seem to have led to psychological problems. In fact, 19th +century American society had an optimistic and self-confident tone, quite unlike that of today’s society. [8] +57. The difference, we argue, is that modern man has the sense (largely justified) that change is IMPOSED on him, whereas the +19th century frontiersman had the sense (also largely justified) that he created change himself, by his own choice. Thus a +pioneer settled on a piece of land of his own choosing and made it into a farm through his own effort. In those days an entire +county might have only a couple of hundred inhabitants and was a far more isolated and autonomous entity than a modern +county is. Hence the pioneer farmer participated as a member of a relatively small group in the creation of a new, ordered +community. One may well question whether the creation of this community was an improvement, but at any rate it satisfied the +pioneer’s need for the power process. +58. It would be possible to give other examples of societies in which there has been rapid change and/or lack of close +community ties without the kind of massive behavioral aberration that is seen in today’s industrial society. We contend that the +most important cause of social and psychological problems in modern society is the fact that people have insufficient +opportunity to go through the power process in a normal way. We don’t mean to say that modern society is the only one in +which the power process has been disrupted. Probably most if not all civilized societies have interfered with the power process +to a greater or lesser extent. But in modern industrial society the problem has become particularly acute. Leftism, at least in its +recent (mid- to late-20th century) form, is in part a symptom of deprivation with respect to the power process. + +Disruption of the Power Process in Modern Society +59. We divide human drives into three groups: (1) those drives that can be satisfied with minimal effort; (2) those that can be +satisfied but only at the cost of serious effort; (3) those that cannot be adequately satisfied no matter how much effort one +makes. The power process is the process of satisfying the drives of the second group. The more drives there are in the third +group, the more there is frustration, anger, eventually defeatism, depression, etc. +60. In modern industrial society natural human drives tend to be pushed into the first and third groups, and the second group +tends to consist increasingly of artificially created drives. +61. In primitive societies, physical necessities generally fall into group 2: They can be obtained, but only at the cost of serious +effort. But modern society tends to guaranty the physical necessities to everyone [9] in exchange for only minimal effort, hence +physical needs are pushed into group 1. (There may be disagreement about whether the effort needed to hold a job is +“minimal”; but usually, in lower- to middle-level jobs, whatever effort is required is merely that of OBEDIENCE. You sit or +stand where you are told to sit or stand and do what you are told to do in the way you are told to do it. Seldom do you have to +exert yourself seriously, and in any case you have hardly any autonomy in work, so that the need for the power process is not +well served.) +62. Social needs, such as sex, love and status, often remain in group 2 in modern society, depending on the situation of the +individual. [10] But, except for people who have a particularly strong drive for status, the effort required to fulfill the social +drives is insufficient to satisfy adequately the need for the power process. +63. So certain artificial needs have been created that fall into group 2, hence serve the need for the power process. Advertising +and marketing techniques have been developed that make many people feel they need things that their grandparents never +desired or even dreamed of. It requires serious effort to earn enough money to satisfy these artificial needs, hence they fall into +group 2. (But see paragraphs 80-82.) Modern man must satisfy his need for the power process largely through pursuit of the +artificial needs created by the advertising and marketing industry [11], and through surrogate activities. +64. It seems that for many people, maybe the majority, these artificial forms of the power process are insufficient. A theme that +appears repeatedly in the writings of the social critics of the second half of the 20th century is the sense of purposelessness that +afflicts many people in modern society. (This purposelessness is often called by other names such as “anomie” or “middleclass vacuity.”) We suggest that the so-called “identity crisis” is actually a search for a sense of purpose, often for commitment +to a suitable surrogate activity. It may be that existentialism is in large part a response to the purposelessness of modern life. +[12] Very widespread in modern society is the search for “fulfillment.” But we think that for the majority of people an activity +whose main goal is fulfillment (that is, a surrogate activity) does not bring completely satisfactory fulfillment. In other words, +it does not fully satisfy the need for the power process. (See paragraph 41.) That need can be fully satisfied only through +activities that have some external goal, such as physical necessities, sex, love, status, revenge, etc. +65. Moreover, where goals are pursued through earning money, climbing the status ladder or functioning as part of the system +in some other way, most people are not in a position to pursue their goals AUTONOMOUSLY. Most workers are someone +else’s employee and, as we pointed out in paragraph 61, must spend their days doing what they are told to do in the way they +are told to do it. Even people who are in business for themselves have only limited autonomy. It is a chronic complaint of +small-business persons and entrepreneurs that their hands are tied by excessive government regulation. Some of these +regulations are doubtless unnecessary, but for the most part government regulations are essential and inevitable parts of our +extremely complex society. A large portion of small business today operates on the franchise system. It was reported in the +Wall Street Journal a few years ago that many of the franchise-granting companies require applicants for franchises to take a +personality test that is designed to EXCLUDE those who have creativity and initiative, because such persons are not +sufficiently docile to go along obediently with the franchise system. This excludes from small business many of the people who +most need autonomy. +66. Today people live more by virtue of what the system does FOR them or TO them than by virtue of what they do for +themselves. And what they do for themselves is done more and more along channels laid down by the system. Opportunities +tend to be those that the system provides, the opportunities must be exploited in accord with rules and regulations [13], and +techniques prescribed by experts must be followed if there is to be a chance of success. +67. Thus the power process is disrupted in our society through a deficiency of real goals and a deficiency of autonomy in the +pursuit of goals. But it is also disrupted because of those human drives that fall into group 3: the drives that one cannot +adequately satisfy no matter how much effort one makes. One of these drives is the need for security. Our lives depend on +decisions made by other people; we have no control over these decisions and usually we do not even know the people who +make them. (“We live in a world in which relatively few people -- maybe 500 or 1,000 make the important decisions” -- Philip +B. Heymann of Harvard Law School, quoted by Anthony Lewis, New York Times, April 21,1995.) Our lives depend on +whether safety standards at a nuclear power plant are properly maintained; on how much pesticide is allowed to get into our +food or how much pollution into our air; on how skillful (or incompetent) our doctor is; whether we lose or get a job may +depend on decisions made by government economists or corporation executives; and so forth. Most individuals are not in a +position to secure themselves against these threats to more [than] a very limited extent. The individual’s search for security is +therefore frustrated, which leads to a sense of powerlessness. +68. It may be objected that primitive man is physically less secure than modern man, as is shown by his shorter life expectancy; +hence modern man suffers from less, not more than the amount of insecurity that is normal for human beings. But +psychological security does not closely correspond with physical security. What makes us FEEL secure is not so much +objective security as a sense of confidence in our ability to take care of ourselves. Primitive man, threatened by a fierce animal +or by hunger, can fight in self-defense or travel in search of food. He has no certainty of success in these efforts, but he is by no +means helpless against the things that threaten him. The modern individual on the other hand is threatened by many things +against which he is helpless: nuclear accidents, carcinogens in food, environmental pollution, war, increasing taxes, invasion of +his privacy by large organizations, nationwide social or economic phenomena that may disrupt his way of life. +69. It is true that primitive man is powerless against some of the things that threaten him; disease for example. But he can +accept the risk of disease stoically. It is part of the nature of things, it is no one’s fault, unless it is the fault of some imaginary, +impersonal demon. But threats to the modern individual tend to be MAN-MADE. They are not the results of chance but are +IMPOSED on him by other persons whose decisions he, as an individual, is unable to influence. Consequently he feels +frustrated, humiliated and angry. +70. Thus primitive man for the most part has his security in his own hands (either as an individual or as a member of a SMALL +group) whereas the security of modern man is in the hands of persons or organizations that are too remote or too large for him +to be able personally to influence them. So modern man’s drive for security tends to fall into groups 1 and 3; in some areas +(food, shelter etc.) his security is assured at the cost of only trivial effort, whereas in other areas he CANNOT attain security. +(The foregoing greatly simplifies the real situation, but it does indicate in a rough, general way how the condition of modern +man differs from that of primitive man.) +71. People have many transitory drives or impulses that are necessarily frustrated in modern life, hence fall into group 3. One +may become angry, but modern society cannot permit fighting. In many situations it does not even permit verbal aggression. +When going somewhere one may be in a hurry, or one may be in a mood to travel slowly, but one generally has no choice but +to move with the flow of traffic and obey the traffic signals. One may want to do one’s work in a different way, but usually one +can work only according to the rules laid down by one’s employer. In many other ways as well, modern man is strapped down +by a network of rules and regulations (explicit or implicit) that frustrate many of his impulses and thus interfere with the power +process. Most of these regulations cannot be dispensed with, because they are necessary for the functioning of industrial +society. +72. Modern society is in certain respects extremely permissive. In matters that are irrelevant to the functioning of the system +we can generally do what we please. We can believe in any religion (as long as it does not encourage behavior that is +dangerous to the system). We can go to bed with anyone we like (as long as we practice “safe sex”). We can do anything we +like as long as it is UNIMPORTANT. But in all IMPORTANT matters the system tends increasingly to regulate our behavior. +73. Behavior is regulated not only through explicit rules and not only by the government. Control is often exercised through +indirect coercion or through psychological pressure or manipulation, and by organizations other than the government, or by the +system as a whole. Most large organizations use some form of propaganda [14] to manipulate public attitudes or behavior. +Propaganda is not limited to “commercials” and advertisements, and sometimes it is not even consciously intended as +propaganda by the people who make it. For instance, the content of entertainment programming is a powerful form of +propaganda. An example of indirect coercion: There is no law that says we have to go to work every day and follow our +employer’s orders. Legally there is nothing to prevent us from going to live in the wild like primitive people or from going into +business for ourselves. But in practice there is very little wild country left, and there is room in the economy for only a limited +number of small business owners. Hence most of us can survive only as someone else’s employee. +74. We suggest that modern man’s obsession with longevity, and with maintaining physical vigor and sexual attractiveness to +an advanced age, is a symptom of unfulfillment resulting from deprivation with respect to the power process. The “mid-life +crisis” also is such a symptom. So is the lack of interest in having children that is fairly common in modern society but almost +unheard-of in primitive societies. +75. In primitive societies life is a succession of stages. The needs and purposes of one stage having been fulfilled, there is no +particular reluctance about passing on to the next stage. A young man goes through the power process by becoming a hunter, +hunting not for sport or for fulfillment but to get meat that is necessary for food. (In young women the process is more +complex, with greater emphasis on social power; we won’t discuss that here.) This phase having been successfully passed +through, the young man has no reluctance about settling down to the responsibilities of raising a family. (In contrast, some +modern people indefinitely postpone having children because they are too busy seeking some kind of “fulfillment.” We suggest +that the fulfillment they need is adequate experience of the power process -- with real goals instead of the artificial goals of +surrogate activities.) Again, having successfully raised his children, going through the power process by providing them with +the physical necessities, the primitive man feels that his work is done and he is prepared to accept old age (if he survives that +long) and death. any modern people, on the other hand, are disturbed by the prospect of physical deterioration and death, as is +shown by the amount of effort they expend trying to maintain their physical condition, appearance and health. We argue that +this is due to unfulfillment resulting from the fact that they have never put their physical powers to any practical use, have +never gone through the power process using their bodies in a serious way. It is not the primitive man, who has used his body +daily for practical purposes, who fears the deterioration of age, but the modern man, who has never had a practical use for his +body beyond walking from his car to his house. It is the man whose need for the power process has been satisfied during his +life who is best prepared to accept the end of that life. +76. In response to the arguments of this section someone will say, “Society must find a way to give people the opportunity to +go through the power process.” For such people the value of the opportunity is destroyed by the very fact that society gives it to +them. What they need is to find or make their own opportunities. As long as the system GIVES them their opportunities it still +has them on a leash. To attain autonomy they must get off that leash. + +How Some People Adjust +77. Not everyone in industrial-technological society suffers from psychological problems. Some people even profess to be +quite satisfied with society as it is. We now discuss some of the reasons why people differ so greatly in their response to +modern society. +78. First, there doubtless are differences in the strength of the drive for power. Individuals with a weak drive for power may +have relatively little need to go through the power process, or at least relatively little need for autonomy in the power process. +These are docile types who would have been happy as plantation darkies in the Old South. (We don’t mean to sneer at the +“plantation darkies” of the Old South. To their credit, most of the slaves were NOT content with their servitude. We do sneer at +people who ARE content with servitude.) +79. Some people may have some exceptional drive, in pursuing which they satisfy their need for the power process. For +example, those who have an unusually strong drive for social status may spend their whole lives climbing the status ladder +without ever getting bored with that game. +80. People vary in their susceptibility to advertising and marketing techniques. Some are so susceptible that, even if they make +a great deal of money, they cannot satisfy their constant craving for the shiny new toys that the marketing industry dangles +before their eyes. So they always feel hard-pressed financially even if their income is large, and their cravings are frustrated. +81. Some people have low susceptibility to advertising and marketing techniques. These are the people who aren’t interested in +money. Material acquisition does not serve their need for the power process. +82. People who have medium susceptibility to advertising and marketing techniques are able to earn enough money to satisfy +their craving for goods and services, but only at the cost of serious effort (putting in overtime, taking a second job, earning +promotions, etc.). Thus material acquisition serves their need for the power process. But it does not necessarily follow that their +need is fully satisfied. They may have insufficient autonomy in the power process (their work may consist of following orders) +and some of their drives may be frustrated (e.g., security, aggression). (We are guilty of oversimplification in paragraphs 80-82 +because we have assumed that the desire for material acquisition is entirely a creation of the advertising and marketing +industry. Of course it’s not that simple. [11] +83. Some people partly satisfy their need for power by identifying themselves with a powerful organization or mass movement. +An individual lacking goals or power joins a movement or an organization, adopts its goals as his own, then works toward +those goals. When some of the goals are attained, the individual, even though his personal efforts have played only an +insignificant part in the attainment of the goals, feels (through his identification with the movement or organization) as if he +had gone through the power process. This phenomenon was exploited by the fascists, nazis and communists. Our society uses it +too, though less crudely. Example: Manuel Noriega was an irritant to the U.S. (goal: punish Noriega). The U.S. invaded +Panama (effort) and punished Noriega (attainment of goal). Thus the U.S. went through the power process and many +Americans, because of their identification with the U.S., experienced the power process vicariously. Hence the widespread +public approval of the Panama invasion; it gave people a sense of power. [15] We see the same phenomenon in armies, +corporations, political parties, humanitarian organizations, religious or ideological movements. In particular, leftist movements +tend to attract people who are seeking to satisfy their need for power. But for most people identification with a large +organization or a mass movement does not fully satisfy the need for power. +84. Another way in which people satisfy their need for the power process is through surrogate activities. As we explained in +paragraphs 38-40, a surrogate activity is an activity that is directed toward an artificial goal that the individual pursues for the +sake of the “fulfillment” that he gets from pursuing the goal, not because he needs to attain the goal itself. For instance, there is +no practical motive for building enormous muscles, hitting a little ball into a hole or acquiring a complete series of postage +stamps. Yet many people in our society devote themselves with passion to bodybuilding, golf or stamp-collecting. Some people +are more “other-directed” than others, and therefore will more readily attach importance to a surrogate activity simply because +the people around them treat it as important or because society tells them it is important. That is why some people get very +serious about essentially trivial activities such as sports, or bridge, or chess, or arcane scholarly pursuits, whereas others who +are more clear-sighted never see these things as anything but the surrogate activities that they are, and consequently never +attach enough importance to them to satisfy their need for the power process in that way. It only remains to point out that in +many cases a person’s way of earning a living is also a surrogate activity. Not a PURE surrogate activity, since part of the +motive for the activity is to gain the physical necessities and (for some people) social status and the luxuries that advertising +makes them want. But many people put into their work far more effort than is necessary to earn whatever money and status +they require, and this extra effort constitutes a surrogate activity. This extra effort, together with the emotional investment that +accompanies it, is one of the most potent forces acting toward the continual development and perfecting of the system, with +negative consequences for individual freedom (see paragraph 131). Especially, for the most creative scientists and engineers, +work tends to be largely a surrogate activity. This point is so important that it deserves a separate discussion, which we shall +give in a moment (paragraphs 87-92). +85. In this section we have explained how many people in modern society do satisfy their need for the power process to a +greater or lesser extent. But we think that for the majority of people the need for the power process is not fully satisfied. In the +first place, those who have an insatiable drive for status, or who get firmly “hooked” on a surrogate activity, or who identify +strongly enough with a movement or organization to satisfy their need for power in that way, are exceptional personalities. +Others are not fully satisfied with surrogate activities or by identification with an organization (see paragraphs 41, 64). In the +second place, too much control is imposed by the system through explicit regulation or through socialization, which results in a +deficiency of autonomy, and in frustration due to the impossibility of attaining certain goals and the necessity of restraining too +many impulses. +86. But even if most people in industrial-technological society were well satisfied, we (FC) would still be opposed to that form +of society, because (among other reasons) we consider it demeaning to fulfill one’s need for the power process through +surrogate activities or through identification with an organization, rather than through pursuit of real goals. + +The Motives of Scientists +87. Science and technology provide the most important examples of surrogate activities. Some scientists claim that they are +motivated by “curiosity” or by a desire to “benefit humanity.” But it is easy to see that neither of these can be the principal +motive of most scientists. As for “curiosity,” that notion is simply absurd. Most scientists work on highly specialized problems +that are not the object of any normal curiosity. For example, is an astronomer, a mathematician or an entomologist curious +about the properties of isopropyltrimethylmethane? Of course not. Only a chemist is curious about such a thing, and he is +curious about it only because chemistry is his surrogate activity. Is the chemist curious about the appropriate classification of a +new species of beetle? No. That question is of interest only to the entomologist, and he is interested in it only because +entomology is his surrogate activity. If the chemist and the entomologist had to exert themselves seriously to obtain the +physical necessities, and if that effort exercised their abilities in an interesting way but in some nonscientific pursuit, then they +wouldn’t give a damn about isopropyltrimethylmethane or the classification of beetles. Suppose that lack of funds for +postgraduate education had led the chemist to become an insurance broker instead of a chemist. In that case he would have +been very interested in insurance matters but would have cared nothing about isopropyltrimethylmethane. In any case it is not +normal to put into the satisfaction of mere curiosity the amount of time and effort that scientists put into their work. The +“curiosity” explanation for the scientists’ motive just doesn’t stand up. +88. The “benefit of humanity” explanation doesn’t work any better. Some scientific work has no conceivable relation to the +welfare of the human race most of archaeology or comparative linguistics for example. Some other areas of science present +obviously dangerous possibilities. Yet scientists in these areas are just as enthusiastic about their work as those who develop +vaccines or study air pollution. Consider the case of Dr. Edward Teller, who had an obvious emotional involvement in +promoting nuclear power plants. Did this involvement stem from a desire to benefit humanity? If so, then why didn’t Dr. Teller +get emotional about other “humanitarian” causes? If he was such a humanitarian then why did he help to develop the H-bomb? +As with many other scientific achievements, it is very much open to question whether nuclear power plants actually do benefit +humanity. Does the cheap electricity outweigh the accumulating waste and the risk of accidents? Dr. Teller saw only one side +of the question. Clearly his emotional involvement with nuclear power arose not from a desire to “benefit humanity” but from a +personal fulfillment he got from his work and from seeing it put to practical use. +89. The same is true of scientists generally. With possible rare exceptions, their motive is neither curiosity nor a desire to +benefit humanity but the need to go through the power process: to have a goal (a scientific problem to solve), to make an effort +(research) and to attain the goal (solution of the problem.) Science is a surrogate activity because scientists work mainly for the +fulfillment they get out of the work itself. +90. Of course, it’s not that simple. Other motives do play a role for many scientists. Money and status for example. Some +scientists may be persons of the type who have an insatiable drive for status (see paragraph 79) and this may provide much of +the motivation for their work. No doubt the majority of scientists, like the majority of the general population, are more or less +susceptible to advertising and marketing techniques and need money to satisfy their craving for goods and services. Thus +science is not a PURE surrogate activity. But it is in large part a surrogate activity. +91. Also, science and technology constitute a power mass movement, and many scientists gratify their need for power through +identification with this mass movement (see paragraph 83). +92. Thus science marches on blindly, without regard to the real welfare of the human race or to any other standard, obedient +only to the psychological needs of the scientists and of the government officials and corporation executives who provide the +funds for research. + +The Nature of Freedom +93. We are going to argue that industrial-technological society cannot be reformed in such a way as to prevent it from +progressively narrowing the sphere of human freedom. But, because “freedom” is a word that can be interpreted in many ways, +we must first make clear what kind of freedom we are concerned with. +94. By “freedom” we mean the opportunity to go through the power process, with real goals not the artificial goals of surrogate +activities, and without interference, manipulation or supervision from anyone, especially from any large organization. Freedom +means being in control (either as an individual or as a member of a SMALL group) of the life-and-death issues of one’s +existence; food, clothing, shelter and defense against whatever threats there may be in one’s environment. Freedom means +having power; not the power to control other people but the power to control the circumstances of one’s own life. One does not +have freedom if anyone else (especially a large organization) has power over one, no matter how benevolently, tolerantly and +permissively that power may be exercised. It is important not to confuse freedom with mere permissiveness (see paragraph 72). +95. It is said that we live in a free society because we have a certain number of constitutionally guaranteed rights. But these are +not as important as they seem. The degree of personal freedom that exists in a society is determined more by the economic and +technological structure of the society than by its laws or its form of government. [16] Most of the Indian nations of New +England were monarchies, and many of the cities of the Italian Renaissance were controlled by dictators. But in reading about +these societies one gets the impression that they allowed far more personal freedom than our society does. In part this was +because they lacked efficient mechanisms for enforcing the ruler’s will: There were no modern, well-organized police forces, +no rapid long-distance communications, no surveillance cameras, no dossiers of information about the lives of average citizens. +Hence it was relatively easy to evade control. +96. As for our constitutional rights, consider for example that of freedom of the press. We certainly don’t mean to knock that +right; it is very important tool for limiting concentration of political power and for keeping those who do have political power +in line by publicly exposing any misbehavior on their part. But freedom of the press is of very little use to the average citizen +as an individual. The mass media are mostly under the control of large organizations that are integrated into the system. +Anyone who has a little money can have something printed, or can distribute it on the Internet or in some such way, but what +he has to say will be swamped by the vast volume of material put out by the media, hence it will have no practical effect. To +make an impression on society with words is therefore almost impossible for most individuals and small groups. Take us (FC) +for example. If we had never done anything violent and had submitted the present writings to a publisher, they probably would +not have been accepted. If they had been accepted and published, they probably would not have attracted many readers, +because it’s more fun to watch the entertainment put out by the media than to read a sober essay. Even if these writings had had +many readers, most of these readers would soon have forgotten what they had read as their minds were flooded by the mass of +material to which the media expose them. In order to get our message before the public with some chance of making a lasting +impression, we’ve had to kill people. +97. Constitutional rights are useful up to a point, but they do not serve to guarantee much more than what might be called the +bourgeois conception of freedom. According to the bourgeois conception, a “free” man is essentially an element of a social +machine and has only a certain set of prescribed and delimited freedoms; freedoms that are designed to serve the needs of the +social machine more than those of the individual. Thus the bourgeois’s “free” man has economic freedom because that +promotes growth and progress; he has freedom of the press because public criticism restrains misbehavior by political leaders; +he has a right to a fair trial because imprisonment at the whim of the powerful would be bad for the system. This was clearly +the attitude of Simon Bolivar. To him, people deserved liberty only if they used it to promote progress (progress as conceived +by the bourgeois). Other bourgeois thinkers have taken a similar view of freedom as a mere means to collective ends. Chester +C. Tan, “Chinese Political Thought in the Twentieth Century,” page 202, explains the philosophy of the Kuomintang leader Hu +Han-min: “An individual is granted rights because he is a member of society and his community life requires such rights. By +community Hu meant the whole society of the nation.” And on page 259 Tan states that according to Carsum Chang (Chang +Chun-mai, head of the State Socialist Party in China) freedom had to be used in the interest of the state and of the people as a +whole. But what kind of freedom does one have if one can use it only as someone else prescribes? FC’s conception of freedom +is not that of Bolivar, Hu, Chang or other bourgeois theorists. The trouble with such theorists is that they have made the +development and application of social theories their surrogate activity. Consequently the theories are designed to serve the +needs of the theorists more than the needs of any people who may be unlucky enough to live in a society on which the theories +are imposed. +98. One more point to be made in this section: It should not be assumed that a person has enough freedom just because he +SAYS he has enough. Freedom is restricted in part by psychological controls of which people are unconscious, and moreover +many people’s ideas of what constitutes freedom are governed more by social convention than by their real needs. For +example, it’s likely that many leftists of the oversocialized type would say that most people, including themselves, are +socialized too little rather than too much, yet the oversocialized leftist pays a heavy psychological price for his high level of +socialization. + +Some Principles of History +99. Think of history as being the sum of two components: an erratic component that consists of unpredictable events that +follow no discernible pattern, and a regular component that consists of long-term historical trends. Here we are concerned with +the long-term trends. +100. FIRST PRINCIPLE. If a SMALL change is made that affects a long-term historical trend, then the effect of that change +will almost always be transitory -- the trend will soon revert to its original state. (Example: A reform movement designed to +clean up political corruption in a society rarely has more than a short-term effect; sooner or later the reformers relax and +corruption creeps back in. The level of political corruption in a given society tends to remain constant, or to change only slowly +with the evolution of the society. Normally, a political cleanup will be permanent only if accompanied by widespread social +changes; a SMALL change in the society won’t be enough.) If a small change in a long-term historical trend appears to be +permanent, it is only because the change acts in the direction in which the trend is already moving, so that the trend is not +altered by only pushed a step ahead. +101. The first principle is almost a tautology. If a trend were not stable with respect to small changes, it would wander at +random rather than following a definite direction; in other words it would not be a long-term trend at all. +102. SECOND PRINCIPLE. If a change is made that is sufficiently large to alter permanently a long-term historical trend, then +it will alter the society as a whole. In other words, a society is a system in which all parts are interrelated, and you can’t +permanently change any important part without changing all other parts as well. +103. THIRD PRINCIPLE. If a change is made that is large enough to alter permanently a long-term trend, then the +consequences for the society as a whole cannot be predicted in advance. (Unless various other societies have passed through +the same change and have all experienced the same consequences, in which case one can predict on empirical grounds that +another society that passes through the same change will be like to experience similar consequences.) +104. FOURTH PRINCIPLE. A new kind of society cannot be designed on paper. That is, you cannot plan out a new form of +society in advance, then set it up and expect it to function as it was designed to do. +105. The third and fourth principles result from the complexity of human societies. A change in human behavior will affect the +economy of a society and its physical environment; the economy will affect the environment and vice versa, and the changes in +the economy and the environment will affect human behavior in complex, unpredictable ways; and so forth. The network of +causes and effects is far too complex to be untangled and understood. +106. FIFTH PRINCIPLE. People do not consciously and rationally choose the form of their society. Societies develop through +processes of social evolution that are not under rational human control. +107. The fifth principle is a consequence of the other four. +108. To illustrate: By the first principle, generally speaking an attempt at social reform either acts in the direction in which the +society is developing anyway (so that it merely accelerates a change that would have occurred in any case) or else it has only a +transitory effect, so that the society soon slips back into its old groove. To make a lasting change in the direction of +development of any important aspect of a society, reform is insufficient and revolution is required. (A revolution does not +necessarily involve an armed uprising or the overthrow of a government.) By the second principle, a revolution never changes +only one aspect of a society, it changes the whole society; and by the third principle changes occur that were never expected or +desired by the revolutionaries. By the fourth principle, when revolutionaries or utopians set up a new kind of society, it never +works out as planned. +109. The American Revolution does not provide a counterexample. The American “Revolution” was not a revolution in our +sense of the word, but a war of independence followed by a rather far-reaching political reform. The Founding Fathers did not +change the direction of development of American society, nor did they aspire to do so. They only freed the development of +American society from the retarding effect of British rule. Their political reform did not change any basic trend, but only +pushed American political culture along its natural direction of development. British society, of which American society was +an offshoot, had been moving for a long time in the direction of representative democracy. And prior to the War of +Independence the Americans were already practicing a significant degree of representative democracy in the colonial +assemblies. The political system established by the Constitution was modeled on the British system and on the colonial +assemblies. With major alteration, to be sure -- there is no doubt that the Founding Fathers took a very important step. But it +was a step along the road that English-speaking world was already traveling. The proof is that Britain and all of its colonies +that were populated predominantly by people of British descent ended up with systems of representative democracy essentially +similar to that of the United States. If the Founding Fathers had lost their nerve and declined to sign the Declaration of +Independence, our way of life today would not have been significantly different. Maybe we would have had somewhat closer +ties to Britain, and would have had a Parliament and Prime Minister instead of a Congress and President. No big deal. Thus the +American Revolution provides not a counterexample to our principles but a good illustration of them. +110. Still, one has to use common sense in applying the principles. They are expressed in imprecise language that allows +latitude for interpretation, and exceptions to them can be found. So we present these principles not as inviolable laws but as +rules of thumb, or guides to thinking, that may provide a partial antidote to naive ideas about the future of society. The +principles should be borne constantly in mind, and whenever one reaches a conclusion that conflicts with them one should +carefully reexamine one’s thinking and retain the conclusion only if one has good, solid reasons for doing so. + +Industrial-Technological Society Cannot Be Reformed +111. The foregoing principles help to show how hopelessly difficult it would be to reform the industrial system in such a way +as to prevent it from progressively narrowing our sphere of freedom. There has been a consistent tendency, going back at least +to the Industrial Revolution for technology to strengthen the system at a high cost in individual freedom and local autonomy. +Hence any change designed to protect freedom from technology would be contrary to a fundamental trend in the development +of our society. Consequently, such a change either would be a transitory one -- soon swamped by the tide of history -- or, if +large enough to be permanent would alter the nature of our whole society. This by the first and second principles. Moreover, +since society would be altered in a way that could not be predicted in advance (third principle) there would be great risk. +Changes large enough to make a lasting difference in favor of freedom would not be initiated because it would be realized that +they would gravely disrupt the system. So any attempts at reform would be too timid to be effective. Even if changes large +enough to make a lasting difference were initiated, they would be retracted when their disruptive effects became apparent. +Thus, permanent changes in favor of freedom could be brought about only by persons prepared to accept radical, dangerous +and unpredictable alteration of the entire system. In other words by revolutionaries, not reformers. +112. People anxious to rescue freedom without sacrificing the supposed benefits of technology will suggest naive schemes for +some new form of society that would reconcile freedom with technology. Apart from the fact that people who make such +suggestions seldom propose any practical means by which the new form of society could be set up in the first place, it follows +from the fourth principle that even if the new form of society could be once established, it either would collapse or would give +results very different from those expected. +113. So even on very general grounds it seems highly improbable that any way of changing society could be found that would +reconcile freedom with modern technology. In the next few sections we will give more specific reasons for concluding that +freedom and technological progress are incompatible. + +Restriction of Freedom is Unavoidable in Industrial Society +114. As explained in paragraphs 65-67, 70-73, modern man is strapped down by a network of rules and regulations, and his +fate depends on the actions of persons remote from him whose decisions he cannot influence. This is not accidental or a result +of the arbitrariness of arrogant bureaucrats. It is necessary and inevitable in any technologically advanced society. The system +HAS TO regulate human behavior closely in order to function. At work people have to do what they are told to do, otherwise +production would be thrown into chaos. Bureaucracies HAVE TO be run according to rigid rules. To allow any substantial +personal discretion to lower-level bureaucrats would disrupt the system and lead to charges of unfairness due to differences in +the way individual bureaucrats exercised their discretion. It is true that some restrictions on our freedom could be eliminated, +but GENERALLY SPEAKING the regulation of our lives by large organizations is necessary for the functioning of industrialtechnological society. The result is a sense of powerlessness on the part of the average person. It may be, however, that formal +regulations will tend increasingly to be replaced by psychological tools that make us want to do what the system requires of us. +(Propaganda [14], educational techniques, “mental health” programs, etc.) +115. The system HAS TO force people to behave in ways that are increasingly remote from the natural pattern of human +behavior. For example, the system needs scientists, mathematicians and engineers. It can’t function without them. So heavy +pressure is put on children to excel in these fields. It isn’t natural for an adolescent human being to spend the bulk of his time +sitting at a desk absorbed in study. A normal adolescent wants to spend his time in active contact with the real world. Among +primitive peoples the things that children are trained to do tend to be in reasonable harmony with natural human impulses. +Among the American Indians, for example, boys were trained in active outdoor pursuits -- just the sort of thing that boys like. +But in our society children are pushed into studying technical subjects, which most do grudgingly. +[[116 not used.]] +117. In any technologically advanced society the individual’s fate MUST depend on decisions that he personally cannot +influence to any great extent. A technological society cannot be broken down into small, autonomous communities, because +production depends on the cooperation of very large numbers of people. When a decision affects, say, a million people, then +each of the affected individuals has, on the average, only a one-millionth share in making the decision. What usually happens +in practice is that decisions are made by public officials or corporation executives, or by technical specialists, but even when +the public votes on a decision the number of voters ordinarily is too large for the vote of any one individual to be significant. +[17] Thus most individuals are unable to influence measurably the major decisions that affect their lives. There is no +conceivable way to remedy this in a technologically advanced society. The system tries to “solve” this problem by using +propaganda to make people WANT the decisions that have been made for them, but even if this “solution” were completely +successful in making people feel better, it would be demeaning. +118. Conservatives and some others advocate more “local autonomy.” Local communities once did have autonomy, but such +autonomy becomes less and less possible as local communities become more enmeshed with and dependent on large-scale +systems like public utilities, computer networks, highway systems, the mass communications media, the modern health care +system. Also operating against autonomy is the fact that technology applied in one location often affects people at other +locations far way. Thus pesticide or chemical use near a creek may contaminate the water supply hundreds of miles +downstream, and the greenhouse effect affects the whole world. +119. The system does not and cannot exist to satisfy human needs. Instead, it is human behavior that has to be modified to fit +the needs of the system. This has nothing to do with the political or social ideology that may pretend to guide the technological +system. It is the fault of technology, because the system is guided not by ideology but by technical necessity. [18] Of course the +system does satisfy many human needs, but generally speaking it does this only to the extend that it is to the advantage of the +system to do it. It is the needs of the system that are paramount, not those of the human being. For example, the system +provides people with food because the system couldn’t function if everyone starved; it attends to people’s psychological needs +whenever it can CONVENIENTLY do so, because it couldn’t function if too many people became depressed or rebellious. But +the system, for good, solid, practical reasons, must exert constant pressure on people to mold their behavior to the needs of the +system. To much waste accumulating? The government, the media, the educational system, environmentalists, everyone +inundates us with a mass of propaganda about recycling. Need more technical personnel? A chorus of voices exhorts kids to +study science. No one stops to ask whether it is inhumane to force adolescents to spend the bulk of their time studying subjects +most of them hate. When skilled workers are put out of a job by technical advances and have to undergo “retraining,” no one +asks whether it is humiliating for them to be pushed around in this way. It is simply taken for granted that everyone must bow +to technical necessity. and for good reason: If human needs were put before technical necessity there would be economic +problems, unemployment, shortages or worse. The concept of “mental health” in our society is defined largely by the extent to +which an individual behaves in accord with the needs of the system and does so without showing signs of stress. +120. Efforts to make room for a sense of purpose and for autonomy within the system are no better than a joke. For example, +one company, instead of having each of its employees assemble only one section of a catalogue, had each assemble a whole +catalogue, and this was supposed to give them a sense of purpose and achievement. Some companies have tried to give their +employees more autonomy in their work, but for practical reasons this usually can be done only to a very limited extent, and in +any case employees are never given autonomy as to ultimate goals -- their “autonomous” efforts can never be directed toward +goals that they select personally, but only toward their employer’s goals, such as the survival and growth of the company. Any +company would soon go out of business if it permitted its employees to act otherwise. Similarly, in any enterprise within a +socialist system, workers must direct their efforts toward the goals of the enterprise, otherwise the enterprise will not serve its +purpose as part of the system. Once again, for purely technical reasons it is not possible for most individuals or small groups to +have much autonomy in industrial society. Even the small-business owner commonly has only limited autonomy. Apart from +the necessity of government regulation, he is restricted by the fact that he must fit into the economic system and conform to its +requirements. For instance, when someone develops a new technology, the small- business person often has to use that +technology whether he wants to or not, in order to remain competitive. + +The ‘Bad’ Parts of Technology Cannot Be Separated from the ‘Good’ Parts +121. A further reason why industrial society cannot be reformed in favor of freedom is that modern technology is a unified +system in which all parts are dependent on one another. You can’t get rid of the “bad” parts of technology and retain only the +“good” parts. Take modern medicine, for example. Progress in medical science depends on progress in chemistry, physics, +biology, computer science and other fields. Advanced medical treatments require expensive, high-tech equipment that can be +made available only by a technologically progressive, economically rich society. Clearly you can’t have much Progress in +medicine without the whole technological system and everything that goes with it. +122. Even if medical progress could be maintained without the rest of the technological system, it would by itself bring certain +evils. Suppose for example that a cure for diabetes is discovered. People with a genetic tendency to diabetes will then be able to +survive and reproduce as well as anyone else. Natural selection against genes for diabetes will cease and such genes will spread +throughout the population. (This may be occurring to some extent already, since diabetes, while not curable, can be controlled +through use of insulin.) The same thing will happen with many other diseases susceptibility to which is affected by genetic +degradation of the population. The only solution will be some sort of eugenics program or extensive genetic engineering of +human beings, so that man in the future will no longer be a creation of nature, or of chance, or of God (depending on your +religious or philosophical opinions), but a manufactured product. +123. If you think that big government interferes in your life too much NOW, just wait till the government starts regulating the +genetic constitution of your children. Such regulation will inevitably follow the introduction of genetic engineering of human +beings, because the consequences of unregulated genetic engineering would be disastrous. [19] +124. The usual response to such concerns is to talk about “medical ethics.” But a code of ethics would not serve to protect +freedom in the face of medical progress; it would only make matters worse. A code of ethics applicable to genetic engineering +would be in effect a means of regulating the genetic constitution of human beings. Somebody (probably the upper-middle class, +mostly) would decide that such and such applications of genetic engineering were “ethical”. and others were not, so that in +effect they would be imposing their own values on the genetic constitution of the population at large. Even if a code of ethics +were chosen on a completely democratic basis, the majority would be imposing their own values on any minorities who might +have a different idea of what constituted an “ethical” use of genetic engineering. The only code of ethics that would truly +protect freedom would be one that prohibited ANY genetic engineering of human beings, and you can be sure that no such +code will ever be applied in a technological society. No code that reduced genetic engineering to a minor role could stand up +for long, because the temptation presented by the immense power of biotechnology would be irresistible, especially since to the +majority of people many of its applications will seem obviously and unequivocally good (eliminating physical and mental +diseases, giving people the abilities they need to get along in today’s world). Inevitably, genetic engineering will be used +extensively, but only in ways consistent with the needs of the industrial-technological system. [20] + +Technology is a More Powerful Social Force than the Aspiration for Freedom +125. It is not possible to make a LASTING compromise between technology and freedom, because technology is by far the +more powerful social force and continually encroaches on freedom through REPEATED compromises. Imagine the case of +two neighbors, each of whom at the outset owns the same amount of land, but one of whom is more powerful than the other. +The powerful one demands a piece of the other’s land. The weak one refuses. The powerful one says, “OK, let’s compromise. +Give me half of what I asked.” The weak one has little choice but to give in. Some time later the powerful neighbor demands +another piece of land, again there is a compromise, and so forth. By forcing a long series of compromises on the weaker man, +the powerful one eventually gets all of his land. So it goes in the conflict between technology and freedom. +126. Let us explain why technology is a more powerful social force than the aspiration for freedom. +127. A technological advance that appears not to threaten freedom often turns out to threaten it very seriously later on. For +example, consider motorized transport. A walking man formerly could go where he pleased, go at his own pace without +observing any traffic regulations, and was independent of technological support-systems. When motor vehicles were +introduced they appeared to increase man’s freedom. They took no freedom away from the walking man, no one had to have an +automobile if he didn’t want one, and anyone who did choose to buy an automobile could travel much faster and farther than a +walking man. But the introduction of motorized transport soon changed society in such a way as to restrict greatly man’s +freedom of locomotion. When automobiles became numerous, it became necessary to regulate their use extensively. In a car, +especially in densely populated areas, one cannot just go where one likes at one’s own pace one’s movement is governed by the +flow of traffic and by various traffic laws. One is tied down by various obligations: license requirements, driver test, renewing +registration, insurance, maintenance required for safety, monthly payments on purchase price. Moreover, the use of motorized +transport is no longer optional. Since the introduction of motorized transport the arrangement of our cities has changed in such +a way that the majority of people no longer live within walking distance of their place of employment, shopping areas and +recreational opportunities, so that they HAVE TO depend on the automobile for transportation. Or else they must use public +transportation, in which case they have even less control over their own movement than when driving a car. Even the walker’s +freedom is now greatly restricted. In the city he continually has to stop to wait for traffic lights that are designed mainly to +serve auto traffic. In the country, motor traffic makes it dangerous and unpleasant to walk along the highway. (Note this +important point that we have just illustrated with the case of motorized transport: When a new item of technology is introduced +as an option that an individual can accept or not as he chooses, it does not necessarily REMAIN optional. In many cases the +new technology changes society in such a way that people eventually find themselves FORCED to use it.) +128. While technological progress AS A WHOLE continually narrows our sphere of freedom, each new technical advance +CONSIDERED BY ITSELF appears to be desirable. Electricity, indoor plumbing, rapid long-distance communications ... how +could one argue against any of these things, or against any other of the innumerable technical advances that have made modern +society? It would have been absurd to resist the introduction of the telephone, for example. It offered many advantages and no +disadvantages. Yet, as we explained in paragraphs 59-76, all these technical advances taken together have created a world in +which the average man’s fate is no longer in his own hands or in the hands of his neighbors and friends, but in those of +politicians, corporation executives and remote, anonymous technicians and bureaucrats whom he as an individual has no power +to influence. [21] The same process will continue in the future. Take genetic engineering, for example. Few people will resist +the introduction of a genetic technique that eliminates a hereditary disease. It does no apparent harm and prevents much +suffering. Yet a large number of genetic improvements taken together will make the human being into an engineered product +rather than a free creation of chance (or of God, or whatever, depending on your religious beliefs). +129. Another reason why technology is such a powerful social force is that, within the context of a given society, technological +progress marches in only one direction; it can never be reversed. Once a technical innovation has been introduced, people +usually become dependent on it, so that they can never again do without it, unless it is replaced by some still more advanced +innovation. Not only do people become dependent as individuals on a new item of technology, but, even more, the system as a +whole becomes dependent on it. (Imagine what would happen to the system today if computers, for example, were eliminated.) +Thus the system can move in only one direction, toward greater technologization. Technology repeatedly forces freedom to +take a step back, but technology can never take a step back -- short of the overthrow of the whole technological system. +130. Technology advances with great rapidity and threatens freedom at many different points at the same time (crowding, rules +and regulations, increasing dependence of individuals on large organizations, propaganda and other psychological techniques, +genetic engineering, invasion of privacy through surveillance devices and computers, etc.). To hold back any ONE of the +threats to freedom would require a long and difficult social struggle. Those who want to protect freedom are overwhelmed by +the sheer number of new attacks and the rapidity with which they develop, hence they become apathetic and no longer resist. +To fight each of the threats separately would be futile. Success can be hoped for only by fighting the technological system as a +whole; but that is revolution, not reform. +131. Technicians (we use this term in its broad sense to describe all those who perform a specialized task that requires training) +tend to be so involved in their work (their surrogate activity) that when a conflict arises between their technical work and +freedom, they almost always decide in favor of their technical work. This is obvious in the case of scientists, but it also appears +elsewhere: Educators humanitarian groups, conservation organizations do not hesitate to use propaganda or other psychological +techniques to help them achieve their laudable ends. Corporations and government agencies, when they find it useful, do not +hesitate to collect information about individuals without regard to their privacy. Law enforcement agencies are frequently +inconvenienced by the constitutional rights of suspects and often of completely innocent persons, and they do whatever they +can do legally (or sometimes illegally) to restrict or circumvent those rights. Most of these educators, government officials and +law officers believe in freedom, privacy and constitutional rights, but when these conflict with their work, they usually feel that +their work is more important. +132. It is well known that people generally work better and more persistently when striving for a reward than when attempting +to avoid a punishment or negative outcome. Scientists and other technicians are motivated mainly by the rewards they get +through their work. But those who oppose technological invasions of freedom are working to avoid a negative outcome, +consequently there are few who work persistently and well at this discouraging task. If reformers ever achieved a signal victory +that seemed to set up a solid barrier against further erosion of freedom through technical progress, most would tend to relax and +turn their attention to more agreeable pursuits. But the scientists would remain busy in their laboratories, and technology as it +progresses would find ways, in spite of any barriers, to exert more and more control over individuals and make them always +more dependent on the system. +133. No social arrangements, whether laws, institutions, customs or ethical codes, can provide permanent protection against +technology. History shows that all social arrangements are transitory; they all change or break down eventually. But +technological advances are permanent within the context of a given civilization. Suppose for example that it were possible to +arrive at some social arrangements that would prevent genetic engineering from being applied to human beings, or prevent it +from being applied in such a way as to threaten freedom and dignity. Still, the technology would remain waiting. Sooner or +later the social arrangement would break down. Probably sooner, given the pace of change in our society. Then genetic +engineering would begin to invade our sphere of freedom. and this invasion would be irreversible (short of a breakdown of +technological civilization itself). Any illusions about achieving anything permanent through social arrangements should be +dispelled by what is currently happening with environmental legislation. A few years ago its seemed that there were secure +legal barriers preventing at least SOME of the worst forms of environmental degradation. A change in the political wind, and +those barriers begin to crumble. +134. For all of the foregoing reasons, technology is a more powerful social force than the aspiration for freedom. But this +statement requires an important qualification. It appears that during the next several decades the industrial-technological +system will be undergoing severe stresses due to economic and environmental problems, and especially due to problems of +human behavior (alienation, rebellion, hostility, a variety of social and psychological difficulties). We hope that the stresses +through which the system is likely to pass will cause it to break down, or at least will weaken it sufficiently so that a revolution +against it becomes possible. If such a revolution occurs and is successful, then at that particular moment the aspiration for +freedom will have proved more powerful than technology. +135. In paragraph 125 we used an analogy of a weak neighbor who is left destitute by a strong neighbor who takes all his land +by forcing on him a series of compromises. But suppose now that the strong neighbor gets sick, so that he is unable to defend +himself. The weak neighbor can force the strong one to give him his land back, or he can kill him. If he lets the strong man +survive and only forces him to give the land back, he is a fool, because when the strong man gets well he will again take all the +land for himself. The only sensible alternative for the weaker man is to kill the strong one while he has the chance. In the same +way, while the industrial system is sick we must destroy it. If we compromise with it and let it recover from its sickness, it will +eventually wipe out all of our freedom. + +Simpler Social Problems Have Proved Intractable +136. If anyone still imagines that it would be possible to reform the system in such a way as to protect freedom from +technology, let him consider how clumsily and for the most part unsuccessfully our society has dealt with other social problems +that are far more simple and straightforward. Among other things, the system has failed to stop environmental degradation, +political corruption, drug trafficking or domestic abuse. +137. Take our environmental problems, for example. Here the conflict of values is straightforward: economic expedience now +versus saving some of our natural resources for our grandchildren. [22] But on this subject we get only a lot of blather and +obfuscation from the people who have power, and nothing like a clear, consistent line of action, and we keep on piling up +environmental problems that our grandchildren will have to live with. Attempts to resolve the environmental issue consist of +struggles and compromises between different factions, some of which are ascendant at one moment, others at another moment. +The line of struggle changes with the shifting currents of public opinion. This is not a rational process, nor is it one that is +likely to lead to a timely and successful solution to the problem. Major social problems, if they get “solved” at all, are rarely or +never solved through any rational, comprehensive plan. They just work themselves out through a process in which various +competing groups pursuing their own (usually short-term) self-interest [23] arrive (mainly by luck) at some more or less stable +modus vivendi. In fact, the principles we formulated in paragraphs 100-106 make it seem doubtful that rational long-term +social planning can EVER be successful. +138. Thus it is clear that the human race has at best a very limited capacity for solving even relatively straightforward social +problems. How then is it going to solve the far more difficult and subtle problem of reconciling freedom with technology? +Technology presents clear-cut material advantages, whereas freedom is an abstraction that means different things to different +people. and its loss is easily obscured by propaganda and fancy talk. +139. And note this important difference: It is conceivable that our environmental problems (for example) may some day be +settled through a rational, comprehensive plan, but if this happens it will be only because it is in the long-term interest of the +system to solve these problems. But it is NOT in the interest of the system to preserve freedom or small-group autonomy. On +the contrary, it is in the interest of the system to bring human behavior under control to the greatest possible extent. [24] Thus, +while practical considerations may eventually force the system to take a rational, prudent approach to environmental problems, +equally practical considerations will force the system to regulate human behavior ever more closely (preferably by indirect +means that will disguise the encroachment on freedom). This isn’t just our opinion. Eminent social scientists (e.g. James Q. +Wilson) have stressed the importance of “socializing” people more effectively. + +Revolution is Easier than Reform +140. We hope we have convinced the reader that the system cannot be reformed in such a way as to reconcile freedom with +technology. The only way out is to dispense with the industrial-technological system altogether. This implies revolution, not +necessarily an armed uprising, but certainly a radical and fundamental change in the nature of society. 141. People tend to +assume that because a revolution involves a much greater change than reform does, it is more difficult to bring about than +reform is. Actually, under certain circumstances revolution is much easier than reform. The reason is that a revolutionary +movement can inspire an intensity of commitment that a reform movement cannot inspire. A reform movement merely offers +to solve a particular social problem. A revolutionary movement offers to solve all problems at one stroke and create a whole +new world; it provides the kind of ideal for which people will take great risks and make great sacrifices. For this reasons it +would be much easier to overthrow the whole technological system than to put effective, permanent restraints on the +development or application of any one segment of technology, such as genetic engineering, for example. Not many people will +devote themselves with single-minded passion to imposing and maintaining restraints on genetic engineering, but under +suitable conditions large numbers of people may devote themselves passionately to a revolution against the industrialtechnological system. As we noted in paragraph 132, reformers seeking to limit certain aspects of technology would be +working to avoid a negative outcome. But revolutionaries work to gain a powerful reward -- fulfillment of their revolutionary +vision and therefore work harder and more persistently than reformers do. +142. Reform is always restrained by the fear of painful consequences if changes go too far. But once a revolutionary fever has +taken hold of a society, people are willing to undergo unlimited hardships for the sake of their revolution. This was clearly +shown in the French and Russian Revolutions. It may be that in such cases only a minority of the population is really +committed to the revolution, but this minority is sufficiently large and active so that it becomes the dominant force in society. +We will have more to say about revolution in paragraphs 180-205. + +Control of Human Behavior +143. Since the beginning of civilization, organized societies have had to put pressures on human beings of the sake of the +functioning of the social organism. The kinds of pressures vary greatly from one society to another. Some of the pressures are +physical (poor diet, excessive labor, environmental pollution), some are psychological (noise, crowding, forcing human +behavior into the mold that society requires). In the past, human nature has been approximately constant, or at any rate has +varied only within certain bounds. Consequently, societies have been able to push people only up to certain limits. When the +limit of human endurance has been passed, things start going wrong: rebellion, or crime, or corruption, or evasion of work, or +depression and other mental problems, or an elevated death rate, or a declining birth rate or something else, so that either the +society breaks down, or its functioning becomes too inefficient and it is (quickly or gradually, through conquest, attrition or +evolution) replaced by some more efficient form of society. [25] +144. Thus human nature has in the past put certain limits on the development of societies. People could be pushed only so far +and no farther. But today this may be changing, because modern technology is developing ways of modifying human beings. +145. Imagine a society that subjects people to conditions that make them terribly unhappy, then gives them drugs to take away +their unhappiness. Science fiction? It is already happening to some extent in our own society. It is well known that the rate of +clinical depression has been greatly increasing in recent decades. We believe that this is due to disruption of the power process, +as explained in paragraphs 59-76. But even if we are wrong, the increasing rate of depression is certainly the result of SOME +conditions that exist in today’s society. Instead of removing the conditions that make people depressed, modern society gives +them antidepressant drugs. In effect, antidepressants are a means of modifying an individual’s internal state in such a way as to +enable him to tolerate social conditions that he would otherwise find intolerable. (Yes, we know that depression is often of +purely genetic origin. We are referring here to those cases in which environment plays the predominant role.) +146. Drugs that affect the mind are only one example of the new methods of controlling human behavior that modern society is +developing. Let us look at some of the other methods. +147. To start with, there are the techniques of surveillance. Hidden video cameras are now used in most stores and in many +other places, computers are used to collect and process vast amounts of information about individuals. Information so obtained +greatly increases the effectiveness of physical coercion (i.e., law enforcement). [26] Then there are the methods of propaganda, +for which the mass communication media provide effective vehicles. Efficient techniques have been developed for winning +elections, selling products, influencing public opinion. The entertainment industry serves as an important psychological tool of +the system, possibly even when it is dishing out large amounts of sex and violence. Entertainment provides modern man with +an essential means of escape. While absorbed in television, videos, etc., he can forget stress, anxiety, frustration, +dissatisfaction. Many primitive peoples, when they don’t have work to do, are quite content to sit for hours at a time doing +nothing at all, because they are at peace with themselves and their world. But most modern people must be constantly occupied +or entertained, otherwise they get “bored,” i.e., they get fidgety, uneasy, irritable. +148. Other techniques strike deeper than the foregoing. Education is no longer a simple affair of paddling a kid’s behind when +he doesn’t know his lessons and patting him on the head when he does know them. It is becoming a scientific technique for +controlling the child’s development. Sylvan Learning Centers, for example, have had great success in motivating children to +study, and psychological techniques are also used with more or less success in many conventional schools. “Parenting” +techniques that are taught to parents are designed to make children accept fundamental values of the system and behave in +ways that the system finds desirable. “Mental health” programs, “intervention” techniques, psychotherapy and so forth are +ostensibly designed to benefit individuals, but in practice they usually serve as methods for inducing individuals to think and +behave as the system requires. (There is no contradiction here; an individual whose attitudes or behavior bring him into conflict +with the system is up against a force that is too powerful for him to conquer or escape from, hence he is likely to suffer from +stress, frustration, defeat. His path will be much easier if he thinks and behaves as the system requires. In that sense the system +is acting for the benefit of the individual when it brainwashes him into conformity.) Child abuse in its gross and obvious forms +is disapproved in most if not all cultures. Tormenting a child for a trivial reason or no reason at all is something that appalls +almost everyone. But many psychologists interpret the concept of abuse much more broadly. Is spanking, when used as part of +a rational and consistent system of discipline, a form of abuse? The question will ultimately be decided by whether or not +spanking tends to produce behavior that makes a person fit in well with the existing system of society. In practice, the word +“abuse” tends to be interpreted to include any method of child-rearing that produces behavior inconvenient for the system. +Thus, when they go beyond the prevention of obvious, senseless cruelty, programs for preventing “child abuse” are directed +toward the control of human behavior on behalf of the system. +149. Presumably, research will continue to increase the effectiveness of psychological techniques for controlling human +behavior. But we think it is unlikely that psychological techniques alone will be sufficient to adjust human beings to the kind of +society that technology is creating. Biological methods probably will have to be used. We have already mentioned the use of +drugs in this connection. Neurology may provide other avenues for modifying the human mind. Genetic engineering of human +beings is already beginning to occur in the form of “gene therapy,” and there is no reason to assume that such methods will not +eventually be used to modify those aspects of the body that affect mental functioning. +150. As we mentioned in paragraph 134, industrial society seems likely to be entering a period of severe stress, due in part to +problems of human behavior and in part to economic and environmental problems. And a considerable proportion of the +system’s economic and environmental problems result from the way human beings behave. Alienation, low self-esteem, +depression, hostility, rebellion; children who won’t study, youth gangs, illegal drug use, rape, child abuse, other crimes, unsafe +sex, teen pregnancy, population growth, political corruption, race hatred, ethnic rivalry, bitter ideological conflict (e.g., prochoice vs. pro-life), political extremism, terrorism, sabotage, anti-government groups, hate groups. All these threaten the very +survival of the system. The system will therefore be FORCED to use every practical means of controlling human behavior. +151. The social disruption that we see today is certainly not the result of mere chance. It can only be a result of the conditions +of life that the system imposes on people. (We have argued that the most important of these conditions is disruption of the +power process.) If the systems succeeds in imposing sufficient control over human behavior to assure its own survival, a new +watershed in human history will have been passed. Whereas formerly the limits of human endurance have imposed limits on +the development of societies (as we explained in Paragraphs 143, 144), industrial-technological society will be able to pass +those limits by modifying human beings, whether by psychological methods or biological methods or both. In the future, social +systems will not be adjusted to suit the needs of human beings. Instead, human being will be adjusted to suit the needs of the +system. [27] +152. generally speaking, technological control over human behavior will probably not be introduced with a totalitarian +intention or even through a conscious desire to restrict human freedom. [28] Each new step in the assertion of control over the +human mind will be taken as a rational response to a problem that faces society, such as curing alcoholism, reducing the crime +rate or inducing young people to study science and engineering. In many cases there will be a humanitarian justification. For +example, when a psychiatrist prescribes an anti-depressant for a depressed patient, he is clearly doing that individual a favor. It +would be inhumane to withhold the drug from someone who needs it. When Parents send their children to Sylvan Learning +Centers to have them manipulated into becoming enthusiastic about their studies, they do so from concern for their children’s +welfare. It may be that some of these parents wish that one didn’t have to have specialized training to get a job and that their +kid didn’t have to be brainwashed into becoming a computer nerd. But what can they do? They can’t change society, and their +child may be unemployable if he doesn’t have certain skills. So they send him to Sylvan. +153. Thus control over human behavior will be introduced not by a calculated decision of the authorities but through a process +of social evolution (RAPID evolution, however). The process will be impossible to resist, because each advance, considered by +itself, will appear to be beneficial, or at least the evil involved in making the advance will appear to be beneficial, or at least the +evil involved in making the advance will seem to be less than that which would result from not making it (see paragraph 127). +Propaganda for example is used for many good purposes, such as discouraging child abuse or race hatred. [14] Sex education is +obviously useful, yet the effect of sex education (to the extent that it is successful) is to take the shaping of sexual attitudes +away from the family and put it into the hands of the state as represented by the public school system. +154. Suppose a biological trait is discovered that increases the likelihood that a child will grow up to be a criminal, and suppose +some sort of gene therapy can remove this trait. [29] Of course most parents whose children possess the trait will have them +undergo the therapy. It would be inhumane to do otherwise, since the child would probably have a miserable life if he grew up +to be a criminal. But many or most primitive societies have a low crime rate in comparison with that of our society, even +though they have neither high-tech methods of child-rearing nor harsh systems of punishment. Since there is no reason to +suppose that more modern men than primitive men have innate predatory tendencies, the high crime rate of our society must be +due to the pressures that modern conditions put on people, to which many cannot or will not adjust. Thus a treatment designed +to remove potential criminal tendencies is at least in part a way of re-engineering people so that they suit the requirements of +the system. +155. Our society tends to regard as a “sickness” any mode of thought or behavior that is inconvenient for the system, and this is +plausible because when an individual doesn’t fit into the system it causes pain to the individual as well as problems for the +system. Thus the manipulation of an individual to adjust him to the system is seen as a “cure” for a “sickness” and therefore as +good. +156. In paragraph 127 we pointed out that if the use of a new item of technology is INITIALLY optional, it does not +necessarily REMAIN optional, because the new technology tends to change society in such a way that it becomes difficult or +impossible for an individual to function without using that technology. This applies also to the technology of human behavior. +In a world in which most children are put through a program to make them enthusiastic about studying, a parent will almost be +forced to put his kid through such a program, because if he does not, then the kid will grow up to be, comparatively speaking, +an ignoramus and therefore unemployable. Or suppose a biological treatment is discovered that, without undesirable sideeffects, will greatly reduce the psychological stress from which so many people suffer in our society. If large numbers of +people choose to undergo the treatment, then the general level of stress in society will be reduced, so that it will be possible for +the system to increase the stress-producing pressures. In fact, something like this seems to have happened already with one of +our society’s most important psychological tools for enabling people to reduce (or at least temporarily escape from) stress, +namely, mass entertainment (see paragraph 147). Our use of mass entertainment is “optional”: No law requires us to watch +television, listen to the radio, read magazines. Yet mass entertainment is a means of escape and stress-reduction on which most +of us have become dependent. Everyone complains about the trashiness of television, but almost everyone watches it. A few +have kicked the TV habit, but it would be a rare person who could get along today without using ANY form of mass +entertainment. (Yet until quite recently in human history most people got along very nicely with no other entertainment than +that which each local community created for itself.) Without the entertainment industry the system probably would not have +been able to get away with putting as much stress-producing pressure on us as it does. +157. Assuming that industrial society survives, it is likely that technology will eventually acquire something approaching +complete control over human behavior. It has been established beyond any rational doubt that human thought and behavior +have a largely biological basis. As experimenters have demonstrated, feelings such as hunger, pleasure, anger and fear can be +turned on and off by electrical stimulation of appropriate parts of the brain. Memories can be destroyed by damaging parts of +the brain or they can be brought to the surface by electrical stimulation. Hallucinations can be induced or moods changed by +drugs. There may or may not be an immaterial human soul, but if there is one it clearly is less powerful that the biological +mechanisms of human behavior. For if that were not the case then researchers would not be able so easily to manipulate human +feelings and behavior with drugs and electrical currents. +158. It presumably would be impractical for all people to have electrodes inserted in their heads so that they could be +controlled by the authorities. But the fact that human thoughts and feelings are so open to biological intervention shows that the +problem of controlling human behavior is mainly a technical problem; a problem of neurons, hormones and complex +molecules; the kind of problem that is accessible to scientific attack. Given the outstanding record of our society in solving +technical problems, it is overwhelmingly probable that great advances will be made in the control of human behavior. +159. Will public resistance prevent the introduction of technological control of human behavior? It certainly would if an +attempt were made to introduce such control all at once. But since technological control will be introduced through a long +sequence of small advances, there will be no rational and effective public resistance. (See paragraphs 127, 132, 153.) +160. To those who think that all this sounds like science fiction, we point out that yesterday’s science fiction is today’s fact. +The Industrial Revolution has radically altered man’s environment and way of life, and it is only to be expected that as +technology is increasingly applied to the human body and mind, man himself will be altered as radically as his environment +and way of life have been. + +Human Race at a Crossroads + +161. But we have gotten ahead of our story. It is one thing to develop in the laboratory a series of psychological or biological +techniques for manipulating human behavior and quite another to integrate these techniques into a functioning social system. +The latter problem is the more difficult of the two. For example, while the techniques of educational psychology doubtless +work quite well in the “lab schools” where they are developed, it is not necessarily easy to apply them effectively throughout +our educational system. We all know what many of our schools are like. The teachers are too busy taking knives and guns +away from the kids to subject them to the latest techniques for making them into computer nerds. Thus, in spite of all its +technical advances relating to human behavior, the system to date has not been impressively successful in controlling human +beings. The people whose behavior is fairly well under the control of the system are those of the type that might be called +“bourgeois.” But there are growing numbers of people who in one way or another are rebels against the system: welfare +leaches, youth gangs, cultists. Satanists, nazis, radical environmentalists, militiamen, etc. +162. The system is currently engaged in a desperate struggle to overcome certain problems that threaten its survival, among +which the problems of human behavior are the most important. If the system succeeds in acquiring sufficient control over +human behavior quickly enough, it will probably survive. Otherwise it will break down. We think the issue will most likely be +resolved within the next several decades, say 40 to 100 years. +163. Suppose the system survives the crisis of the next several decades. By that time it will have to have solved, or at least +brought under control, the principal problems that confront it, in particular that of “socializing” human beings; that is, making +people sufficiently docile so that heir behavior no longer threatens the system. That being accomplished, it does not appear that +there would be any further obstacle to the development of technology, and it would presumably advance toward its logical +conclusion, which is complete control over everything on Earth, including human beings and all other important organisms. +The system may become a unitary, monolithic organization, or it may be more or less fragmented and consist of a number of +organizations coexisting in a relationship that includes elements of both cooperation and competition, just as today the +government, the corporations and other large organizations both cooperate and compete with one another. Human freedom +mostly will have vanished, because individuals and small groups will be impotent vis-a-vis large organizations armed with +super-technology and an arsenal of advanced psychological and biological tools for manipulating human beings, besides +instruments of surveillance and physical coercion. Only a small number of people will have any real power, and even these +probably will have only very limited freedom, because their behavior too will be regulated; just as today our politicians and +corporation executives can retain their positions of power only as long as their behavior remains within certain fairly narrow +limits. +164. Don’t imagine that the systems will stop developing further techniques for controlling human beings and nature once the +crisis of the next few decades is over and increasing control is no longer necessary for the system’s survival. On the contrary, +once the hard times are over the system will increase its control over people and nature more rapidly, because it will no longer +be hampered by difficulties of the kind that it is currently experiencing. Survival is not the principal motive for extending +control. As we explained in paragraphs 87-90, technicians and scientists carry on their work largely as a surrogate activity; that +is, they satisfy their need for power by solving technical problems. They will continue to do this with unabated enthusiasm, and +among the most interesting and challenging problems for them to solve will be those of understanding the human body and +mind and intervening in their development. For the “good of humanity,” of course. +165. But suppose on the other hand that the stresses of the coming decades prove to be too much for the system. If the system +breaks down there may be a period of chaos, a “time of troubles” such as those that history has recorded at various epochs in +the past. It is impossible to predict what would emerge from such a time of troubles, but at any rate the human race would be +given a new chance. The greatest danger is that industrial society may begin to reconstitute itself within the first few years after +the breakdown. Certainly there will be many people (power-hungry types especially) who will be anxious to get the factories +running again. +166. Therefore two tasks confront those who hate the servitude to which the industrial system is reducing the human race. First, +we must work to heighten the social stresses within the system so as to increase the likelihood that it will break down or be +weakened sufficiently so that a revolution against it becomes possible. Second, it is necessary to develop and propagate an +ideology that opposes technology and the industrial society if and when the system becomes sufficiently weakened. And such +an ideology will help to assure that, if and when industrial society breaks down, its remnants will be smashed beyond repair, so +that the system cannot be reconstituted. The factories should be destroyed, technical books burned, etc. + +Human Suffering + +167. The industrial system will not break down purely as a result of revolutionary action. It will not be vulnerable to +revolutionary attack unless its own internal problems of development lead it into very serious difficulties. So if the system +breaks down it will do so either spontaneously, or through a process that is in part spontaneous but helped along by +revolutionaries. If the breakdown is sudden, many people will die, since the world’s population has become so over-grown that +it cannot even feed itself any longer without advanced technology. Even if the breakdown is gradual enough so that reduction +of the population can occur more through lowering of the birth rate than through elevation of the death rate, the process of deindustrialization probably will be very chaotic and involve much suffering. It is naive to think it likely that technology can be +phased out in a smoothly managed, orderly way, especially since the technophiles will fight stubbornly at every step. Is it +therefore cruel to work for the breakdown of the system? Maybe, but maybe not. In the first place, revolutionaries will not be +able to break the system down unless it is already in enough trouble so that there would be a good chance of its eventually +breaking down by itself anyway; and the bigger the system grows, the more disastrous the consequences of its breakdown will +be; so it may be that revolutionaries, by hastening the onset of the breakdown, will be reducing the extent of the disaster. +168. In the second place, one has to balance struggle and death against the loss of freedom and dignity. To many of us, freedom +and dignity are more important than a long life or avoidance of physical pain. Besides, we all have to die some time, and it may +be better to die fighting for survival, or for a cause, than to live a long but empty and purposeless life. +169. In the third place, it is not at all certain that survival of the system will lead to less suffering than breakdown of the system +would. The system has already caused, and is continuing to cause, immense suffering all over the world. Ancient cultures, that +for hundreds of years gave people a satisfactory relationship with each other and with their environment, have been shattered +by contact with industrial society, and the result has been a whole catalogue of economic, environmental, social and +psychological problems. One of the effects of the intrusion of industrial society has been that over much of the world +traditional controls on population have been thrown out of balance. Hence the population explosion, with all that that implies. +Then there is the psychological suffering that is widespread throughout the supposedly fortunate countries of the West (see +paragraphs 44, 45). No one knows what will happen as a result of ozone depletion, the greenhouse effect and other +environmental problems that cannot yet be foreseen. And, as nuclear proliferation has shown, new technology cannot be kept +out of the hands of dictators and irresponsible Third World nations. Would you like to speculate about what Iraq or North +Korea will do with genetic engineering? +170. “Oh!” say the technophiles, “Science is going to fix all that! We will conquer famine, eliminate psychological suffering, +make everybody healthy and happy!” Yeah, sure. That’s what they said 200 years ago. The Industrial Revolution was supposed +to eliminate poverty, make everybody happy, etc. The actual result has been quite different. The technophiles are hopelessly +naive (or self-deceiving) in their understanding of social problems. They are unaware of (or choose to ignore) the fact that +when large changes, even seemingly beneficial ones, are introduced into a society, they lead to a long sequence of other +changes, most of which are impossible to predict (paragraph 103). The result is disruption of the society. So it is very probable +that in their attempts to end poverty and disease, engineer docile, happy personalities and so forth, the technophiles will create +social systems that are terribly troubled, even more so than the present once. For example, the scientists boast that they will end +famine by creating new, genetically engineered food plants. But this will allow the human population to keep expanding +indefinitely, and it is well known that crowding leads to increased stress and aggression. This is merely one example of the +PREDICTABLE problems that will arise. We emphasize that, as past experience has shown, technical progress will lead to +other new problems that CANNOT be predicted in advance (paragraph 103). In fact, ever since the Industrial Revolution, +technology has been creating new problems for society far more rapidly than it has been solving old ones. Thus it will take a +long and difficult period of trial and error for the technophiles to work the bugs out of their Brave New World (if they every +do). In the meantime there will be great suffering. So it is not at all clear that the survival of industrial society would involve +less suffering than the breakdown of that society would. Technology has gotten the human race into a fix from which there is +not likely to be any easy escape. + +The Future +171. But suppose now that industrial society does survive the next several decades and that the bugs do eventually get worked +out of the system, so that it functions smoothly. What kind of system will it be? We will consider several possibilities. +172. First let us postulate that the computer scientists succeed in developing intelligent machines that can do all things better +than human beings can do them. In that case presumably all work will be done by vast, highly organized systems of machines +and no human effort will be necessary. Either of two cases might occur. The machines might be permitted to make all of their +own decisions without human oversight, or else human control over the machines might be retained. +173. If the machines are permitted to make all their own decisions, we can’t make any conjectures as to the results, because it is +impossible to guess how such machines might behave. We only point out that the fate of the human race would be at the mercy +of the machines. It might be argued that the human race would never be foolish enough to hand over all power to the machines. +But we are suggesting neither that the human race would voluntarily turn power over to the machines nor that the machines +would willfully seize power. What we do suggest is that the human race might easily permit itself to drift into a position of +such dependence on the machines that it would have no practical choice but to accept all of the machines’ decisions. As society +and the problems that face it become more and more complex and as machines become more and more intelligent, people will +let machines make more and more of their decisions for them, simply because machine-made decisions will bring better results +than man-made ones. Eventually a stage may be reached at which the decisions necessary to keep the system running will be so +complex that human beings will be incapable of making them intelligently. At that stage the machines will be in effective +control. People won’t be able to just turn the machine off, because they will be so dependent on them that turning them off +would amount to suicide. +174. On the other hand it is possible that human control over the machines may be retained. In that case the average man may +have control over certain private machines of his own, such as his car or his personal computer, but control over large systems +of machines will be in the hands of a tiny elite -- just as it is today, but with two differences. Due to improved techniques the +elite will have greater control over the masses; and because human work will no longer be necessary the masses will be +superfluous, a useless burden on the system. If the elite is ruthless they may simply decide to exterminate the mass of +humanity. If they are humane they may use propaganda or other psychological or biological techniques to reduce the birth rate +until the mass of humanity becomes extinct, leaving the world to the elite. Or, if the elite consists of soft-hearted liberals, they +may decide to play the role of good shepherds to the rest of the human race. They will see to it that everyone’s physical needs +are satisfied, that all children are raised under psychologically hygienic conditions, that everyone has a wholesome hobby to +keep him busy, and that anyone who may become dissatisfied undergoes “treatment” to cure his “problem.” Of course, life will +be so purposeless that people will have to be biologically or psychologically engineered either to remove their need for the +power process or to make them “sublimate” their drive for power into some harmless hobby. These engineered human beings +may be happy in such a society, but they most certainly will not be free. They will have been reduced to the status of domestic +animals. +175. But suppose now that the computer scientists do not succeed in developing artificial intelligence, so that human work +remains necessary. Even so, machines will take care of more and more of the simpler tasks so that there will be an increasing +surplus of human workers at the lower levels of ability. (We see this happening already. There are many people who find it +difficult or impossible to get work, because for intellectual or psychological reasons they cannot acquire the level of training +necessary to make themselves useful in the present system.) On those who are employed, ever-increasing demands will be +placed: They will need more and more training, more and more ability, and will have to be ever more reliable, conforming and +docile, because they will be more and more like cells of a giant organism. Their tasks will be increasingly specialized, so that +their work will be, in a sense, out of touch with the real world, being concentrated on one tiny slice of reality. The system will +have to use any means that it can, whether psychological or biological, to engineer people to be docile, to have the abilities that +the system requires and to “sublimate” their drive for power into some specialized task. But the statement that the people of +such a society will have to be docile may require qualification. The society may find competitiveness useful, provided that +ways are found of directing competitiveness into channels that serve the needs of the system. We can imagine a future society +in which there is endless competition for positions of prestige and power. But no more than a very few people will ever reach +the top, where the only real power is (see end of paragraph 163). Very repellent is a society in which a person can satisfy his +need for power only by pushing large numbers of other people out of the way and depriving them of THEIR opportunity for +power. +176. One can envision scenarios that incorporate aspects of more than one of the possibilities that we have just discussed. For +instance, it may be that machines will take over most of the work that is of real, practical importance, but that human beings +will be kept busy by being given relatively unimportant work. It has been suggested, for example, that a great development of +the service industries might provide work for human beings. Thus people would spent their time shining each other’s shoes, +driving each other around in taxicabs, making handicrafts for one another, waiting on each other’s tables, etc. This seems to us +a thoroughly contemptible way for the human race to end up, and we doubt that many people would find fulfilling lives in such +pointless busy-work. They would seek other, dangerous outlets (drugs, crime, “cults,” hate groups) unless they were +biologically or psychologically engineered to adapt them to such a way of life. +177. Needless to say, the scenarios outlined above do not exhaust all the possibilities. They only indicate the kinds of outcomes +that seem to us most likely. But we can envision no plausible scenarios that are any more palatable than the ones we’ve just +described. It is overwhelmingly probable that if the industrial-technological system survives the next 40 to 100 years, it will by +that time have developed certain general characteristics: Individuals (at least those of the “bourgeois” type, who are integrated +into the system and make it run, and who therefore have all the power) will be more dependent than ever on large +organizations; they will be more “socialized” than ever and their physical and mental qualities to a significant extent (possibly +to a very great extent) will be those that are engineered into them rather than being the results of chance (or of God’s will, or +whatever); and whatever may be left of wild nature will be reduced to remnants preserved for scientific study and kept under +the supervision and management of scientists (hence it will no longer be truly wild). In the long run (say a few centuries from +now) it is likely that neither the human race nor any other important organisms will exist as we know them today, because once +you start modifying organisms through genetic engineering there is no reason to stop at any particular point, so that the +modifications will probably continue until man and other organisms have been utterly transformed. +178. Whatever else may be the case, it is certain that technology is creating for human beings a new physical and social +environment radically different from the spectrum of environments to which natural selection has adapted the human race +physically and psychologically. If man is not adjusted to this new environment by being artificially re-engineered, then he will +be adapted to it through a long and painful process of natural selection. The former is far more likely than the latter. +179. It would be better to dump the whole stinking system and take the consequences. + +Strategy +180. The technophiles are taking us all on an utterly reckless ride into the unknown. Many people understand something of +what technological progress is doing to us yet take a passive attitude toward it because they think it is inevitable. But we (FC) +don’t think it is inevitable. We think it can be stopped, and we will give here some indications of how to go about stopping it. +181. As we stated in paragraph 166, the two main tasks for the present are to promote social stress and instability in industrial +society and to develop and propagate an ideology that opposes technology and the industrial system. When the system becomes +sufficiently stressed and unstable, a revolution against technology may be possible. The pattern would be similar to that of the +French and Russian Revolutions. French society and Russian society, for several decades prior to their respective revolutions, +showed increasing signs of stress and weakness. Meanwhile, ideologies were being developed that offered a new world view +that was quite different from the old one. In the Russian case, revolutionaries were actively working to undermine the old +order. Then, when the old system was put under sufficient additional stress (by financial crisis in France, by military defeat in +Russia) it was swept away by revolution. What we propose is something along the same lines. +182. It will be objected that the French and Russian Revolutions were failures. But most revolutions have two goals. One is to +destroy an old form of society and the other is to set up the new form of society envisioned by the revolutionaries. The French +and Russian revolutionaries failed (fortunately!) to create the new kind of society of which they dreamed, but they were quite +successful in destroying the old society. We have no illusions about the feasibility of creating a new, ideal form of society. Our +goal is only to destroy the existing form of society. +183. But an ideology, in order to gain enthusiastic support, must have a positive ideal as well as a negative one; it must be FOR +something as well as AGAINST something. The positive ideal that we propose is Nature. That is, WILD nature: those aspects +of the functioning of the Earth and its living things that are independent of human management and free of human interference +and control. And with wild nature we include human nature, by which we mean those aspects of the functioning of the human +individual that are not subject to regulation by organized society but are products of chance, or free will, or God (depending on +your religious or philosophical opinions). +184. Nature makes a perfect counter-ideal to technology for several reasons. Nature (that which is outside the power of the +system) is the opposite of technology (which seeks to expand indefinitely the power of the system). Most people will agree that +nature is beautiful; certainly it has tremendous popular appeal. The radical environmentalists ALREADY hold an ideology that +exalts nature and opposes technology. [30] It is not necessary for the sake of nature to set up some chimerical utopia or any +new kind of social order. Nature takes care of itself: It was a spontaneous creation that existed long before any human society, +and for countless centuries many different kinds of human societies coexisted with nature without doing it an excessive amount +of damage. Only with the Industrial Revolution did the effect of human society on nature become really devastating. To relieve +the pressure on nature it is not necessary to create a special kind of social system, it is only necessary to get rid of industrial +society. Granted, this will not solve all problems. Industrial society has already done tremendous damage to nature and it will +take a very long time for the scars to heal. Besides, even pre-industrial societies can do significant damage to nature. +Nevertheless, getting rid of industrial society will accomplish a great deal. It will relieve the worst of the pressure on nature so +that the scars can begin to heal. It will remove the capacity of organized society to keep increasing its control over nature +(including human nature). Whatever kind of society may exist after the demise of the industrial system, it is certain that most +people will live close to nature, because in the absence of advanced technology there is no other way that people CAN live. To +feed themselves they must be peasants or herdsmen or fishermen or hunters, etc. And, generally speaking, local autonomy +should tend to increase, because lack of advanced technology and rapid communications will limit the capacity of governments +or other large organizations to control local communities. +185. As for the negative consequences of eliminating industrial society -- well, you can’t eat your cake and have it too. To gain +one thing you have to sacrifice another. +186. Most people hate psychological conflict. For this reason they avoid doing any serious thinking about difficult social +issues, and they like to have such issues presented to them in simple, black-and-white terms: THIS is all good and THAT is all +bad. The revolutionary ideology should therefore be developed on two levels. +187. On the more sophisticated level the ideology should address itself to people who are intelligent, thoughtful and rational. +The object should be to create a core of people who will be opposed to the industrial system on a rational, thought-out basis, +with full appreciation of the problems and ambiguities involved, and of the price that has to be paid for getting rid of the +system. It is particularly important to attract people of this type, as they are capable people and will be instrumental in +influencing others. These people should be addressed on as rational a level as possible. Facts should never intentionally be +distorted and intemperate language should be avoided. This does not mean that no appeal can be made to the emotions, but in +making such appeal care should be taken to avoid misrepresenting the truth or doing anything else that would destroy the +intellectual respectability of the ideology. +188. On a second level, the ideology should be propagated in a simplified form that will enable the unthinking majority to see +the conflict of technology vs. nature in unambiguous terms. But even on this second level the ideology should not be expressed +in language that is so cheap, intemperate or irrational that it alienates people of the thoughtful and rational type. Cheap, +intemperate propaganda sometimes achieves impressive short-term gains, but it will be more advantageous in the long run to +keep the loyalty of a small number of intelligently committed people than to arouse the passions of an unthinking, fickle mob +who will change their attitude as soon as someone comes along with a better propaganda gimmick. However, propaganda of +the rabble-rousing type may be necessary when the system is nearing the point of collapse and there is a final struggle between +rival ideologies to determine which will become dominant when the old world-view goes under. +189. Prior to that final struggle, the revolutionaries should not expect to have a majority of people on their side. History is +made by active, determined minorities, not by the majority, which seldom has a clear and consistent idea of what it really +wants. Until the time comes for the final push toward revolution [31], the task of revolutionaries will be less to win the shallow +support of the majority than to build a small core of deeply committed people. As for the majority, it will be enough to make +them aware of the existence of the new ideology and remind them of it frequently; though of course it will be desirable to get +majority support to the extent that this can be done without weakening the core of seriously committed people. +190. Any kind of social conflict helps to destabilize the system, but one should be careful about what kind of conflict one +encourages. The line of conflict should be drawn between the mass of the people and the power-holding elite of industrial +society (politicians, scientists, upper-level business executives, government officials, etc.). It should NOT be drawn between +the revolutionaries and the mass of the people. For example, it would be bad strategy for the revolutionaries to condemn +Americans for their habits of consumption. Instead, the average American should be portrayed as a victim of the advertising +and marketing industry, which has suckered him into buying a lot of junk that he doesn’t need and that is very poor +compensation for his lost freedom. Either approach is consistent with the facts. It is merely a matter of attitude whether you +blame the advertising industry for manipulating the public or blame the public for allowing itself to be manipulated. As a +matter of strategy one should generally avoid blaming the public. +191. One should think twice before encouraging any other social conflict than that between the power-holding elite (which +wields technology) and the general public (over which technology exerts its power). For one thing, other conflicts tend to +distract attention from the important conflicts (between power-elite and ordinary people, between technology and nature); for +another thing, other conflicts may actually tend to encourage technologization, because each side in such a conflict wants to +use technological power to gain advantages over its adversary. This is clearly seen in rivalries between nations. It also appears +in ethnic conflicts within nations. For example, in America many black leaders are anxious to gain power for African +Americans by placing back individuals in the technological power-elite. They want there to be many black government +officials, scientists, corporation executives and so forth. In this way they are helping to absorb the African American subculture +into the technological system. Generally speaking, one should encourage only those social conflicts that can be fitted into the +framework of the conflicts of power-elite vs. ordinary people, technology vs. nature. +192. But the way to discourage ethnic conflict is NOT through militant advocacy of minority rights (see paragraphs 21, 29). +Instead, the revolutionaries should emphasize that although minorities do suffer more or less disadvantage, this disadvantage is +of peripheral significance. Our real enemy is the industrial- technological system, and in the struggle against the system, ethnic +distinctions are of no importance. +193. The kind of revolution we have in mind will not necessarily involve an armed uprising against any government. It may or +may not involve physical violence, but it will not be a POLITICAL revolution. Its focus will be on technology and economics, +not politics. [32] +194. Probably the revolutionaries should even AVOID assuming political power, whether by legal or illegal means, until the +industrial system is stressed to the danger point and has proved itself to be a failure in the eyes of most people. Suppose for +example that some “green” party should win control of the United States Congress in an election. In order to avoid betraying or +watering down their own ideology they would have to take vigorous measures to turn economic growth into economic +shrinkage. To the average man the results would appear disastrous: There would be massive unemployment, shortages of +commodities, etc Even if the grosser ill effects could be avoided through superhumanly skillful management, still people would +have to begin giving up the luxuries to which they have become addicted. Dissatisfaction would grow, the “green” party would +be voted out of office and the revolutionaries would have suffered a severe setback. For this reason the revolutionaries should +not try to acquire political power until the system has gotten itself into such a mess that any hardships will be seen as resulting +from the failures of the industrial system itself and not from the policies of the revolutionaries. The revolution against +technology will probably have to be a revolution by outsiders, a revolution from below and not from above. +195. The revolution must be international and worldwide. It cannot be carried out on a nation-by-nation basis. Whenever it is +suggested that the United States, for example, should cut back on technological progress or economic growth, people get +hysterical and start screaming that if we fall behind in technology the Japanese will get ahead of us. Holy robots! The world +will fly off its orbit if the Japanese ever sell more cars than we do! (Nationalism is a great promoter of technology.) More +reasonably, it is argued that if the relatively democratic nations of the world fall behind in technology while nasty, dictatorial +nations like China, Vietnam and North Korea continue to progress, eventually the dictators may come to dominate the world. +That is why the industrial system should be attacked in all nations simultaneously, to the extent that this may be possible. True, +there is no assurance that the industrial system can be destroyed at approximately the same time all over the world, and it is +even conceivable that the attempt to overthrow the system could lead instead to the domination of the system by dictators. That +is a risk that has to be taken. And it is worth taking, since the difference between a “democratic” industrial system and one +controlled by dictators is small compared with the difference between an industrial system and a non-industrial one. [33] It +might even be argued that an industrial system controlled by dictators would be preferable, because dictator-controlled systems +usually have proved inefficient, hence they are presumably more likely to break down. Look at Cuba. +196. Revolutionaries might consider favoring measures that tend to bind the world economy into a unified whole. Free trade +agreements like NAFTA and GATT are probably harmful to the environment in the short run, but in the long run they may +perhaps be advantageous because they foster economic interdependence between nations. It will be easier to destroy the +industrial system on a worldwide basis if the world economy is so unified that its breakdown in any one major nation will lead +to its breakdown in all industrialized nations. +197. Some people take the line that modern man has too much power, too much control over nature; they argue for a more +passive attitude on the part of the human race. At best these people are expressing themselves unclearly, because they fail to +distinguish between power for LARGE ORGANIZATIONS and power for INDIVIDUALS and SMALL GROUPS. It is a +mistake to argue for powerlessness and passivity, because people NEED power. Modern man as a collective entity -- that is, +the industrial system -- has immense power over nature, and we (FC) regard this as evil. But modern INDIVIDUALS and +SMALL GROUPS OF INDIVIDUALS have far less power than primitive man ever did. Generally speaking, the vast power of +“modern man” over nature is exercised not by individuals or small groups but by large organizations. To the extent that the +average modern INDIVIDUAL can wield the power of technology, he is permitted to do so only within narrow limits and only +under the supervision and control of the system. (You need a license for everything and with the license come rules and +regulations.) The individual has only those technological powers with which the system chooses to provide him. His +PERSONAL power over nature is slight. +198. Primitive INDIVIDUALS and SMALL GROUPS actually had considerable power over nature; or maybe it would be +better to say power WITHIN nature. When primitive man needed food he knew how to find and prepare edible roots, how to +track game and take it with homemade weapons. He knew how to protect himself from heat cold, rain, dangerous animals, etc. +But primitive man did relatively little damage to nature because the COLLECTIVE power of primitive society was negligible +compared to the COLLECTIVE power of industrial society. +199. Instead of arguing for powerlessness and passivity, one should argue that the power of the INDUSTRIAL SYSTEM +should be broken, and that this will greatly INCREASE the power and freedom of INDIVIDUALS and SMALL GROUPS. +200. Until the industrial system has been thoroughly wrecked, the destruction of that system must be the revolutionaries’ +ONLY goal. Other goals would distract attention and energy from the main goal. More importantly if the revolutionaries +permit themselves to have any other goal than the destruction of technology, they will be tempted to use technology as a tool +for reaching that other goal. If they give in to that temptation, they will fall right back into the technological trap, because +modern technology is a unified, tightly organized system, so that, in order to retain SOME technology, one finds oneself +obliged to retain MOST technology, hence one ends up sacrificing only token amounts of technology. +201. Suppose for example that the revolutionaries took “social justice” as a goal. Human nature being what it is, social justice +would not come about spontaneously; it would have to be enforced. In order to enforce it the revolutionaries would have to +retain central organization and control. For that they would need rapid long-distance transportation and communication, and +therefore all the technology needed to support the transportation and communication systems. To feed and clothe poor people +they would have to use agricultural and manufacturing technology. And so forth. So that the attempt to insure social justice +would force them to retain most parts of the technological system. Not that we have anything against social justice, but it must +not be allowed to interfere with the effort to get rid of the technological system. +202. It would be hopeless for revolutionaries to try to attack the system without using SOME modern technology. If nothing +else they must use the communications media to spread their message. But they should use modern technology for only ONE +purpose: to attack the technological system. +203. Imagine an alcoholic sitting with a barrel of wine in front of him. Suppose he starts saying to himself, “Wine isn’t bad for +you if used in moderation. Why, they say small amounts of wine are even good for you! It won’t do me any harm if I take just +one little drink....” Well you know what is going to happen. Never forget that the human race with technology is just like an +alcoholic with a barrel of wine. +204. Revolutionaries should have as many children as they can. There is strong scientific evidence that social attitudes are to a +significant extent inherited. No one suggests that a social attitude is a direct outcome of a person’s genetic constitution, but it +appears that personality traits are partly inherited and that certain personality traits tend, within the context of our society, to +make a person more likely to hold this or that social attitude. Objections to these findings have been raised, but the objections +are feeble and seem to be ideologically motivated. In any event, no one denies that children tend on the average to hold social +attitudes similar to those of their parents. From our point of view it doesn’t matter all that much whether the attitudes are +passed on genetically or through childhood training. In either case they ARE passed on. +205. The trouble is that many of the people who are inclined to rebel against the industrial system are also concerned about the +population problems, hence they are apt to have few or no children. In this way they may be handing the world over to the sort +of people who support or at least accept the industrial system. To insure the strength of the next generation of revolutionaries +the present generation should reproduce itself abundantly. In doing so they will be worsening the population problem only +slightly. And the important problem is to get rid of the industrial system, because once the industrial system is gone the world’s +population necessarily will decrease (see paragraph 167); whereas, if the industrial system survives, it will continue developing +new techniques of food production that may enable the world’s population to keep increasing almost indefinitely. +206. With regard to revolutionary strategy, the only points on which we absolutely insist are that the single overriding goal +must be the elimination of modern technology, and that no other goal can be allowed to compete with this one. For the rest, +revolutionaries should take an empirical approach. If experience indicates that some of the recommendations made in the +foregoing paragraphs are not going to give good results, then those recommendations should be discarded. + +Two Kinds of Technology +207. An argument likely to be raised against our proposed revolution is that it is bound to fail, because (it is claimed) +throughout history technology has always progressed, never regressed, hence technological regression is impossible. But this +claim is false. +208. We distinguish between two kinds of technology, which we will call small-scale technology and organization-dependent +technology. Small-scale technology is technology that can be used by small-scale communities without outside assistance. +Organization-dependent technology is technology that depends on large-scale social organization. We are aware of no +significant cases of regression in small-scale technology. But organization-dependent technology DOES regress when the +social organization on which it depends breaks down. Example: When the Roman Empire fell apart the Romans’ small-scale +technology survived because any clever village craftsman could build, for instance, a water wheel, any skilled smith could +make steel by Roman methods, and so forth. But the Romans’ organization- dependent technology DID regress. Their +aqueducts fell into disrepair and were never rebuilt. Their techniques of road construction were lost. The Roman system of +urban sanitation was forgotten, so that not until rather recent times did the sanitation of European cities equal that of Ancient +Rome. +209. The reason why technology has seemed always to progress is that, until perhaps a century or two before the Industrial +Revolution, most technology was small-scale technology. But most of the technology developed since the Industrial +Revolution is organization-dependent technology. Take the refrigerator for example. Without factory-made parts or the +facilities of a postindustrial machine shop it would be virtually impossible for a handful of local craftsmen to build a +refrigerator. If by some miracle they did succeed in building one it would be useless to them without a reliable source of +electric power. So they would have to dam a stream and build a generator. Generators require large amounts of copper wire. +Imagine trying to make that wire without modern machinery. And where would they get a gas suitable for refrigeration? It +would be much easier to build an icehouse or preserve food by drying or picking, as was done before the invention of the +refrigerator. +210. So it is clear that if the industrial system were once thoroughly broken down, refrigeration technology would quickly be +lost. The same is true of other organization-dependent technology. And once this technology had been lost for a generation or +so it would take centuries to rebuild it, just as it took centuries to build it the first time around. Surviving technical books would +be few and scattered. An industrial society, if built from scratch without outside help, can only be built in a series of stages: +You need tools to make tools to make tools to make tools ... . A long process of economic development and progress in social +organization is required. And, even in the absence of an ideology opposed to technology, there is no reason to believe that +anyone would be interested in rebuilding industrial society. The enthusiasm for “progress” is a phenomenon peculiar to the +modern form of society, and it seems not to have existed prior to the 17th century or thereabouts. +211. In the late Middle Ages there were four main civilizations that were about equally “advanced”: Europe, the Islamic world, +India, and the Far East (China, Japan, Korea). Three of those civilizations remained more or less stable, and only Europe +became dynamic. No one knows why Europe became dynamic at that time; historians have their theories but these are only +speculation. At any rate, it is clear that rapid development toward a technological form of society occurs only under special +conditions. So there is no reason to assume that a long-lasting technological regression cannot be brought about. +212. Would society EVENTUALLY develop again toward an industrial-technological form? Maybe, but there is no use in +worrying about it, since we can’t predict or control events 500 or 1,000 years in the future. Those problems must be dealt with +by the people who will live at that time. + +The Danger of Leftism +213. Because of their need for rebellion and for membership in a movement, leftists or persons of similar psychological type +often are unattracted to a rebellious or activist movement whose goals and membership are not initially leftist. The resulting +influx of leftish types can easily turn a non-leftist movement into a leftist one, so that leftist goals replace or distort the original +goals of the movement. +214. To avoid this, a movement that exalts nature and opposes technology must take a resolutely anti-leftist stance and must +avoid all collaboration with leftists. Leftism is in the long run inconsistent with wild nature, with human freedom and with the +elimination of modern technology. Leftism is collectivist; it seeks to bind together the entire world (both nature and the human +race) into a unified whole. But this implies management of nature and of human life by organized society, and it requires +advanced technology. You can’t have a united world without rapid transportation and communication, you can’t make all +people love one another without sophisticated psychological techniques, you can’t have a “planned society” without the +necessary technological base. Above all, leftism is driven by the need for power, and the leftist seeks power on a collective +basis, through identification with a mass movement or an organization. Leftism is unlikely ever to give up technology, because +technology is too valuable a source of collective power. +215. The anarchist [34] too seeks power, but he seeks it on an individual or small-group basis; he wants individuals and small +groups to be able to control the circumstances of their own lives. He opposes technology because it makes small groups +dependent on large organizations. +216. Some leftists may seem to oppose technology, but they will oppose it only so long as they are outsiders and the +technological system is controlled by non-leftists. If leftism ever becomes dominant in society, so that the technological system +becomes a tool in the hands of leftists, they will enthusiastically use it and promote its growth. In doing this they will be +repeating a pattern that leftism has shown again and again in the past. When the Bolsheviks in Russia were outsiders, they +vigorously opposed censorship and the secret police, they advocated self-determination for ethnic minorities, and so forth; but +as soon as they came into power themselves, they imposed a tighter censorship and created a more ruthless secret police than +any that had existed under the tsars, and they oppressed ethnic minorities at least as much as the tsars had done. In the United +States, a couple of decades ago when leftists were a minority in our universities, leftist professors were vigorous proponents of +academic freedom, but today, in those of our universities where leftists have become dominant, they have shown themselves +ready to take away from everyone else’s academic freedom. (This is “political correctness.”) The same will happen with leftists +and technology: They will use it to oppress everyone else if they ever get it under their own control. +217. In earlier revolutions, leftists of the most power-hungry type, repeatedly, have first cooperated with non-leftist +revolutionaries, as well as with leftists of a more libertarian inclination, and later have double-crossed them to seize power for +themselves. Robespierre did this in the French Revolution, the Bolsheviks did it in the Russian Revolution, the communists did +it in Spain in 1938 and Castro and his followers did it in Cuba. Given the past history of leftism, it would be utterly foolish for +non-leftist revolutionaries today to collaborate with leftists. +218. Various thinkers have pointed out that leftism is a kind of religion. Leftism is not a religion in the strict sense because +leftist doctrine does not postulate the existence of any supernatural being. But, for the leftist, leftism plays a psychological role +much like that which religion plays for some people. The leftist NEEDS to believe in leftism; it plays a vital role in his +psychological economy. His beliefs are not easily modified by logic or facts. He has a deep conviction that leftism is morally +Right with a capital R, and that he has not only a right but a duty to impose leftist morality on everyone. (However, many of +the people we are referring to as “leftists” do not think of themselves as leftists and would not describe their system of beliefs +as leftism. We use the term “leftism” because we don’t know of any better words to designate the spectrum of related creeds +that includes the feminist, gay rights, political correctness, etc., movements, and because these movements have a strong +affinity with the old left. See paragraphs 227-230.) +219. Leftism is a totalitarian force. Wherever leftism is in a position of power it tends to invade every private corner and force +every thought into a leftist mold. In part this is because of the quasi-religious character of leftism; everything contrary to leftist +beliefs represents Sin. More importantly, leftism is a totalitarian force because of the leftists’ drive for power. The leftist seeks +to satisfy his need for power through identification with a social movement and he tries to go through the power process by +helping to pursue and attain the goals of the movement (see paragraph 83). But no matter how far the movement has gone in +attaining its goals the leftist is never satisfied, because his activism is a surrogate activity (see paragraph 41). That is, the +leftist’s real motive is not to attain the ostensible goals of leftism; in reality he is motivated by the sense of power he gets from +struggling for and then reaching a social goal. [35] Consequently the leftist is never satisfied with the goals he has already +attained; his need for the power process leads him always to pursue some new goal. The leftist wants equal opportunities for +minorities. When that is attained he insists on statistical equality of achievement by minorities. And as long as anyone harbors +in some corner of his mind a negative attitude toward some minority, the leftist has to re-educated him. And ethnic minorities +are not enough; no one can be allowed to have a negative attitude toward homosexuals, disabled people, fat people, old people, +ugly people, and on and on and on. It’s not enough that the public should be informed about the hazards of smoking; a warning +has to be stamped on every package of cigarettes. Then cigarette advertising has to be restricted if not banned. The activists +will never be satisfied until tobacco is outlawed, and after that it will be alcohol, then junk food, etc. Activists have fought +gross child abuse, which is reasonable. But now they want to stop all spanking. When they have done that they will want to ban +something else they consider unwholesome, then another thing and then another. They will never be satisfied until they have +complete control over all child rearing practices. And then they will move on to another cause. +220. Suppose you asked leftists to make a list of ALL the things that were wrong with society, and then suppose you instituted +EVERY social change that they demanded. It is safe to say that within a couple of years the majority of leftists would find +something new to complain about, some new social “evil” to correct because, once again, the leftist is motivated less by +distress at society’s ills than by the need to satisfy his drive for power by imposing his solutions on society. +221. Because of the restrictions placed on their thoughts and behavior by their high level of socialization, many leftists of the +over-socialized type cannot pursue power in the ways that other people do. For them the drive for power has only one morally +acceptable outlet, and that is in the struggle to impose their morality on everyone. +222. Leftists, especially those of the oversocialized type, are True Believers in the sense of Eric Hoffer’s book, “The True +Believer.” But not all True Believers are of the same psychological type as leftists. Presumably a true-believing nazi, for +instance, is very different psychologically from a true-believing leftist. Because of their capacity for single-minded devotion to +a cause, True Believers are a useful, perhaps a necessary, ingredient of any revolutionary movement. This presents a problem +with which we must admit we don’t know how to deal. We aren’t sure how to harness the energies of the True Believer to a +revolution against technology. At present all we can say is that no True Believer will make a safe recruit to the revolution +unless his commitment is exclusively to the destruction of technology. If he is committed also to another ideal, he may want to +use technology as a tool for pursuing that other ideal (see paragraphs 220, 221). +223. Some readers may say, “This stuff about leftism is a lot of crap. I know John and Jane who are leftish types and they don’t +have all these totalitarian tendencies.” It’s quite true that many leftists, possibly even a numerical majority, are decent people +who sincerely believe in tolerating others’ values (up to a point) and wouldn’t want to use high-handed methods to reach their +social goals. Our remarks about leftism are not meant to apply to every individual leftist but to describe the general character of +leftism as a movement. And the general character of a movement is not necessarily determined by the numerical proportions of +the various kinds of people involved in the movement. +224. The people who rise to positions of power in leftist movements tend to be leftists of the most power-hungry type, because +power-hungry people are those who strive hardest to get into positions of power. Once the power-hungry types have captured +control of the movement, there are many leftists of a gentler breed who inwardly disapprove of many of the actions of the +leaders, but cannot bring themselves to oppose them. They NEED their faith in the movement, and because they cannot give up +this faith they go along with the leaders. True, SOME leftists do have the guts to oppose the totalitarian tendencies that emerge, +but they generally lose, because the power-hungry types are better organized, are more ruthless and Machiavellian and have +taken care to build themselves a strong power base. +225. These phenomena appeared clearly in Russia and other countries that were taken over by leftists. Similarly, before the +breakdown of communism in the, USSR, leftish types in the West would, seldom criticize that country. If prodded they would +admit that the USSR did many wrong things, but then they would try to find excuses for the communists and begin talking +about the faults of the West. They always opposed Western military resistance to communist aggression. Leftish types all over +the world vigorously protested the U.S. military action in Vietnam, but when the USSR invaded Afghanistan they did nothing. +Not that they approved of the Soviet actions; but because of their leftist faith, they just couldn’t bear to put themselves in +opposition to communism. Today, in those of our universities where “political correctness” has become dominant, there are +probably many leftish types who privately disapprove of the suppression of academic freedom, but they go along with it +anyway. +226. Thus the fact that many individual leftists are personally mild and fairly tolerant people by no means prevents leftism as a +whole form having a totalitarian tendency. +227. Our discussion of leftism has a serious weakness. It is still far from clear what we mean by the word “leftist.” There +doesn’t seem to be much we can do about this. Today leftism is fragmented into a whole spectrum of activist movements. Yet +not all activist movements are leftist, and some activist movements (e.g., radical environmentalism) seem to include both +personalities of the leftist type and personalities of thoroughly un-leftist types who ought to know better than to collaborate +with leftists. Varieties of leftists fade out gradually into varieties of non-leftists and we ourselves would often be hard-pressed +to decide whether a given individual is or is not a leftist. To the extent that it is defined at all, our conception of leftism is +defined by the discussion of it that we have given in this article, and we can only advise the reader to use his own judgment in +deciding who is a leftist. +228. But it will be helpful to list some criteria for diagnosing leftism. These criteria cannot be applied in a cut and dried +manner. Some individuals may meet some of the criteria without being leftists, some leftists may not meet any of the criteria. +Again, you just have to use your judgment. +229. The leftist is oriented toward large-scale collectivism. He emphasizes the duty of the individual to serve society and the +duty of society to take care of the individual. He has a negative attitude toward individualism. He often takes a moralistic tone. +He tends to be for gun control, for sex education and other psychologically “enlightened” educational methods, for social +planning, for affirmative action, for multi- culturalism. He tends to identify with victims. He tends to be against competition +and against violence, but he often finds excuses for those leftists who do commit violence. He is fond of using the common +catch-phrases of the left, like “racism,” “sexism,” “homophobia,” “capitalism,” “imperialism,” “neocolonialism,” “genocide,” +“social change,” “social justice,” “social responsibility.” Maybe the best diagnostic trait of the leftist is his tendency to +sympathize with the following movements: feminism, gay rights, ethnic rights, disability rights, animal rights, political +correctness. Anyone who strongly sympathizes with ALL of these movements is almost certainly a leftist. [36] +230. The more dangerous leftists, that is, those who are most power-hungry, are often characterized by arrogance or by a +dogmatic approach to ideology. However, the most dangerous leftists of all may be certain oversocialized types who avoid +irritating displays of aggressiveness and refrain from advertising their leftism, but work quietly and unobtrusively to promote +collectivist values, “enlightened” psychological techniques for socializing children, dependence of the individual on the +system, and so forth. These crypto-leftists (as we may call them) approximate certain bourgeois types as far as practical action +is concerned, but differ from them in psychology, ideology and motivation. The ordinary bourgeois tries to bring people under +control of the system in order to protect his way of life, or he does so simply because his attitudes are conventional. The +crypto-leftist tries to bring people under control of the system because he is a True Believer in a collectivist ideology. The +crypto-leftist is differentiated from the average leftist of the oversocialized type by the fact that his rebellious impulse is +weaker and he is more securely socialized. He is differentiated from the ordinary well-socialized bourgeois by the fact that +there is some deep lack within him that makes it necessary for him to devote himself to a cause and immerse himself in a +collectivity. And maybe his (well-sublimated) drive for power is stronger than that of the average bourgeois. + +Final Note +231. Throughout this article we’ve made imprecise statements and statements that ought to have had all sorts of qualifications +and reservations attached to them; and some of our statements may be flatly false. Lack of sufficient information and the need +for brevity made it impossible for us to formulate our assertions more precisely or add all the necessary qualifications. And of +course in a discussion of this kind one must rely heavily on intuitive judgment, and that can sometimes be wrong. So we don’t +claim that this article expresses more than a crude approximation to the truth. +232. All the same, we are reasonably confident that the general outlines of the picture we have painted here are roughly correct. +Just one possible weak point needs to be mentioned. We have portrayed leftism in its modern form as a phenomenon peculiar +to our time and as a symptom of the disruption of the power process. But we might possibly be wrong about this. +Oversocialized types who try to satisfy their drive for power by imposing their morality on everyone have certainly been +around for a long time. But we THINK that the decisive role played by feelings of inferiority, low self-esteem, powerlessness, +identification with victims by people who are not themselves victims, is a peculiarity of modern leftism. Identification with +victims by people not themselves victims can be seen to some extent in 19th century leftism and early Christianity but as far as +we can make out, symptoms of low self-esteem, etc., were not nearly so evident in these movements, or in any other +movements, as they are in modern leftism. But we are not in a position to assert confidently that no such movements have +existed prior to modern leftism. This is a significant question to which historians ought to give their attention. + +Notes +1. (Paragraph 19) We are asserting that ALL, or even most, bullies and ruthless competitors suffer from feelings of inferiority. +2. (Paragraph 25) During the Victorian period many oversocialized people suffered from serious psychological problems as a +result of repressing or trying to repress their sexual feelings. Freud apparently based his theories on people of this type. Today +the focus of socialization has shifted from sex to aggression. +3. (Paragraph 27) Not necessarily including specialists in engineering or the “hard” sciences. +4. (Paragraph 28) There are many individuals of the middle and upper classes who resist some of these values, but usually their +resistance is more or less covert. Such resistance appears in the mass media only to a very limited extent. The main thrust of +propaganda in our society is in favor of the stated values. The main reason why these values have become, so to speak, the +official values of our society is that they are useful to the industrial system. Violence is discouraged because it disrupts the +functioning of the system. Racism is discouraged because ethnic conflicts also disrupt the system, and discrimination wastes +the talents of minority-group members who could be useful to the system. Poverty must be “cured” because the underclass +causes problems for the system and contact with the underclass lowers the morale of the other classes. Women are encouraged +to have careers because their talents are useful to the system and, more importantly, because by having regular jobs women +become better integrated into the system and tied directly to it rather than to their families. This helps to weaken family +solidarity. (The leaders of the system say they want to strengthen the family, but they really mean is that they want the family +to serve as an effective tool for socializing children in accord with the needs of the system. We argue in paragraphs 51, 52 that +the system cannot afford to let the family or other small-scale social groups be strong or autonomous.) +5. (Paragraph 42) It may be argued that the majority of people don’t want to make their own decisions but want leaders to do +their thinking for them. There is an element of truth in this. People like to make their own decisions in small matters, but +making decisions on difficult, fundamental questions requires facing up to psychological conflict, and most people hate +psychological conflict. Hence they tend to lean on others in making difficult decisions. But it does not follow that they like to +have decisions imposed upon them without having any opportunity to influence those decisions. The majority of people are +natural followers, not leaders, but they like to have direct personal access to their leaders, they want to be able to influence the +leaders and participate to some extent in making even the difficult decisions. At least to that degree they need autonomy. +6. (Paragraph 44) Some of the symptoms listed are similar to those shown by caged animals. To explain how these symptoms +arise from deprivation with respect to the power process: common-sense understanding of human nature tells one that lack of +goals whose attainment requires effort leads to boredom and that boredom, long continued, often leads eventually to +depression. Failure to attain goals leads to frustration and lowering of self-esteem. Frustration leads to anger, anger to +aggression, often in the form of spouse or child abuse. It has been shown that long-continued frustration commonly leads to +depression and that depression tends to cause guilt, sleep disorders, eating disorders and bad feelings about oneself. Those who +are tending toward depression seek pleasure as an antidote; hence insatiable hedonism and excessive sex, with perversions as a +means of getting new kicks. Boredom too tends to cause excessive pleasure-seeking since, lacking other goals, people often use +pleasure as a goal. See accompanying diagram. +The foregoing is a simplification. Reality is more complex, and of course, deprivation with respect to the power process is not +the ONLY cause of the symptoms described. By the way, when we mention depression we do not necessarily mean depression +that is severe enough to be treated by a psychiatrist. Often only mild forms of depression are involved. And when we speak of +goals we do not necessarily mean long-term, thought-out goals. For many or most people through much of human history, the +goals of a hand-to-mouth existence (merely providing oneself and one’s family with food from day to day) have been quite +sufficient. +7. (Paragraph 52) A partial exception may be made for a few passive, inward-looking groups, such as the Amish, which have +little effect on the wider society. Apart from these, some genuine small-scale communities do exist in America today. For +instance, youth gangs and “cults.” Everyone regards them as dangerous, and so they are, because the members of these groups +are loyal primarily to one another rather than to the system, hence the system cannot control them. Or take the gypsies. The +gypsies commonly get away with theft and fraud because their loyalties are such that they can always get other gypsies to give +testimony that “proves” their innocence. Obviously the system would be in serious trouble if too many people belonged to such +groups. Some of the early-20th century Chinese thinkers who were concerned with modernizing China recognized the +necessity breaking down small-scale social groups such as the family: “(According to Sun Yat-sen) the Chinese people needed +a new surge of patriotism, which would lead to a transfer of loyalty from the family to the state.... (According to Li Huang) +traditional attachments, particularly to the family had to be abandoned if nationalism were to develop in China.” (Chester C. +Tan, “Chinese Political Thought in the Twentieth Century,” page 125, page 297.) +8. (Paragraph 56) Yes, we know that 19th century America had its problems, and serious ones, but for the sake of brevity we +have to express ourselves in simplified terms. +9. (Paragraph 61) We leave aside the “underclass.” We are speaking of the mainstream. +10. (Paragraph 62) Some social scientists, educators, “mental health” professionals and the like are doing their best to push the +social drives into group 1 by trying to see to it that everyone has a satisfactory social life. +11. (Paragraphs 63, 82) Is the drive for endless material acquisition really an artificial creation of the advertising and marketing +industry? Certainly there is no innate human drive for material acquisition. There have been many cultures in which people +have desired little material wealth beyond what was necessary to satisfy their basic physical needs (Australian aborigines, +traditional Mexican peasant culture, some African cultures). On the other hand there have also been many pre-industrial +cultures in which material acquisition has played an important role. So we can’t claim that today’s acquisition-oriented culture +is exclusively a creation of the advertising and marketing industry. But it is clear that the advertising and marketing industry +has had an important part in creating that culture. The big corporations that spend millions on advertising wouldn’t be spending +that kind of money without solid proof that they were getting it back in increased sales. One member of FC met a sales +manager a couple of years ago who was frank enough to tell him, “Our job is to make people buy things they don’t want and +don’t need.” He then described how an untrained novice could present people with the facts about a product, and make no sales +at all, while a trained and experienced professional salesman would make lots of sales to the same people. This shows that +people are manipulated into buying things they don’t really want. +12. (Paragraph 64) The problem of purposelessness seems to have become less serious during the last 15 years or so, because +people now feel less secure physically and economically than they did earlier, and the need for security provides them with a +goal. But purposelessness has been replaced by frustration over the difficulty of attaining security. We emphasize the problem +of purposelessness because the liberals and leftists would wish to solve our social problems by having society guarantee +everyone’s security; but if that could be done it would only bring back the problem of purposelessness. The real issue is not +whether society provides well or poorly for people’s security; the trouble is that people are dependent on the system for their +security rather than having it in their own hands. This, by the way, is part of the reason why some people get worked up about +the right to bear arms; possession of a gun puts that aspect of their security in their own hands. +13. (Paragraph 66) Conservatives’ efforts to decrease the amount of government regulation are of little benefit to the average +man. For one thing, only a fraction of the regulations can be eliminated because most regulations are necessary. For another +thing, most of the deregulation affects business rather than the average individual, so that its main effect is to take power from +the government and give it to private corporations. What this means for the average man is that government interference in his +life is replaced by interference from big corporations, which may be permitted, for example, to dump more chemicals that get +into his water supply and give him cancer. The conservatives are just taking the average man for a sucker, exploiting his +resentment of Big Government to promote the power of Big Business. +14. (Paragraph 73) When someone approves of the purpose for which propaganda is being used in a given case, he generally +calls it “education” or applies to it some similar euphemism. But propaganda is propaganda regardless of the purpose for which +it is used. +15. (Paragraph 83) We are not expressing approval or disapproval of the Panama invasion. We only use it to illustrate a point. +16. (Paragraph 95) When the American colonies were under British rule there were fewer and less effective legal guarantees of +freedom than there were after the American Constitution went into effect, yet there was more personal freedom in preindustrial America, both before and after the War of Independence, than there was after the Industrial Revolution took hold in +this country. We quote from “Violence in America: Historical and Comparative Perspectives,” edited by Hugh Davis Graham +and Ted Robert Gurr, Chapter 12 by Roger Lane, pages 476-478: “The progressive heightening of standards of propriety, and +with it the increasing reliance on official law enforcement (in l9th century America) ... were common to the whole society.... +[T]he change in social behavior is so long term and so widespread as to suggest a connection with the most fundamental of +contemporary social processes; that of industrial urbanization itself....”Massachusetts in 1835 had a population of some +660,940, 81 percent rural, overwhelmingly pre-industrial and native born. It’s citizens were used to considerable personal +freedom. Whether teamsters, farmers or artisans, they were all accustomed to setting their own schedules, and the nature of +their work made them physically independent of each other.... Individual problems, sins or even crimes, were not generally +cause for wider social concern....” But the impact of the twin movements to the city and to the factory, both just gathering force +in 1835, had a progressive effect on personal behavior throughout the 19th century and into the 20th. The factory demanded +regularity of behavior, a life governed by obedience to the rhythms of clock and calendar, the demands of foreman and +supervisor. In the city or town, the needs of living in closely packed neighborhoods inhibited many actions previously +unobjectionable. Both blue- and white-collar employees in larger establishments were mutually dependent on their fellows; as +one man’s work fit into anther’s, so one man’s business was no longer his own. “The results of the new organization of life and +work were apparent by 1900, when some 76 percent of the 2,805,346 inhabitants of Massachusetts were classified as urbanites. +Much violent or irregular behavior which had been tolerable in a casual, independent society was no longer acceptable in the +more formalized, cooperative atmosphere of the later period.... The move to the cities had, in short, produced a more tractable, +more socialized, more ‘civilized’ generation than its predecessors.” +If copyright problems make it impossible for this long quotation to be printed, then please change Note 16 to read as follows: +16. (Paragraph 95) When the American colonies were under British rule there were fewer and less effective legal guarantees of +freedom than there were after the American Constitution went into effect, yet there was more personal freedom in preindustrial America, both before and after the War of Independence, than there was after the Industrial Revolution took hold in +this country. In “Violence in America: Historical and Comparative Perspectives,” edited by Hugh Davis Graham and Ted +Robert Gurr, Chapter 12 by Roger Lane, it is explained how in pre-industrial America the average person had greater +independence and autonomy than he does today, and how the process of industrialization necessarily led to the restriction of +personal freedom. +17. (Paragraph 117) Apologists for the system are fond of citing cases in which elections have been decided by one or two +votes, but such cases are rare. +18. (Paragraph 119) “Today, in technologically advanced lands, men live very similar lives in spite of geographical, religious, +and political differences. The daily lives of a Christian bank clerk in Chicago, a Buddhist bank clerk in Tokyo, and a +Communist bank clerk in Moscow are far more alike than the life of any one of them is like that of any single man who lived a +thousand years ago. These similarities are the result of a common technology....” L. Sprague de Camp, “The Ancient +Engineers,” Ballantine edition, page 17. The lives of the three bank clerks are not IDENTICAL. Ideology does have SOME +effect. But all technological societies, in order to survive, must evolve along APPROXIMATELY the same trajectory. +19. (Paragraph 123) Just think an irresponsible genetic engineer might create a lot of terrorists. +20. (Paragraph 124) For a further example of undesirable consequences of medical progress, suppose a reliable cure for cancer +is discovered. Even if the treatment is too expensive to be available to any but the elite, it will greatly reduce their incentive to +stop the escape of carcinogens into the environment. +21. (Paragraph 128) Since many people may find paradoxical the notion that a large number of good things can add up to a bad +thing, we illustrate with an analogy. Suppose Mr. A is playing chess with Mr. B. Mr. C, a Grand Master, is looking over Mr. +A’s shoulder. Mr. A of course wants to win his game, so if Mr. C points out a good move for him to make, he is doing Mr. A a +favor. But suppose now that Mr. C tells Mr. A how to make ALL of his moves. In each particular instance he does Mr. A a +favor by showing him his best move, but by making ALL of his moves for him he spoils his game, since there is not point in +Mr. A’s playing the game at all if someone else makes all his moves. The situation of modern man is analogous to that of Mr. +A. The system makes an individual’s life easier for him in innumerable ways, but in doing so it deprives him of control over +his own fate. +22. (Paragraph 137) Here we are considering only the conflict of values within the mainstream. For the sake of simplicity we +leave out of the picture “outsider” values like the idea that wild nature is more important than human economic welfare. +23. (Paragraph 137) Self-interest is not necessarily MATERIAL self-interest. It can consist in fulfillment of some +psychological need, for example, by promoting one’s own ideology or religion. +24. (Paragraph 139) A qualification: It is in the interest of the system to permit a certain prescribed degree of freedom in some +areas. For example, economic freedom (with suitable limitations and restraints) has proved effective in promoting economic +growth. But only planned, circumscribed, limited freedom is in the interest of the system. The individual must always be kept +on a leash, even if the leash is sometimes long (see paragraphs 94, 97). +25. (Paragraph 143) We don’t mean to suggest that the efficiency or the potential for survival of a society has always been +inversely proportional to the amount of pressure or discomfort to which the society subjects people. That certainly is not the +case. There is good reason to believe that many primitive societies subjected people to less pressure than European society did, +but European society proved far more efficient than any primitive society and always won out in conflicts with such societies +because of the advantages conferred by technology. +26. (Paragraph 147) If you think that more effective law enforcement is unequivocally good because it suppresses crime, then +remember that crime as defined by the system is not necessarily what YOU would call crime. Today, smoking marijuana is a +“crime,” and, in some places in the U.S., so is possession of an unregistered handgun. Tomorrow, possession of ANY firearm, +registered or not, may be made a crime, and the same thing may happen with disapproved methods of child-rearing, such as +spanking. In some countries, expression of dissident political opinions is a crime, and there is no certainty that this will never +happen in the U.S., since no constitution or political system lasts forever. If a society needs a large, powerful law enforcement +establishment, then there is something gravely wrong with that society; it must be subjecting people to severe pressures if so +many refuse to follow the rules, or follow them only because forced. Many societies in the past have gotten by with little or no +formal law-enforcement. +27. (Paragraph 151) To be sure, past societies have had means of influencing human behavior, but these have been primitive +and of low effectiveness compared with the technological means that are now being developed. +28. (Paragraph 152) However, some psychologists have publicly expressed opinions indicating their contempt for human +freedom. And the mathematician Claude Shannon was quoted in Omni (August 1987) as saying, “I visualize a time when we +will be to robots what dogs are to humans, and I’m rooting for the machines.” +29. (Paragraph 154) This is no science fiction! After writing paragraph 154 we came across an article in Scientific American +according to which scientists are actively developing techniques for identifying possible future criminals and for treating them +by a combination of biological and psychological means. Some scientists advocate compulsory application of the treatment, +which may be available in the near future. (See “Seeking the Criminal Element,” by W. Wayt Gibbs, Scientific American, +March 1995.) Maybe you think this is OK because the treatment would be applied to those who might become violent +criminals. But of course it won’t stop there. Next, a treatment will be applied to those who might become drunk drivers (they +endanger human life too), then perhaps to peel who spank their children, then to environmentalists who sabotage logging +equipment, eventually to anyone whose behavior is inconvenient for the system. +30. (Paragraph 184) A further advantage of nature as a counter-ideal to technology is that, in many people, nature inspires the +kind of reverence that is associated with religion, so that nature could perhaps be idealized on a religious basis. It is true that in +many societies religion has served as a support and justification for the established order, but it is also true that religion has +often provided a basis for rebellion. Thus it may be useful to introduce a religious element into the rebellion against +technology, the more so because Western society today has no strong religious foundation. Religion, nowadays either is used +as cheap and transparent support for narrow, short-sighted selfishness (some conservatives use it this way), or even is cynically +exploited to make easy money (by many evangelists), or has degenerated into crude irrationalism (fundamentalist protestant +sects, “cults”), or is simply stagnant (Catholicism, main-line Protestantism). The nearest thing to a strong, widespread, dynamic +religion that the West has seen in recent times has been the quasi-religion of leftism, but leftism today is fragmented and has no +clear, unified, inspiring goal. Thus there is a religious vacuum in our society that could perhaps be filled by a religion focused +on nature in opposition to technology. But it would be a mistake to try to concoct artificially a religion to fill this role. Such an +invented religion would probably be a failure. Take the “Gaia” religion for example. Do its adherents REALLY believe in it or +are they just play-acting? If they are just play-acting their religion will be a flop in the end. It is probably best not to try to +introduce religion into the conflict of nature vs. technology unless you REALLY believe in that religion yourself and find that +it arouses a deep, strong, genuine response in many other people. +31. (Paragraph 189) Assuming that such a final push occurs. Conceivably the industrial system might be eliminated in a +somewhat gradual or piecemeal fashion (see paragraphs 4, 167 and Note 4). +32. (Paragraph 193) It is even conceivable (remotely) that the revolution might consist only of a massive change of attitudes +toward technology resulting in a relatively gradual and painless disintegration of the industrial system. But if this happens we’ll +be very lucky. It’s far more probably that the transition to a non-technological society will be very difficult and full of conflicts +and disasters. +33. (Paragraph 195) The economic and technological structure of a society are far more important than its political structure in +determining the way the average man lives (see paragraphs 95, 119 and Notes 16, 18). +34. (Paragraph 215) This statement refers to our particular brand of anarchism. A wide variety of social attitudes have been +called “anarchist,” and it may be that many who consider themselves anarchists would not accept our statement of paragraph +215. It should be noted, by the way, that there is a nonviolent anarchist movement whose members probably would not accept +FC as anarchist and certainly would not approve of FC’s violent methods. +35. (Paragraph 219) Many leftists are motivated also by hostility, but the hostility probably results in part from a frustrated +need for power. +36. (Paragraph 229) It is important to understand that we mean someone who sympathizes with these MOVEMENTS as they +exist today in our society. One who believes that women, homosexuals, etc., should have equal rights is not necessary a leftist. +The feminist, gay rights, etc., movements that exist in our society have the particular ideological tone that characterizes leftism, +and if one believes, for example, that women should have equal rights it does not necessarily follow that one must sympathize +with the feminist movement as it exists today. diff --git a/_site/tutoring/index.html b/_site/tutoring/index.html new file mode 100644 index 0000000..ae5b0b2 --- /dev/null +++ b/_site/tutoring/index.html @@ -0,0 +1,76 @@ + + + + + Tutoring | tait.tech + + + + + + + +
    +
    +

    tait.tech

    + + + +
    +
    +

    Tutoring

    + +

    I believe in solving problems. +Learn how to solve your own unique computer-related problems by learning how they work underneath all the fancy buttons you see.

    + +

    Contact me at tutoring@tait.tech for more info.

    + +

    Rates/hour

    + +

    C$25 for a programming language I am comfortable with already.

    +
      +
    • C, C++, Java, Python, HTML/CSS/JS.
    • +
    + +

    C$20 + 1 week prep. time for a programming language I am not very comfortable with already.

    +
      +
    • Rust, Go, Node.js.
    • +
    + +

    C$35 for Linux system administration.

    +
      +
    • Paritioning disks, install and configure software and automate tasks via the command line interface (CLI).
    • +
    + +

    I offer group discounts for 2+ students.

    + +
    Resources:
    + +

    My favourite resources are the following:

    + + + +
    +
    + +
    + +