This assignment will take you through 2 tasks implementing and using functions in assembly. You will be also asked to write some C (pseudo-)code as a blueprint for your assembly. The C code does not need to compile or run, but needs to reflect the functionality. Feel free to rely on your Java knowledge here. If in doubt, leave comments.

This assignment will also introduce separate compilation. For one of the tasks you will write a function called by an already implemented main function. For the other, your task will be to write a main function for an already implemented function. Read the instruction carefully to make sure you understand what is being asked in each of the tasks.

For each of the required functions, you need to follow the Assembly Design Recipe from class. You can optimize your code once you’ve written it – if, for example, there is some repetition that can be trivially removed.

Task 1: Mystery function

You are provided with a “mystery library”. This pre-compiled library contains a single function, crunch, which takes two signed long integers and returns a long integer.

Your task is to complete the mystery program by implementing the main{.c} function in mystery-main.s, which needs to call the provided crunch function. This is also called a “driver”. When compiled, the program should have the following features and behavior:

  1. Accept exactly two arguments. You can assume that, when provided, these arguments are valid signed long integers.

  2. The provided crunch function should be called with these two numbers, converting them from strings as necessary.

  3. Based on the result from the function, the program should print one of the following strings (with a newline at the end) and exit with an exit status of 0:
    • hat, if crunch returned a negative integer
    • tea, if crunch returned 0
    • beer, if crunch returned a positive integer.
  4. If fewer or more than 2 arguments are provided, the program should print “Two arguments required.”, followed by a newline, and exit with a status of 1.

First, write the main{.c} function in C and save it in mystery-main.c, where we provided a “stub” for you. The program does not have to compile, but it should be a fairly accurate high-level representation of the assembly program. Do not spend too much time on this, but give it your best shot. Try to use your knowledge of Java and provide comments if you are struggling with C.

Second, implement an assembly version of the main{.c} function in mystery-main.s. Your program must compile using the provided Makefile by running make mystery. The command make mystery will combine libmystery.a (provided by us) and mystery-main.s (written by you) into the executable mystery.

Sample test cases with mystery:

$ ./mystery 1 2
tea
$ ./mystery 2344 12
beer
$ ./mystery -1345 321
hat
$ ./mystery 1
Two arguments required.

Task 2: The Maximum of an Array

The second task is to write the function:

unsigned long array_max(unsigned long n, unsigned long *items)

This function will return the maximum value of an array of long integers $\ge 0$. The first argument provided is the number of elements, the second argument is the address of the first element.

You do not need to write a C version of this function, but we recommend doing so.

We have provided the driver program in array-max-main.c, which processes the command line arguments and calls the array_max function. The Makefile will compile both array-max-main.c and your implementation of the array_max function to produce the executable array-max. Once compiled, the interactions with array-max should look as follows:

$ ./array-max 1 2
2
$ ./array-max 42 1
42
$ ./array-max 3 1 5 8 2 4 8 20 1
20

Your program must compile without any modification to the provided array-max-main.c file, using the provided Makefile. The command make array-max will combine array-max-main.c (provided by us) and array-max.s (written by you) into the executable array-max.

Using the Makefile

We have provided a Makefile for you. You can use it as follows on the command line:

Deliverables

Task 1

Modify the files mystery-main.s (implementation) and mystery-main.c (blueprint) and commit them to your repository.

`mystery`

Task 2

Modify the file array-max.s and commit it to your repository.

`array-max`


Ben Weintraub © 2025
Site Last Updated May 31, 2025 at 16:29:55 UTC