Converting a C program to MIPS assembly language. Must use QtSpim and deliver .asm files.
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
System Call Code
Arguments
Result
print_int
1
$a0 = integer
print_float
2
$f12 = float
print_double
3
$f12 = double
print_string
4
$a0 = string
read_int
5
integer (in $v0)
read_float
6
float (in $f0)
read_double
7
double (in $f0)
read_string
8
$a0 = buffer, $a1 = length
sbrk
9
$a0 = amount
exit
10
print_character
11
read_character
12
open
13
address (in $v0)
$a0 = character
character (in $v0)
$a0 = filename,
file descriptor (in $v0)
$a1 = flags, $a2 = mode
read
14
$a0 = file descriptor,
bytes read (in $v0)
$a1 = buffer, $a2 = count
write
15
$a0 = file descriptor,
bytes written (in $v0)
$a1 = buffer, $a2 = count
close
16
$a0 = file descriptor
exit2
17
$a0 = value
0 (in $v0)
1
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:
jal hello
li $v0,10
syscall #exit the program gracefully.
hello:
la $a0, str
li $v0,4
syscall # to print.
jr $ra
2
#include
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;
}
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;
}
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 n)
{
for (int i = 0; i < n; i++)
printf("%d \n", arr[i]);
}
3