Running Stallings Figure 1.4

 

Note - on taking screenshots

Below there may be junctures where you are asked to take screenshots (notations appear there in bracketed red italics). To do that, it's recommended to use a screenshot program that you may have on your host machine. Take the screenshot with the VM window open showing the activity of interest.

Alternatively, the guest virtual machine has a screenshot program. You can take a screenshot within the VM by pressing the PrintScreen key. The result is that a file is deposited on the disk. The name of the file will be something like "Screenshot from 2020-09-14 21-29-57.png" and it will be found in the "Pictures" subdirectory of the user's home directory. In the exercise, performed as student, that will be /home/student/Pictures/.

The host option is recommended in order to avoid the need to transfer the file out of the guest VM, since the VMs as provided are not equipped with interfaces that give them internet/external connectivity. It can be done and is documented on our class website but is best avoided.

 

Refer to Figure 1.4 in the William Stallings textbook:

It exemplifies a machine language program that adds 3 plus 2. I wrote such a program myself to do exactly that, in order to embody the Figure in real life.

If using VirtualBox and the fedora virtual machine (as example here fedora36-summer22, to be updated and slightly renamed in subsequent semesters) use the Oracle VM VirtualBox Manager to make a linked clone of that machine. (Right-click on fedora36-summer22, choose Clone from the resulting context menu, then "Linked clone" for clone type. Avoid choosing full clone, which must make a copy of the whole virtual hard disk and is time-consuming.) You will do this exercise in the clone.

Start the "fedora36-summer22 Clone" virtual machine. Boot into it as non-root (e.g., student), then at the command line bring up the GUI (graphical interface) by issuing the following command:

startx

Launch a terminal window.


Acquire and install the debugger and its interface as needed

Per your instructor, install the ddd graphical interface for the gdb debugger if needed. (It is not needed in the fedora virtual machine, where it is already installed.)


Acquire and assemble the sample program

Also obtain the assembly language program "stalllings-fig1.4.s". You can get it by downloading and unzipping "stallings-fig1.4.zip" if internet-connected. (In the fedora virtual machine the file is located in /home/public, whence it can be copied to wherever you want it.) Open a terminal window (from "Activities" at upper left of gnome desktop).

cd
cp  /home/public/stallings-fig1.4.zip  .   
[ assuming the presence of the file on your platform; don't overlook the dot at the end ]
unzip  stallings-fig1.4.zip

Then prepare the program to run by the 2-step process of assembling and linking it. First, assemble it:

as -a --gstabs -o stallings-fig1.4.o stallings-fig1.4.s

Second, link the result:

ld -o stallings-fig1.4 stallings-fig1.4.o

The program "stallings-fig1.4" is now ready to run. Before doing that, let's examine its code and compare with Figure 1.4 in the Stallings textbook. You can look at the program on your screen with this command:

cat stallings-fig1.4.s 

For your convenience, here it is with line numbers:

     1	# Stallings figure 1.4 "live"
     2 
     3	.data  # data, in memory somewhere
     4	addend1: 
     5	      .long   3
     6	addend2:
     7	      .long   2
     8	sum:
     9	      .long   0
    10 
    11	.text  # code, also in memory
    12	.globl _start
    13	_start:
    14	      movl $0,%eax
    15	      movl (addend1), %eax
    16	      addl (addend2), %eax
    17	      movl %eax, sum
    18	      ret

The Figure accomplishes the job (adding 3 plus 2)  in 3 steps. So does my implementing program, whose line 15 corresponds to the Figure's step 1 (top row), line 16 to step 2 (middle row), and line 17 to step 3 (bottom row). The Figure gets its addends from memory locations 940 and 941. The program gets them from memory locations I have labeled "addend1" and "addend2" (I use labels since I don't know what the address numbers are going to be; they can vary each time the program runs.) The Figure uses the register "AC" in which to perform the math; the program uses the actual chip's register "eax". The Figure indicates which instruction to execute next in the register "PC" while the program will similarly use the actual chip's register "eip". The Figure stores its sum to memory location 941; the program to a location I have labeled "sum".

