Running Stallings Figure 1.4

Look at 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.

Boot into linux, 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.


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". In the terminal window, make the directory containing that program your current directory. 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.


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, tturn 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. In the little panel window of buttons for various commands, press "Run". 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 do the former, on the horizontal main menu click "Status" and on the resulting drop-down submenu click "Registers". To do the latter, menu item Data and submenu item Memory. A dialog box pops up. Enter "12" in the "Examine" field, and "&addend1" in the "from" field. Then press the "Display" button and close the dialog box (click X in the upper-right corner). 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 804908c 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 eip (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 eax register. The eax register will end up containing the number 3. Before you do it, find eax in the "DDD: Registers" window and note what value it contains. Now pull the trigger on line 15 by pressing 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 eax. It changed. According to the instruction. Hey! this works. Also, note the new value of eip. Write it down. Predict what will happen if you now run line 16 by pressing "Step" again. Test your hypothesis by doing so. What has happened to eax now? Finally, predict the result of one more "Step". Look for its effect in the data window. Do it. Check it. Note also eip's change.

Now close ddd-- File/Exit.


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. Turn in the sheet at the end of the lab.