Compiling and running register machines

To run register machine programs in C++ in the computer labs follow the instructions here carefully. If you prefer to use your own computer, or a different C++ system that is fine but you may have to modify these instructions slightly.

1. Using the command line

It is recommended that you organise your work carefully into folders and subfolders (also called directories). You will have a space on disk or networked folder for your own documents (usually "U:" on university computers). You should make a folder for 2NP work here (called "2NP" perhaps) and inside this you can have folders for each week and inside those folders for your actual project. You can make some of these folders now using the windows file explorer or your preferred file manager. You will end up with a folder called (say) "U:\2NP\week1\firstproject". Do not use spaces in your file or foldernames. Some special characters like $ - and _ are OK in file names or folder names.

We start with the very simple program that creates a register with the value 2 and prints it. It looks like this:

  USES(x)
  INCR(x)
  INCR(x)
  PRINT(x)

The first step is to open the Notepad++ editor to type this in. Use "file->new" to make a new document and "file->save as". You can save it as first.cpp (which should appear as a "C++ source file") in the folder you have just created (or choose or make a new folder in Notepad++ as you save).

For this particular program you will also need to put some lines above and below the program to make sure the system understands what sort of program it is and where it starts and ends. Continue editing until it looks like this:

#include "regmac.h"

MAIN

  USES(x)
  INCR(x)
  INCR(x)
  PRINT(x)

END

The idea is that the computer should first read (or "#include") a special file called "regmac.h" and then read a program delimited by "MAIN" and "END". All your register machine programs will look like this.

You will also have to download "regmac.h" and save it in the same directory (folder) as your project. Do so now by right clicking the link. You do not have to read or understand this file and please don't change it or else things will stop working!

To access the comand line in windows, press the start button and type "cmd". Then click the "Command Prompt" app that appears. Here are some basic command line commands.

dir
give a list of all files in the current folder
U:
change to drive U:
cd foldername
change to the folder with name "foldername"
cd ..
change up to the parent folder containing this one
mkdir newfolder
create a folder with name "newfolder"
exit
quit the command prompt app

Using the command line, navigate to the folder "U:\2NP\week1\firstproject" containing the file that you just edited and also containing "regmac.h". Verify that these files are present using "dir".

Before the program can be executed (run) you must compile it. This is to create a special machine readable version of the program that will work very quickly.

If your program is in a file called "myfile.cpp" to compile it you might enter the following command on the command line,

c++ myfile.cpp -o myfile

The gets the C++ compiler to read your file and create output (-o for "output") called "myfile.exe". If there are any problems you will get a list of error messages (this can be quite long). If there is no error and you get the command line prompt again then it has succeeded. You can test this with dir to see a list of files (including "myfile.exe") and you can run the program by entering myfile (or myfile.exe) at the command prompt (or ./makefile on linux or macs). Note that compiling a C++ source file creates an "app" or "executable" exe file, which is like a new command available at the command line.

There are a number of alternatives to the compile command above. On my system "g++" is equivalent to "c++". Also the command c++ myfile.cpp without the output name defaults to an output called "a.exe" ("a.out" on linux or macs) which you can run with the command a (just one letter) or ./a.out on linux or macs. The command mingw32-make myfile works slightly differently but has the same effect: it tries to make a file called "myfile.exe" and by looking at the files in the current folder it is clever enough to realise that it can do this my compiling "myfile.cpp" using the installed C++ compiler, which is what it does. (This command is probably just make myfile on linux or macs.)

Exercise.

Spend some time checking that this works as you expect. Make a note of the commands you like best that work for you.

Exercise.

Modify the program (in Notepad++) and save it, recompile it and run it again. (Don't forget to save, and don't forget to recompile after you have saved.) Make sure you understand what happens. If there are compiler errors, correct them in the editor save and recompile. (I don't expect you to understand the exact details in the compiler error messages at this stage, but you should be able to see what's wrong easily enough.)

There are plenty of example programs in these pages to give you ideas of things to try.

Remark.

It is also possible to run your program directly from the file explorer by double-clicking the executable file "myfile" (you may not be able to see ".exe" in the file explorer). What will probably happen is that when your program is run a console flashes up quickly and then closes equally quickly so you might not even see it. One quick fix to this is to add a line INPUT(end) just before END in your ptogram. Then the computer will ask you for a value for an input variable called "end" just before it finishes. The value will be ignored, but the program will stop for long enough that you can see what it has done.

If you want to make a new register machine program, you can do so in the same folder (provided they don't have the same names and you don't use a.exe for both) but it may be better to make a new folder for your new program. If you make a new folder you will need a separate copy of regmac.h in the new folder.

Remember! You must SAVE your edits in the Notepad++ editor. Do this regularly to make sure your work is not lost. After you have saved your work you need to RECOMPILE it before you can run it.

The most common mistakes that can be made when working like this are: (a) forgetting to recompile; (b) forgetting to save before you recompile; or (c) having two versions of the same file and editing one but compiling the other. Please check this carefully before putting your hand up to ask about a problem!

2. Summary

You have now learnt the basics of how to compile and execute a program and will have an idea of the process the computer goes through and the files that are needed. Working from the command line is often the simplest and most straightforward way of working, and it is certainly one of the easiest ways of seeing what is going on.

You will learn that the compiler will complain at almost anything that is not typed correctly. Complete attention to detail is essential in everything you type on the computer so that the compiler can accept what you have written.

Even if the compiler accepts what you write and compiles it, you will probably learn that it is very easy to make a mistake in writing your program, so programs have to be written, tested, and modified, sometimes many times, until they are right.

You will probably also learn that the actual set of numbers the computer uses for "natural numbers" is very limited and computer numbers sometimes behave in a strange way. We will discuss this in detail later.