The parallelism is complete and unmistakable.


Register nomenclature

Don't be confused by the slight differences in the naming of cpu registers seen in different environments. Early on, cpu's called their several general purpose registers A, B, C, D. The 16-bit intel cpu's called them AX, BX, CX, DX. Another important one, the "instruction pointer" register, was named IP. The 32-bit cpu  generation prefixed these names with an "E" (for "extended" because the registers were twice as wide), and the 64-bit generation with an "R". 

 

register names

16-bit

32-bit

64-bit

AX

EAX

RAX

BX

EBX

RBX

CX

ECX

RCX

DX

EDX

RDX

IP

EIP

RIP

The assembly language source code of our example program uses 32-bit naming (which nevertheless works in the 64-bit environment). In the debugger however, the 64-bit naming is displayed. So where the code manipulates the "eax" register, when shown within the debugger it's "rax". They are the same thing.


Watching the program unfold step-by-step through the debugger

The program is to be viewed in a debugger, executed step-by-step in order to reveal its correspondence with the Figure. In your terminal window:

ddd stallings-fig1.4 &

A graphical program comes up. Close the "tip" dialog box. Using the view menu make sure to display sub-windows (horizontal screen bands) for: Data Window, Source Window, Machine Code Window.  You should see our lines of code in the Code Window. If line numbers are not displayed, turn on that feature using the checkbox in Edit/Preferences/Source.

- double click on the line number for line 15. A stop sign icon appears there.

- press "Run" in the little panel window of buttons for various commands. A green arrow appears to the left of line 15, pointing to it. The program has just run up to, but not including, that line at which it has halted. At this juncture we can examine what's in the CPU's registers, and in selected parts of memory.

-  to examine CPU registers, on the horizontal main menu click "Status" and on the resulting drop-down submenu click "Registers"

- to examine memory, menu item Data and submenu item Memory. A dialog box pops up.

-enter "12" in the "Examine" field
 enter "&addend1" in the "from" field

 - press the "Display" button
   press the "Close" button to close the dialog box

 Look at what shows up in the data window, the uppermost horizontal band of the display. It shows the 12 bytes of memory starting at the location labeled "addend1". And it concretely shows what address number that actually is (e.g., in my experiment I saw 402000 in the hexadecimal number system; yours might differ). addend1 occupies 4 bytes, and holds the number 3. The ensuing 4 bytes, which are labeled addend 2, hold the number 2 (do you see it?). And the next 4 bytes after that, labeled sum, hold the number 0. Before proceeding, note in the "DDD: Registers" window the value of rip (the instruction pointer register). Write it down. Now we want to proceed by running just a single instruction, the one in line 15. That's going to move the contents of addend1 into the rax register. The rax register will end up containing the number 3. Before you do it, find rax in the "DDD: Registers" window and note what value it contains. Now pull the trigger on line 15:

- press the "Step" button. The green arrow moves to line 16. That means line 15 was performed; it's now over. In its aftermath, note the value now contained in rax. It changed. According to the instruction. Hey! this works. Also, note the new value of rip. Write it down. Predict what will happen if you now run line 16 by pressing "Step" again. Test your hypothesis by doing so.

- press the "Step" button

What has happened to rax now? Finally, predict the result of one more "Step". Look for its effect in the data window. Do it:

- press the "Step" button    [ at this point take a screenshot and save it in a file named three-plus-two.jpg or .png ]

 Check it. Note also rip's change.

Now close ddd-- File/Exit.

Now that you are finished, power the VM off. Either use the "poweroff" command as root at the command line, or do it through the graphical desktop.


The assignment to perform

Print out this answer sheet. Run again from the start, just as in the previous section, stopping after each line and filling in the values the answer sheet calls for. Scan or photograph your sheet, naming the scan file "stallings-fig1.4.jpg" (or .png). Combine this file together with "three-plus-two.jpg, referred to above, into a single zip file named "three-plus-two.zip". Upload that file.