4.9. Abstraction Inside the CPU (optional)¶
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 App Inventor language is a great example of a very high-level abstraction. Consider the Camera component. App Inventor lets you take a photo by using a single Camera.TakePicture 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 App Inventor block hides all of that complexity.
- 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
- 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 notes from this lesson
4.9.2. Learning Activities¶
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 ENIAC, 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.
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.
In this lesson, we will use a 4-bit Computer Simulator 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 simulator that will open in a separate tab.
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 1 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.
Generation 2: Assembly Language Programming
Generation 2 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.
In this lesson, you learned how to:
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 programmers 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).
Here is a table of the technical terms introduced in this lesson. Hover over the terms to review the definitions.
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 Doc where you may use File/Make a Copy to make your own editable copy.