CS380C:  Assignment 3
x86 code generator

Assigned: Tuesday, September 20th, 2016
Due:      Thursday, September 29th, 2016 at 11:59pm

    In Assignment 1, you implemented a Bali compiler that generated SaM code. In this assignment, you will retarget that compiler to generate x86 code.

How to proceed:

  1. The x86 ISA is much more complicated than the SaM ISA. However, we will need only a small subset of the ISA for this assignment. To help you get up to speed quickly, we have written a guide that you should read carefully before starting the assignment. You can find it here: http://www.cs.utexas.edu/~pingali/CS380C/2016-fall/assignments/x86CodeGen/x86-help.html
  2. There are many assemblers for x86 code, each with its own syntax. To simplify grading, we require that your output run on the SASM x86 IDE and emulator. SASM has a very nice GUI and it makes writing and debugging x86 code relatively easy. For example, you can single-step through x86 code and track all the registers and memory, which you cannot do if you run directly on an x86 processor. SASM runs out of the box on Windows machines; installation on Macs and Linux machines is not much more difficult. After reading the guide, you should download and install SASM on your computer.
  3. You can find a factorial program we wrote by hand here. The code mimics the kind of code a simple code generator might produce from Bali programs. Run this example, and study the code to get a feel for the ISA and for the kind of assembly programs you must generate.
  4. Write a few x86 programs yourself using the SASM IDE. Fibonacci is a good test for whether you understand procedure calls, returns, and stack frames.
  5. Once you are comfortable with SASM syntax and the x86 ISA subset you need, retarget your Bali compiler to produce x86 code.

Test cases:

You can use the testcases for assignment 1 for debugging. For grading, there will be more testcases.

What to turn in:

A .tar.gz/.tgz/.zip file, which contains a directory [YOUR_NAME]. This directory contains

  • BaliX86Compiler.jar: The executable jar file if you use Java, the running command will be java -jar BaliX86Compiler.jar [SOURCE] [TARGET]. Do include all the pacakages you used in your jar file.
  • Makefile: If you use C++ (gcc 4.9.2). It should generate the binary file BaliX86Compiler by make, the running command will be ./BaliX86Compiler [SOURCE] [TARGET].
  • src/ : Source file directory.

About Assembly Code:

I will use the following commands to generate the executable file for your assembly code.

gcc /usr/share/sasm/NASM/macro.c -c -o macro.o -m32
nasm -g -f elf32 /usr/share/sasm/include/io.inc -i /usr/share/sasm/include/ -o io.o
nasm -g -f elf32 [YOUR_ASSEMBLY_CODE] -i /usr/share/sasm/include/ -o tmp.o
gcc macro.o io.o tmp.o -m32 -o a.out

a.out should print the return value of main method to stdout, end up with a newline.