A1: Subroutines and I/O
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.
Required Knowledge
In order to fulfil this assignment, you will need to further extend your toolbox:
Last updated