Spring 2001 CSCI 255 Lab 6

This lab is scheduled for the week of 26 February - 2 March.

Goals and Methods

This week you'll learn a couple of things: (1), how to cruise the Unix directory hierarchy; and (2), how to modify code and data in an LC-2 binary.

A bit more Linux

We'll start by creating and then connecting to a directory called csci/255/lab6.

The current and parent directories

Now list the directory contents with the command “ls -la”. That's an l (ell) not a 1, and it stands for long. When you use the -l option with ls, you get a longer directory listing that includes file permissions (or modes in Unix) and file modification times. The -a option causes all files and subdirectories to be listed. Normally, the ls command does not list files and subdirectories whose names start with a period.

In this case, ls should only list two “subdirectories” called “.” and “..”. These are two very special directory names. “.” refers to the current directory, and “..” refers to the parent of the current directory. If you're a heavy user of MS Windows, you're probably familiar with this notation. Unix did it first.

Now use ls to list the contents of your present and parent directories.

Note that “ls -la .” gives the same information as “ls -la”. That's not surprising. Both list the current directory.

Now use the .. link to go up and then down the directory hierarchy. One of these commands will come back to your current directory.

Listing directories rather than director contents

You can also use ls to list information about a single file, such as the ch5_ex1.obj file you used last week. (When listing a file, the -a option is superfluous.)

This command will show you the time of the last modification of the file ch5_ex1.obj. If you want to know the last modification time for a directory, as opposed to the files of a directory, you need to use the -d option. Execute the following command and write into the Lab 6 check off sheet the time of the last modification of your lab5 directory.

Home directory shortcuts

When you first log into Linux, you are connected to your home directory and are typing commands to a program called the shell. When you type a tilde (~) as the first character of a “word”, the shell tries to transform the word into a reference to some user's home directory. Try typing the following shell commands:

Write into the Lab 6 check off sheet the results of each command. You should see that the bare ~ gives the name of your home directory; while the ~brock gives the name of user brock's home directory.

A mystery presented and solved -- We hope

We're going to have to go into some detail here. Hold on.

Type pwd prints the name of your present working directory. Type pwd and record its output on the check off sheet

Notice that pwd claims that the name of your home directory starts with /woodfin/users while we fonund out earlier that the shell thinks it begins with /usr/users. Why the difference?

No matter which Linux computer you use in 004, you always have the same home directory. This is because your home directory is actually stored on an NFS, Network File System, server called woodfin. The /woodfin/users name is our local convention to indicate the exported /users directory of woodfin.

But what about /usr/users? That's harder to explain. In the CSCI department, we manage several hundred accounts across about forty computers. Your home directory is located on /woodfin/users, but some home directories are located on other remote disk drives. For ease of administration (particularly in the writing of system management programs) we want all home directories to be of the form /usr/users/userid where userid is a Unix logon id.

Fortunately, Unix provides links which allow a file or directory to have more than one name, such as /usr/users/userid and /woodfin/users/userid. There are actually two types of links, hard and symbolic. We'll only use symbolic links in this lab.

First, use the following command to list the directory /usr/users.

Instead of getting a listing of the /usr/users directory, you only see a single line that ends in something like:

The “->” indicates that references to /usr/users are directed to /woodfin/users. That's how your home directory can be called both /usr/users/userid and /woodfin/users/userid.

If you want ls to list the contents of the real directory, rather than the name of the target of the symbolic link, try the following command and watch the show scroll quickly by.

Creating your own symbolic link

One of the best uses of symbolic links is “sharing” files in several directories. Suppose you wanted both last week's lab5 and this week's lab6 to share the file ch5_ex1.obj. You can make this happen by typing the following command. The ln command will create the link. The -s option specifies that the link will be symbolic. Be sure to include the period at the end of the command!

Type to the following to verify that you have created the link. The cat command will list the contents of ch5_ex1.obj. It will be some binary "garbage", so don't try to understand it.

Listing all the subdirectories

