Chapter 4.9 - Abstraction Inside the CPU

Time Estimate: 45 minutes

4.9.1. Introduction and Goals

Abstraction In Hardware and Software

Abstraction is an important concept in CS -- and justifiably so. In fact, the history of computing can be seen as an advance from very primitive abstractions to very high-level abstractions.

Our Thunkable language is a great example of a very high-level abstraction. Consider the Camera component. Thunkable lets you take a photo by using a single photo from camera block in your app. But think about all of the very low-level operations that have to take place to focus the camera, gather and convert light rays into pixels and then into bits and then into an image file on your device. The Thunkable block hides all of that complexity.

Learning Objectives: I will learn to

  • describe how abstractions in the CPU hide complexity and make a computer easier to use

  • explain in detail how the CPU executes the instrucitons of a program

Language Objectives: I will be able to

  • use target vocabulary, such as machine language, fetch-execute cycle, and overflow error while describing abstractions within the CPU, with the support of concept definitions and vocabulary notesarrow-up-right from this lesson

4.9.2. Learning Activities

Gen 0 YouTube Videoarrow-up-right | Gen 1 YouTube Videoarrow-up-right | Gen 2 YouTube Videoarrow-up-right | Gen 0 TeacherTube Videoarrow-up-right | Gen 1 TeacherTube Videoarrow-up-right | Gen 2 TeacherTube Videoarrow-up-right

Before There Was Software

To help us get a sense of this march through ever higher levels of abstraction, we're going to imagine ourselves back in the very first days of computing -- to the days before there was such a thing as software, when "programming" was a matter of manually inserting instructions and data into the computer, pressing the "run" switch and hoping for the best. There were no operating systems and blocks editors back then -- those abstractions came much later.

In the early days of computing, before there was software, virtually all programmers were women. Women 'computers', as they were called, wrote the first programs on the ENIACarrow-up-right, the first digital computer. But the story of the ENIAC programmers had not been told until just recently. Watch the trailer of a new movie. After watching, discuss with your classmates whether or not programmers used the same or different skills compared to what you're learning in this course.

Watch the ENIAC Trailer - Created with Studio G for Google I/Oarrow-up-right from Kathy Kleimanarrow-up-right on Vimeo.

Women programming ENIAC

4-Bit Computer Simulator

The ENIAC weighed 30 tons. But in the hardware of the day it could store only 20 10-digit numbers in its accumulators or memory registers. Programs had to be written by hand on paper and once the algorithm was figured out, it would often take days to get the program into the ENIAC by manipulating its switches and cables. Later on, punched cards like the following were used to input programs or a simple addition calculation.

Gen 0 4-bit

In this lesson, we will use a 4-bit Computer Simulatorarrow-up-right that has only 16 8-bit memory locations, so it's not that much 'smaller' than ENIAC. And like the ENIAC, it has little or no software. This will give you a hands-on sense of what programming was like before we had high-level languages and sophisticated programming platforms. It's also important to realize that the 4-bit Simulator is an accurate model of how today's computers work -- before your App Inventor programs can be run on your smart phones, they have to be translated into machine language, where they are interpreted by the CPU.

The videos and exercises below introduce the 4-bit computer simulator.

  • Generation 0: Programming the raw machine. Just like the ENIAC women did, machine language programs have to be put directly into the computer's memory.

  • Generation 1: Using an Editor and a Loader. Our first software abstractions will be an editor, which will let us type out the machine instructions, and a loader, which will load the instructions into memory for us.

  • Generation 2. Using an Assembly Language. Instead of having to deal with 0s and 1s, our assembly language will give us a higher-level abstraction by letting us deal with symbolic names for instructions and data.

The simulator models a simple CPU Fetch/Execute Cycle like below but where the instructions are 1) Fetched from RAM, 2) Decoded in the CPU 3) Any needed data is fetched from RAM and 4) the operation is Executed in the CPU.

For each of the simulators below, watch the video and then in groups or pairs, do the self-check exercises after each video.

Generation 0: The Raw Machine

The video that follows takes us on a tour of the 4-bit computer. Perhaps the easiest way to follow along on the tour is to open the simulator itself in an adjacent tab and pause the video at spots to explore the simulator itself. Here's a link to the simulatorarrow-up-right that will open in a separate tab.

Q-1: What is 0011?

In the 4-bit computer we can find several occurrences of the 4-bit string, 0011. What does this string of bits represent? (Choose all answers that apply.)

A. The decimal value 3.

B. The machine instruction for ADDing a number to the accumulator

C. The decimal value 17.

D. A memory location in the computer's RAM.

Q-2: What's the Algorithm?

Generation 0arrow-up-right of the 4-bit computer comes pre-loaded with a program that adds 1 and 2 and outputs their sum, 3. The value 1 in decimal is stored in location 1000. And the value 2 is stored in location 1001. Which of the following pseudocode algorithms correctly describes that program's machine language algorithm?

A.

Load the value at 1000 into the Accumulator

Add the value in 1001 to the Accumulator

