CS380C:  Assignment 2
x86 code generator


Assigned: Monday, February 10th, 2025
Due: Tuesday, February 18th, 2025 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 again be more testcases.

 

Submission instructions

The submission instructions below are exactly the same as for Assignment 1.

If using Java, submit (to Canvas) the following:

  1. compiler.jar
    A runnable .jar file of your project. Please refer to online resources to learn how to create a runnable .jar file for your Java project. Make sure that your .jar file can be executed using the command line mentioned in the evaluation section below. Preferred Java version is as follows:
    openjdk version "19.0.2" 2023-01-17
    OpenJDK Runtime Environment (build 19.0.2+7-44)

    If you use another version of Java, make sure to include the version information. For testing, we'll compile your source code into the jar file for the correct version if needed.
  2. source.zip
    A .zip file containing all your source files, including any libraries you may have used. Ideally, we should be able to re-create the .jar file from the source, if needed.

If using C++, submit (to Canvas) the following:

  1. Makefile
    This Makefile should build an executable named compiler. In other words, make && ./compiler test1.bali output.s should read the Bali program in test1.bali and output it as an x86 program to output.s.
  2. source.zip
    Like with the Java requirements, please include all your source files. This zip should contain your Makefile as well.

If using Python, submit (to Canvas) the following:

  1. compiler.py
    We should be able to run python3 compiler.py test1.bali output.s to read the Bali program in test1.bali and output it as an x86 program to output.s.
  2. source.zip
    A .zip containing any supporting files. This zip should contain compiler.py.

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.