Assignment 1, CSC431, Fall 2019
1 Getting Started
1.1 Installing Tools
1.1.1 Linux
1.1.2 Mac
1.1.3 Windows
1.2 Using Tools
2 Continuing on
3 Submitting
7.5.0.16

Assignment 1, CSC431, Fall 2019

Next, use this github classroom link to create a repo for the first milestone.

Next, in the language of your choice, write a compiler for this language:

Expr ::= Int

       | Int + Int

... where an Int is an integer between -(2^16) and 2^16.

Specifically, your program should accept an input file written in this "language" and output a file written in assembly.

1 Getting Started

Before we think about generating assembly files, we need to figure out how to actually run them once we’ve generated them.

The translation from assembly to machine code is amusing but VERY VERY TEDIOUS, so we’re going to use an assembler, "nasm", to compile assembly into machine code.

Also, this course isn’t going to be spending much time talking about linking and file formats, so we’re going to use a linker to generate files in an executable format that your machine can use. In this course we’re going to use "clang", and its accompanying tool "objdump".

1.1 Installing Tools

Both nasm and clang are open-source and widely available.

1.1.1 Linux

You should be able to install both nasm and clang using your favorite package manager.

You’re going to have to be familiar with a terminal application (each Linux distribution has its own favorite, last time I checked) and some shell (bash, zsh, whatever).

1.1.2 Mac

You can probably install nasm using your favorite package manager, either homebrew or macports or does anyone still use fink?

Installing clang should be much easier, because clang is actually the default compiler on MacOS 10.14 (and most earlier versions). Installing clang is therefore most easily accomplished by installing the Developer Tools using the App Store. This is a terrifyingly ginormous download, though, so do it when you have a nice fast connection and a bit of free time.

Note that running the various commands below is going to require that you’re familiar with the Terminal application, which can run a shell program for you.

1.1.3 Windows

On Windows I believe you have a couple of choices. Specifically, you can either run a Linux toolset in the linux subsystem that can be enabled on modern Windows 10 systems, or you can run on the Windows side directly.

If you use the linux subsystem, you’d probably be using a package manager, as described in the Linux system.

If you want to run on the Windows side, you can download and install both tools directly. For nasm, go to nasm.us. It looks like you can get the full set of LLVM tools (including clang) from The LLVM download link. I recommend the most recent one that has a windows download.

Finally, you’re going to want to use a shell, as with the other platforms.

1.2 Using Tools

Once you’ve got these tools, let’s try actually taking a (one-line!) assembly program and compiling it into an executable.

In the Lab 1 directory, you should find the two files "wrapper.c" and "our-code.s". Read them. Make sure the C file makes sense. Make sure the single instruction in the assembly file makes sense.

Note: we’re using the wrapper.c file for now to simplify the task of generating some assembly code that actually prints output. That is: we’re cheating. But it’s okay.

To generate an executable, we need to convert our assembly file "our-code.s" into an object file using nasm as an assembler.

On a linux or mac platform, we’d do this by running

% nasm -f elf64 our-code.s

On a mac, it would look like this:

% nasm -f macho64 our-code.s

... and here’s Windows:

% nasm -f win64 our-code.s

Did it work? Well, let’s take a look. We can use objdump to disassemble the code:

% objdump -x86-asm-syntax=intel -disassemble-all our-code.o

Hopefully you can see the code, and how it corresponds to the input file.

Okay, now we need to compile the c file that we’re using as a crutch, using "clang":

% clang -m64 -c wrapper.c

Make sure that it worked, using objdump.

Finally, we’ll link the two object files:

% clang -m64 -o a.out our-code.o wrapper.o

Now, run the resulting "a.out" executable.

Yay! It worked!

Or... actually, this is CS, so it probably didn’t work. Too bad. Figure out the problem!

2 Continuing on

Now you know how to assemble and link, starting from an assembly file. Your task for Milestone 1 is to write a compiler that generates assembly code for the language specified above.

This is VERY EASY. When you’re done you’re going to think you did it wrong. Yes, it’s just a branch and then writing a few lines of printout to a file.

3 Submitting

You need to submit by committing (and pushing!) your code to the github classroom repo. You also need to demo your project to me, and this will be the primary way that I see your code and give you credit.