Store the Accumulator at location 1010

Stop

B.

Load the value at 1000 into the Accumulator

Add the value in 1001 to the Accumulator

Store the Accumulator at location 1010

Print the value in location 1010

Stop

C.

Load the value at 1000 into the Accumulator

Add the value in 1000 to the Accumulator

Store the Accumulator at location 1010

Print the value in location 1010

Stop

D.

Load the value at 1000 into the Accumulator

Add the value in 1001 to the Accumulator

Print the value in location 1010

Stop

Q-3: What's the Output?

Our 4-bit computer uses 8-bit bytes to represent its data. An 8-bit byte can store values ranging from 0 to 255 -- i.e., 00000000 to 11111111. What do you suppose would happen if you added 1 to 11111111?

To help answer this question, you might want to use the 4-bit simulatorarrow-up-right to write a little machine language program to see what happens. By default, the 4-bit computer adds the values in locations 1000 and 1001 and prints the sum. So here is how you would set up the machine to add 11111111 and 00000001:

  • Put the value 11111111 (decimal 255) in memory location 1000.

  • Put the value 00000001 (decimal 1) in memory location 1001.

  • Run the program and observe the output.

What decimal value do you get when you add binary 1 to binary 11111111?

A. 0

B. 256

C. 255

D. No value. The machine would crash.

As you saw in the last self-check exercise, it is easy to see an overflow error when the 4-bit computer attempts to handle a number that is larger than the memory it has available. Even modern computers can occasionally have an overflow error when the computer attempts to handle a very large number that is outside of the defined range of values can be represented.

Generation 1: Machine Language Programming

Generation 1arrow-up-right of the 4-bit computer comes with some system software, software that today would be considered part of the computer's operating system. It provides an editor, which is software that lets you compose a machine language program, and a loader, software that will load the program into memory. This was similar to using punched cards to load in a program into a computer in the 1950s-1970s.

It also represents the first step toward a higher-level abstraction by freeing us from having to directly input values into the machine's memory. Instead, we can just type the program in the editor and the software will figure out how to load it into memory.

The following video will show you how this works.

Q-4: What's the Output?

As you saw in the demo videoarrow-up-right, the 4-bit editor requires you to organize your program as follows:

Data initialization statements

Machine language instructions

Type the following machine language program into the editor -- just the 0s and 1s part, not the pseudocode comments, which begin after the --.

1000:00001111

-- Initialize memory location 1000 to 00001111

1001:00001000

-- Initialize memory location 1001 to 00001000

00011000

-- Loads value at memory location 1000 into ACC

01011001

-- Multiplies value at memory location 1001 to ACC

00101010

-- Stores value in ACC to location 1010

10001010

-- Prints the value currently in location 1010

00000000

-- Stops the program

Then click the "Load" button to load it into memory and then run the program. What value does it output?

Generation 2: Assembly Language Programming

Generation 2arrow-up-right of the 4-bit computer introduces some additional software in the form of an assembly language. Assembly languages were the first step in the direction of raising the level of abstraction used in writing and debugging programs. It's not a big step beyond machine language. But it does succeed in hiding some of the machine's underlying complexity, including the need to remember binary opcodes, memory addresses and data values.

The following video will show you how this works.

4.9.3. Summary

In this lesson, you learned how to:

Learning Objective CRD-2.I.a: For errors in an algorithm or program: a. Identify the error.

Learning Objective CRD-2.I.b: For errors in an algorithm or program: b. Correct the error.

4.9.4. Still Curious?

Here are a couple of additional assembly language problems:

  • Write an assembly language program that computes the square function for any valur x -- i.e., f(x) = x2. HINT: You'll need to use the INP operation to input the value for x. Once you have your program working, use it to help answer this question: Given that the 4-bit computer can only represent the numbers between 0 and 255, what's the largest value for x for which your program will work?

  • Write an assembly language program that computes the value f(a,b) = a2 + b. HINT: Be economical. This program will barely fit into the 4-bit computer's memory.

The story of the ENIAC programmersarrow-up-right is now told in a short documentary film that is freely available for viewing. If you want to watch it individually it is 20 minutes long (and may cost $5 to stream it).

4.9.5. Self-Check

Here is a table of the technical terms introduced in this lesson. Hover over the terms to review the definitions.

RAM

CPU

ALU

machine language

fetch-execute cycle

instruction register

instruction counter

accumulator

assembly language

overflow error

Q-5:

What's the Output?

As you saw in the demo videoarrow-up-right, Generation 2arrow-up-right of the 4-bit computer lets you use an assembly language to program the machine. Here's an example:

VAR A 10

VAR B 0

LDA A

MUL A

STA B

PRN B

NOP

Type that program into the Editor and then assemble, load, and run it. What output do you get?

4.9.6. Reflection: For Your Portfolio

Answer the following portfolio reflection questions as directed by your instructor. Questions are also available in this Google Docarrow-up-right where you may use File > Make a Copy to make your own editable copy.

Last updated