Cross Compiling User Applications

Spring 2023

You are welcome to download Nachos to your own system. Since it uses Java, it should run on any reasonably modern system. But, user-level programs in Nachos are implemented in C and need to be compiled to the MIPS architecture. As a result, projects 2 and 3 require the installation of a MIPS cross-compiler so that you can compile C code into MIPS executables independently of the architecture that you run the compiler on.

We have installed a MIPS cross-compiler on the instructional systems for class use. But if you install Nachos on your own system, you will need to install a MIPS cross-compiler as well. We have binaries for the cross-compiler on Linux that you can download:

The Makefile in the Nachos test directory requires two environment variables to be properly initialized to use the cross-compiler. For example, the bash commands for the environment variables on the instructional machines are:
export ARCHDIR=/home/linux/ieng6/cs120sp23/public/mips-x86.linux-xgcc
export PATH=/home/linux/ieng6/cs120sp23/public/mips-x86.linux-xgcc:$PATH

On your system, you would set them as appropriate for where you placed the cross-compiler. Note that, in the shell that uses these environment variables, they will prevent you from using the the original gcc because mips-gcc will now appear first in your PATH. If you want to use the regular gcc again in that shell, you will need to reset PATH.

There are multiple stages to building a Nachos-compatible MIPS binary (all of which are handled by the test Makefile):

  1. Source files (*.c) are compiled into object files (*.o) by mips-gcc.
  2. Some of the object files are linked into libnachos.a, the Nachos standard library.
  3. start.s is preprocessed and assembled into start.o. This file contains the assembly-language code to initialize a process. It also provides the system call "stub code" which allows system calls to be invoked. This makes use of the special MIPS instruction syscall which traps to the Nachos kernel to invoke a system call.
  4. An object file is linked with libnachos.a to produce a Nachos-compatible MIPS binary, which has the extension *.coff. (COFF stands for Common Object File Format and is an industry-standard binary format which the Nachos kernel understands.)