Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

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

Step: 1

Heres a simplified implementation of the printf subroutine in C c include stdarg h include stdbool h ... blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intermediate Accounting

Authors: kieso, weygandt and warfield.

14th Edition

9780470587232, 470587288, 470587237, 978-0470587287

More Books

Students also viewed these Accounting questions

Question

What is the definition of fair value?

Answered: 1 week ago