AIM: To get acquainted with MIPS assembly language and the system calls. Partial marks will be awarded for incorporating interactive interface as spec- ified, appropriate use of system calls for printing and taking inputs, suitable commenting and correct implementation of the logic.
INSTRUCTIONS: Make one submission per group in the form of a single zipped folder containing your source code(s). Name your submitted zipped folder as Assgn 2 Grp GroupNo.zip and (e.g. Assgn 2 Grp 25.zip). Inside each submitted source files, there should be a clear header describing the assignment no., problem no., semester, group no., and names of group members. Liberally comment your code to improve its comprehensibility.
Question 1 (Tutorial: Do not submit the solution)
Write a complete MIPS-32 program that –
- Reads one positive integer (n > 0) with the prompt – “Enter a positive integer:”. (After the input number is collected from the user, there should be sanity checking to ensure that the integer is positive.)
- Calculate the sum of all integers from 1 to n (including n).
- Accordingly, print the calculated sum with the message – “The sum of the
first < n > integers is ”
Steps to follow in QtSpim:
- Write your MIPs code in a text file with “.s” extension.
- Open QtSpim and load the < filename.s > file.
- You can run the code by clicking on the “play” button at the top.
1
- Put your inputs in the console. The output will be displayed.
- To debug, you can execute the program in a single step fashion and reading the intermediate values in the register.
- Everytimeyouexecutetheprogram,youneedtoreloadthe<filename.s> file.
Question 2
Write a complete MIPS-32 program which:
- Reads two positive integers with the prompt – “Enter the first positive integer:” and “Enter the second positive integer:”. (After the input num- bers are collected from the user, there should be sanity checking to ensure that the integers are positive.)
- Calculate their GCD by repeated subtraction as given in the below al- gorithm (Algorithm 1). DO NOT USE ANY INTEGER DIVISION IN- STRUCTION, EVEN IF IT IS AVAILABLE IN MIPS-32.
Algorithm 1 Find GCD of two numbers
1: 2: 3: 4: 5: 6:
procedure GCD(a, b)
if a == 0 then return b; while b ̸= 0 do
if a > b then a = a – b;
else b = b – a; return a
◃ The GCD of a and b
3. Accordingly, print the calculated GCD with the message – “GCD of the two integers is: ”.
Question 3
Write a complete MIPS-32 program which:
- Reads a positive integer n ≥ 10 with the prompt – “Enter a positive integer greater than equals to 10:”. ( After the input number is collected from the user, there should be sanity checking to ensure that the integer is≥10. )
- Determines whether it is prime or composite. Prime numbers are con- sidered as the numbers which retain only two factors i.e. one and itself. The integer which can be obtained by multiplying the two smallest pos- itive numbers and includes at least one divisor other than 1 is known as composite numbers.
2
3. Display the finding of your program with a proper message at the end by printing “Entered number is a PRIME (or COMPOSITE) number.”.
Question 4
Write a complete MIPS-32 program which:
- Reads an integer with the prompt – “Enter a positive integer:”. ( After the input number is collected from the user, there should be sanity checking to ensure that the integer is positive.)
- Checks whether it is a perfect number. A perfect number is equal to the sum of its proper divisors (including 1 and excluding itself). The smallest perfect number is 6, which is the sum of 1, 2, and 3. Some other perfect numbers are 28, 496, and 8128.
- Accordingly, prints the message – “Entered number is (not) a perfect number.”.
3




