Question: Write a simplified printf subroutine that takes a variable amount of arguments. The first ar- gument for your subroutine is the format string. The rest

Write a simplified printf subroutine that takes a variable amount of arguments. The first ar- gument for your subroutine is the format string. The rest of the arguments are printed instead of the placeholders (also called format specifiers) in the format string. How those arguments are printed depends on the corresponding format specifiers. Your printf function has to support any number of format specifiers in the format string. Any format specifier after that may be printed without modification. Unlike the real printf, your version only has to understand the format specifiers listed below. If a format specifier is not recognized, it should be printed without modification. Please note that for this exercise you are not allowed to use the printf function or any other C library function, except for the putchar function to output a single ASCII character. Your function must follow the proper x86 64 calling conventions. It must accept any num- ber of arguments (e.g., tens of arguments) like any standard printf implementation.
For details on how to pass and accept a large number of arguments. Supported format specifiers: %d Print a signed integer in decimal. The corresponding parameter is a 64 bit signed integer. %u Print an unsigned integer in decimal. The corresponding parameter is a 64 bit unsigned integer. %s Print a null terminated string. No format specifiers should be parsed in this string. The corresponding parameter is the address of first character of the string. %% Print a percent sign. This format specifier takes no argument. Example: Suppose you have the following format string: My name is %s. I think I’ll get a %u for my exam. What does %r do? And %%? Also suppose you have the additional arguments “Piet” and 10. Then your subroutine should output: My name is Piet. I think I’ll get a 10 for my exam. What does %r do? And %?


  • Hints To get started you may divide the work in a number of steps. Note that these are just hints, you do not have to follow these steps to finish this assignment. 1. Write a subroutine that prints a string character by character, for example by using putchar. 19 2. Modify the subroutine to recognize format specifiers in the format string. Initially, you can discard the format specifiers rather than process them. Characters that are not part of a format specifier can be printed as before. 3. Implement the various format specifiers. It may help to implement %u before %d. 4. It may help to store all input argument registers on the stack at the start of your function, even if you don’t end up using them. Bonus points: support flags and width fields (+250 points) Extend your printf function to support the flags and width fields on a format specifier. You can find a comprehensive definition in any C standard library reference


  • (e.g., https://www. cplusplus.com/reference/cstdio/printf/) or on Wikipedia at https://en.wikipedia.org/ wiki/Printf_format_string. To get the bonus points, you must support all of the following for the %d, %u, and %s format specifiers: 1. the minus (on %d and %u), plus (on %d and %u), space, and zero flags, including any combination of flags; 2. the width field given as an integer in the format string; and 3. the width field given as an argument to printf, indicated by an asterisk in the format string.

  

Step by Step Solution

3.42 Rating (161 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Heres a simplified implementation of the printf subroutine in C c include stdarg h include stdbool h ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!