Objective
To learn the fundamentals of MIPS architecture through the use of its assembly language and the SPIM simulator.
Description
You are to write a complete program in MIPS assembly language that behaves exactly as the included C program. This program contains four functions in addition to the main() one. Your solution must contain all five C routines as they have been coded in the example. In essence, your solution must follow their dependence as determined by the way they are invoked in the C code included. You are to use the MIPS simulator QtSpim. A list of SPIM system calls is provided in the lecture notes and also supplied below for quick reference:
Service
print_int print_float print_double print_string read_int read_float read_double read_string sbrk
System Call Code
1 2 3 4 5 6 7 8 9 10
Arguments
$a0 = integer $f12 = float $f12 = double $a0 = string
$a0 = buffer, $a1 = length $a0 = amount
$a0 = character
$a0 = filename,
$a1 = flags, $a2 = mode
Result
integer (in $v0) float (in $f0) double (in $f0)
address (in $v0)
character (in $v0)
file descriptor (in $v0)
exit
print_character 11
read_character 12
open 13
1
read write
close exit2
14 $a0 = file descriptor,
$a1 = buffer, $a2 = count
15 $a0 = file descriptor,
$a1 = buffer, $a2 = count
16 $a0 = file descriptor 17 $a0 = value
bytes read (in $v0) bytes written (in $v0) 0 (in $v0)
Both the function call and return must be done by the pair jal function-name and jr $ra of MIPS instructions. The program that follows illustrates this requirement where the function hello is invoked by a jal hello call in the main function. The jr $ra instruction in function hello illustrates how this function returns execution to the main function.
.data
str: .asciiz "Hello Word!.\n"
.text
.globl main #necessary for the assembler
main: hello:
jal hello
li $v0,10
syscall #exit the program gracefully.
la $a0, str
li $v0,4
syscall # to print.
jr $ra
2
Grading scheme
The following is a tentative grading scheme.
1. Source Code.
a. [10%] Programming style.
-
[5%] Layering.
-
[3%] Readability.
-
[2%] Comments.
b. [90% total] Correctness.
The five functions will be graded according to the percentages stated in the table
below.
Function implemented %
getMax countSort radixSort printData main
What to Submit
20 30 20 10 10
The following are to be submitted to your co-instructor:
-
Copies of all .asm files you created for this exercise as well as
-
Screenshots of the results produced by your solution.
All above listed information must be placed in a Microsoft compressed (zipped) folder (.zip). Your .zip folder must be named: 404 Programming Assignment 1- Your Name. Marks will be deducted if you do not follow this requirement.
3
#include <stdio.h>
int getMax(int arr[], int n) {
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx) mx = arr[i];
return mx; }
void countSort(int arr[], int n, int exp) {
int output[n];
int i, count[10] = { 0 };
for (i = 0; i < n; i++) count[(arr[i] / exp) % 10]++;
for (i = 1; i < 10; i++) count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (i = 0; i < n; i++) arr[i] = output[i];
}
void radixSort(int arr[], int n) {
int m = getMax(arr, n);
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, n, exp); }
void printData(int arr[], int start, int len) {
if( start >= len ) return( 0 );
printf("%d \n", arr[start]);
printData(arr, start+1, len); }
int main() {
int arr[] = {7, 9, 4, 3, 8, 1, 6, 2, 5}; int n = sizeof(arr) / sizeof(arr[0]);
radixSort(arr, n);
printData(arr, n); return 0;
}
4