A2: Subroutines and I/O
After having written your first simple program, it is now time to start thinking bigger. Most programs you have written in your life likely make heavy use of abstraction by using functions (in the context of Assembly known as subroutines). Furthermore, in various applications, it is often essential to obtain input from the user. This assignment will build and test your knowledge of both subroutines and I/O.
Assignment
Write a program that reads a number from the user, calls a subroutine to increment the number, and prints the returned value.
Subroutine
The subroutine should have the signature:
It takes a single (64-bit signed) number as its argument and returns the number incremented by 1. The subroutine itself should not print the incremented number but simply return it.
Your function must follow all calling conventions, including a proper prologue and epilogue, such that it can even be called from outside of your main program and still offer the expected functionality.
Note that overflow is considered a natural limitation. You should not handle cases of overflow explicitly.
Main Program
Your main
program should use scanf
to read a number from the terminal, pass the number to the my_inc
subroutine, and print the returned value such that the final output looks like this:
where <x>
represents the input the user types into the terminal (it does not need to be printed again) and <x+1>
represents the incremented number.
Make sure that your main
program still returns with the correct exit code (0) and not the value read from the user.
Recommended Approach
Implement an "empty"
my_inc
subroutine that, with the proper prologue and epilogue, directly returns. Implement themain
routine in such a way that it calls the subroutine before returning.Extend your
main
program with calls toprintf
for printing the prompt andscanf
for reading a number from the user.Pass the number as the first argument in the call to
my_inc
.Extend your subroutine such that it increments and returns the argument.
Finally, add the required print statement to your
main
program to print the result.
Hint: Now may be a good time to start working with the debugger.
You are using multiple library functions in combination. If things go wrong (which they likely will) it may help to check for the results of the individual parts of your program (e.g., the call to scanf
) or trace the program flow - which is greatly facilitated by a debugger.
If you have never used a debugger before or need a quick refresher on how to work with it, there is an introductory guide in the Appendix.
Required Knowledge
This assignment builds on all concepts that were required for the previous assignment. However, you will need to further extend your toolbox:
You should know how to reserve space for and access Local Variables as part of your
main
routine (such that you can allocate space for and retrieve the number read byscanf
).As you need to obtain input from the user, you should know how to Read from the Terminal.
Note on Submission
If you have completed this assignment (and you are passing all tests on CodeGrade), you can now enter the submission queue to have your work checked and signed off by a TA!
Last updated