You've learned to read and write registers, walk through memory, and package your code as a function inside a shared library.
Now let's put those skills together and build some real algorithms.
When you need to debug one of these .so submissions, refer back to Writing from a Shared Library for the shared-library debugging pattern.
Through this module, we'll gradually build on our solutions until we create code that addresses a very common program need: turning text into numbers. Along the way, we'll learn how to write reusable assembly code, how text and numbers relate to each other, and how to reason about algorithms (and their failings!). You'll grow in knowledge, and in flags!
The Calculator
You can turn text into a number with your atoi, and a number back into text with your itoa.
Now you'll use both at once to build a small command-line calculator.
It reads an expression from argv --- a left operand, an operator, and a right operand --- as in prog 6 + 7.
The operands are strings you already know how to handle: atoi each one.
The operator is the new piece: a single character you branch on to decide what to compute, quitting on any operator you don't support.
Then itoa the result and write it to standard output, exactly as your summing program did.
We'll add one operator group at a time --- addition, then subtraction, then multiplication, then the bitwise operators, and finally the unary operators --- each reusing the atoi and itoa you've already written.
printf
Programs rarely print just one fixed string.
They print messages assembled from fixed words and changing values: answer: 13, hello, hacker, opened 3 files, and so on.
You could write each message by hand, but then every new message shape needs its own copy loops, number conversions, and write calls.
A format string is a compact recipe for building those messages.
Ordinary characters in the recipe are printed as-is, while special markers say where the next value should go.
The code that follows such a recipe is called a formatter.
printf is the traditional name for a formatter that prints its result.
This mini-module will have you build printf.
Your version will be special: it's yours!
Many other versions of printf exist as well.
For example, the "standard C library", which includes useful functions to use when writing applications in the C programming language, includes an implementation, and your commandline has it too:
hacker@dojo:~$ printf Hello
Hello
hacker@dojo:~$
We'll build up that idea in small steps: literal text, the ASCII newline byte, escaped syntax characters, decimal numbers, repeated values, strings, and finally arbitrary bytes.
30-Day Scoreboard:
This scoreboard reflects solves for challenges in this module after the module launched in this dojo.
| Rank | Hacker | Badges | Score |