CS-220 Spring 2018 Lab 1 - Lost your marbles?

Getting there and Getting Started

The lab takes place at EB G-7 -- the room directly across from the main entrance of the engineering building. You will need your Binghamton University ID card to get into the lab. Please let the TA know if your ID card does not work with the badge reader on the G-7 door.

You will also need the LDAP password that has been e-mailed to you. Log on to one of the machines in G-7 with your bmail userid and the password sent to you in the e-mail. (You can change the password at the https://www.cs.binghamton.edu/~sysadmin/ web page.)

Once you are logged on to the system, open a web browser so you can see these instructions easily. (You may want to bookmark the class web page, since we will be using it for all labs, etc.) Also, open a terminal window. You are now in a UNIX command line environment. If you would like to learn more about UNIX, there is a good tutorial here

You start off in your home directory. I would suggest making a CS220 sub-directory to keep all your stuff for this class separate from other classes. I would also suggest making a lab01 sub-directory of the CS220 directory for this lab.

Writing your First C program

Edit file "hw.c". I recommend using the gedit editor, which can be started using the command gedit hw.c& Then type the following text into the file hw.c

#include <stdio.h>
int main() {
   printf("Hello World - welcome to C!\n");
   return 0;
}

Notice that as you type, the gedit editor highlights and colors your code, based on the C syntax. Gedit knows to do this because of the .c file type.

Save your file. (Note... if you forgot the ampersand at the end of the gedit command, your terminal will be locked up, waiting for gedit to end. If this is the case, quit out of gedit, and retype the gedit command with the ampersand at the end. This will run the editor in the "background", and allow you to use both gedit and the terminal window.)

In your terminal window, type the command: gcc -g -Wall -o hw hw.c This will compile the program in file hw.c, and create an executable file called "hw". If you have any compile errors, they will get printed to the screen, and you need to fix them. Compiler warnings will get printed to the screen as well. If there are errors, the executable file will not be created. If there are warnings, the executable file is created, but it might not work as expected. If nothing gets printed to the screen, there were no errors or warnings... you did good!

If you want to learn more about the gcc command, here is a link to the On-line gcc manual.

Once your file compiles cleanly, run the resulting command/executable file by typing ./hw

Running the GDB debugger

Start the debugger by typing: gdb hw. This will result in several messages from gdb about loading your program and starting the debugging environment, and end with the gdb prompt: (gdb).

Note that if at any point in running gdb, you get into some state that is unexpected and doesn't work, you can always type the gdb command quit at the gdb prompt, and start this section again.

At the GDB prompt, type list to list the source of your program.

At the GDB prompt, type break 3 to set a break point at line 3 of your program.

At the GDB prompt, type run. This will start your program and run it until either your program ends, or it hits a break point. Luckily, we have already set a break point at line 3 of your program, so it should stop there. What shows up on your screen when the program stopped?

At the GDB prompt, type next to move to the next line of your program. What shows up in the terminal window?

Continue typing next until your program finishes. How do you know your program finished? Note that you may have to type next several times before it becomes obvious that gdb has finished.

Note that even though your program has finished, the gdb prompt is still open.

Type quit at the gdb prompt to exit out of gdb, and get back to the command line. If gdb tells you that an inferior process will be killed, that's OK... type "yes" at the gdb prompt to exit anyway.

For more information about the GDB debugger, see the On-line GDB manual

Using Example Code

Navigate in your web browser to the Example C Programs link from the class web page. Scroll down to the xmp_gdb link in that page, and click on that link. Hit the right mouse button with your cursor over the Makefile link and choose the "Save As..." option. (This might be different depending on what browser you are using, but you get the idea. Save the Makefile in your CS220/lab01 directory. Do the same thing with file marbles.c.

In the terminal window, run the ls command to see what files are in your current directory. There should be four files by now... Makefile, hw, hw.c, and marbles.c.

Next run the command make test. What happened?

Run the gdb debugger either using the command gdb marbles or make gdb (which runs the gdb marbles command for you.) Set a breakpoint at the beginning of the main function, and then use the run gdb command to get gdb started. Step through the code using a combination of the next and/or step gdb commands. Set another break point or two, and run the "continue" gdb command. Print out variable values using the print command. When you are done, quit gdb with the quit command.

Modifying marbles.c

Let's change the rules for the marbles game. The new rules are as follows:

Change the marbles.c program so that it follows these new rules. You won't need to modify the nextMarble function, but you will have to make several changes to the main function. Test your code to make sure it is working correctly. If you have problems, you might try running your marbles code under the gdb debugger to see what is going wrong.

Submitting your Results

Go to MyCourses using your web browser, and open the Spring 2018 - Comp Sys II:Arch &Prob (CS-220-A 0) or Spring 2018 - Comp Sys II:Arch &Prob (CS-220-B 1)course. (Notice that you also have a CS-220-A50 or B56 lab link. We will not be using these at all this semester. I usually remove these from view in the My Courses tab.) Click on the "Content" hyperlink in the left column. There is a "Lab Submissions" folder on the content page. Click on the "Lab Submissions" hyperlink, and you will see "Lab 01 Submission". Click on that hyperlink, and use the Browse My Computer button to upload "marbles.c" from your cs220/lab01 directory. Don't forget to hit the submit button.

If you make a mistake submitting your lab, you may resubmit as often as you would like to. The TA's will only grade the last submission you make. (Of course, if you submit after the due date, you will be subject to late penalties.)

Grading your Results

Each lab submission is worth 10 points. Your entire lab grade will be made up of an average of all labs during the semester. Throughout the course, deductions will occur for the following reasons:

For this lab, as long as your marbles.c program compiles and runs to completion, and the submission is correct and on time, you will get full credit.