A6: Printf

500-800 points

You probably have used the printf function (or its relatives) many times already. With that, you likely have come to love and appreciate the abstractions it offers. If you haven't, there is a chance you will do so after having finished this assignment.

Ignoring its deserved place in the coding hall-of-fame, printf is just another function like any other. To prove that (because it needs to be proven, why should you just trust us here?!) in this assignment you will implement your own printf function, that matches the original in quite some of its functionality.


Printf

The printf function is pretty flexible, especially in its number of arguments. How many arguments do you need to give it? 1. How many arguments can you give it? Well, pretty much unlimited.

printf takes a variable number of arguments, so the argument count is not fixed in its signature. The first argument is always the so-called format string. This argument is a pointer to a memory location of the first character of the string to print. This string, however, may not only include "regular" characters but also so-called format specifiers (yes that's the official name of the %ds or %us that you've been using so far). Whenever the function encounters such a specifier in the format string it will look at the next argument in the argument list for the correct substitution (at least for most specifiers, depending on the type).


Assignment (500 points)

Implement a (simplified) printf subroutine. Unlike the full printf, your version only has to understand the format specifiers listen below. If a format specifier is not recognized, it should be printed without modification. Your function must follow the x86_64 calling conventions. It must accept any number of arguments (so even tens or hundreds), like any standard printf implementation.

Format Specifiers

Your implementation should support the following format specifiers:

  • %d: print a signed, 32-bit integer in decimal

  • %u: print an unsigned, 32-bit integer in decimal

  • %s: print a null-terminated string - no format specifiers should be parsed in this string

  • %%: print a percentage sign

Example:

Suppose you have the following format string:

"My name is %s and I am %u years old. My favorite ASCII character is %%."

and the additional arguments: "Lennart" and 21, then the output of your subroutine should be:

My name is Lennart and I am 21 years old. My favorite ASCII character is %.

Library Functions

You are not allowed to use any C library functions except putchar in your implementation. A working solution that uses additional library functions will not be accepted.

You are of course encouraged to structure your implementation using helper functions written by yourself (in Assembly).

Main Program

The framework includes a C-File with a number of calls to your implementation. You can build and run your program such that it uses the frame file for the main routine with:

make a6-frame

Have a look at the a6-printf-frame.c file to understand each of the inputs. The output folder further gives the complete output that is expected from your implementation.


Bonus (300 points)

Your implementation so far can print basic signed and unsigned numbers. A large part of the functionality of printf, however, comes from the additional formatting options that can be specified. To get an additional 300 bonus points, you have to add support for width options including some flags (see the list below) to your implementation. You can find a comprehensive definition in any C standard library reference (like this one). Your implementation must support all of the following:

  • width specifiers, both as part of the format specifier and as an additional argument (represented by an asterisk in the format specifier)

  • the minus, plus, space, and zero flags - only for %d and %u specifiers

  • any combination of flags in any order

It may help to have a look at the tests for the bonus part to understand the function of these flags. Otherwise, it is also recommended to simply call the C standard library printf with some of these format specifiers and check for its output.

Main Program

As for the first part of the assignment, the given frame file also provides sample calls to your implementation that make use of the width specifiers and flags required for this part. You can build and run your program such that it uses the frame file with the bonus inputs with:

make a6-frame-bonus

The expected output can again be found in the output folder.

Last updated