Use the following command to list the contents of your home directory.

Notice that the listing only goes down one level. To dig deeper into your directory, use the -R, for recursive, option. Recursion is a concept you'll study in many of your Computer Science courses. Perhaps you've already encountered it in CSCI 201, 202, or 333. It refers to functions and algorithms that call themselves.

Now, generate a recursive listing for both your home directory and that of a neighbor. You'll have to ask the neighbor his/her userid. You should notice that certainly subdirectories of you neighbor are inaccessible to you.

Write on your check off sheet the name of one of your neighbor's directories that you were unable to see with the ls command.

Do files have addresses?

You're learning in CSCI 255 that program variables are stored in memory locations that are accessed by an address. Each file also has something similar to an address, an inode number. The term inode is an abbreviation for “index node”. It directs you to a disk location used to store information about a file, such as an index to its data blocks.

As you might expect, there is an option of ls that will list inode numbers. That option is -i. Use the following command to list the inode numbers of the files and subdirectories of your home directory. Write the inode number of your csci subdirectory on the check off sheet.

Directory stacks

There is one other CSCI 255 concept we can introduce while studying the Linux directory commands. That is the stack, the topic of Section 14.5 of the text.

Suppose you want to jump over to another directory, do a little work, and then jump back. You could type something like:

However, wouldn't it be nice if the shell were able to remember that original jump-off directory for you. It can. You use the command pushd to push the name of your current directory onto the top of a directory stack. Later you can use popd to re-connect to the directory on top of the stack. At any time you can use the command dirs to look at the entire directory stack.

Type the following commands and write into the check off sheet the lines printed by the two pwd commands.

Computer Science paradigms

In our study of Linux directories, we've seen four major paradigms of Computer Science.

  1. tree data structure -- Unix directory hierarchy with ..
  2. recursion -- The -R option of ls
  3. pointers and address -- File inode numbers
  4. stacks -- Pushing and popping directories

Back to the LC-2

It's time to get back to the LC-2. Now that you are, hopefully, back to your csci/255/lab6 directory, start up the LC-2 simulator by typing the command “lc2sim &”. Then load the program ch5_ex1.obj with File » Load Program ....

Modifying program data

Use the menu choices Run » Run Program ... to execute the machine language code. As you undoubtedly recall from last week, the program will add up the three numbers stored in memory locations 3010, 3011, and 3012 and place the sum in memory location 300f.

Let's begin my modifying one of the numbers to be added. Use the Set Values » Set Register or Memory to bring up the Set Value dialog.
Set Value Bar

Go to the text field for the register and memory location and type in x3010, the address of the memory location you wish to modify. Press the tab key and then type in a new value for this memory location.

Because the PC changed when you executed the program, you must restore it to point to location 3000. Again use Set Values » Set Register or Memory to do this. This time type PC as the register to be changed.

Now re-execute your program.

Modifying program code I

Sometimes it is necessary to modify program code. Suppose you wanted this program to add the contents of the five memory locations from 3010 to 3014; that is, you want it to add in two more numbers. By modifying one machine instruction you can make that change.

Modify that one instruction and write on the check off sheet the address and new “value” for that instruction. If you need some help, start by thinking how we ensured the original program added only three numbers.

How to do it I

Look for the one instruction that sets the initial value of the loop counter. Change the one field of that instruction that contains the initial value.

Modifying program code II

Now suppose you wanted to the program to add the contents of memory locations 3800 to 3802. How would you do this? You'll probably need to modify at least two two memory locations, one an instruction and the other a data location, in order to do this. I suggest you use memory location 300b as the one data location.

Write the modified memory locations on the check off sheet. If you need to modify more than two, just write down all the instructions you change.

How to do it II

The key is changing the first instruction so that it loads 3800 into register 1, rather than 3010. You'll probably need to listen to a mini-lecture from your lab instructor about LEA, LD, and LDR to figure out how to do this.

Going home

When you are done, exit the simulator and log out of the Linux workstation.