Converting a C code to assembly MIPS I am having trouble converting this C code
ID: 3562567 • Letter: C
Question
Converting a C code to assembly MIPS
I am having trouble converting this C code to MIPS
Assignment Description:
Implement a MIPS assembly language program that defines main, readArray and sumEvenOdd procedures. The readArray takes an array of integers, its length (int), and how many integers to be read as it parameters and reads in integers from a user to fill the array. Note that if you change the content of the array in the readArray procedure, then the main and other procedures will also see the new content of the array.
The sumEvenOdd procedure takes parameters of an array of integers, its length, and whether to compute a sum of even or odd numbers. It calls the readArray procedure to read in integers into an array, computes a sum of even or odd numbers and prints it, then it should also print the content of the array.
The main needs to call the sumEvenOdd procedure twice and compares the two returned values and print out if they are same, or which one is larger. If your program causes an infinite loop, press Control and 'C' keys at the same time to stop it. Name your source code file assignment6.s.
//The readArray procedure reads integers from user input
void readArray(int array[], int length, int howMany)
{
int num, i = 0;
while (i < howMany && i < length)
{
printf("Enter an integer: ");
//read an integer from a user input and store it in num
scanf("%d", &num);
array[i] = num;
i++;
}
return;
}
//The sumEvenOdd computes the sum of even numbers
//in the array if the parameter "even" is 0,
//and computes the sum of odd numbers if
//the parameter "even" is not zero.
//Then it returns the sum.
int sumEvenOdd(int array[], int length, int even )
{
int i = 0;
int sum = 0;
int howMany = 0;
printf("Specify how many numbers should be stored in the array (at most 8): ");
scanf("%d", &howMany);
readArray(array, length, howMany);
while (i < howMany && i < length)
{
if (even == 0 && array[i]%2 == 0)
{
sum = sum+array[i];
}
if (even != 0 && array[i]%2 != 0)
{
sum = sum+array[i];
}
i++;
}
if (even == 0)
printf("The sum of even numbers is %d ", sum);
else
printf("The sum of odd numbers is %d ", sum);
i = 0;
printf("The array content is: ");
while (i < howMany && i < length)
{
printf("%d ", array[i]);
i++;
}
return sum;
}
// The main calls the the sumEvenOdd procedure twice with different arrays
void main()
{
int arraysize = 8;
int numbers[arraysize];
int i, even, sum1, sum2;
printf("Enter zero to compute a sum of even numbers or non-zero to compute a sum of odd numbers ");
scanf("%d", &even);
sum1 = sumEvenOdd(numbers, arraysize, even);
printf("Enter zero to compute a sum of even numbers or non-zero to compute a sum of odd numbers ");
scanf("%d", &even);
sum2 = sumEvenOdd(numbers, arraysize, even);
if (sum1 == sum2)
{
printf("They have the same sum. ");
}
else if (sum1 > sum2)
{
printf("The first array has a larger sum. ");
}
else
{
printf("The second array has a larger sum. ");
}
return;
}
This is my code
.data
arraysize: .word 8
array: .space 8
message1: .asciiz "Enter zero to compute a sum of even numbers or non-zero to compute a sum of odd numbers "
message2: .asciiz "Specify how many numbers should be stored in the array (at most 8): "
message3: .asciiz "Enter an integer: "
message4: .asciiz "The array content is: "
msg1: .asciiz "They have the same sum. "
msg2: .asciiz "The first array has a larger sum. "
msg3: .asciiz "The second array has a larger sum. "
Exit_msg: .asciiz "I am in EXIT"
Test_msg: .asciiz "I am here "
#program code is contained below under .text
.text
.globl main #define a global function main
# the program begins execution at main()
main:
# $s0 - array base address
# $s1 - length
# $s2 - even
# $s3 - sum2
# $s4 - howmanny
# $t1 - counter readArray
# $t7 - counter array content
# $t8 - has array readArray
add $t1,$zero,$zero
add $t7,$zero,$zero
la $s0,array
#$s1 for arraysize (length)
addi $s1,$zero,8
#preparing the stack
# howMany - 0($sp)
#
#
addi $sp,$sp,-12
# sum
#
#
#
# Printing "Enter zero to compute a sum of even numbers or non-zero to compute a sum of odd numbers "
#
la $a0,message1
li $v0,4
syscall
li $v0,5
syscall
move $s2,$v0 # int even -> $s2
#
# sum1 = sumEvenOdd(numbers, arraysize, even);
#
jal sumEvenOdd
#move $s0,$ ****
# printf("Enter zero to compute a sum of even numbers or non-zero to compute a sum of odd numbers ");
# scanf("%d", &even);
la $a0,message1
li $v0,4
syscall
li $v0,5
syscall
move $s3,$v0 #int even $s3
#
# sum2 = sumEvenOdd(numbers, arraysize, even);
#
jal sumEvenOdd
#
# If statments here
#
#beq $,$,IfSum1EqlSum2
#bgt $,$,ElIfSum1GrtSum2
#blt $,$,Else
# return
jr $ra
IfSum1EqlSum2:
la $a0,msg1
li $v0,4
syscall
jr $rt
ElIfSum1GrtSum2:
la $a0,msg2
li $v0,4
syscall
jr $rt
Else:
la $a0,msg3
li $v0,4
syscall
jr $rt
# Procedure and stuff
readArray:
#move $t1,$zero
beq $t1,$s4,sumEvenOdd_Loop1
beq $t1,$s1,sumEvenOdd_Loop1
#greater than howmany jump
bgt $t1,$s4,sumEvenOdd_Loop1
#greater than length
bgt $t1,$s1,sumEvenOdd_Loop1
# if i < howMany
#blt $t1,$s4,readArray_Loop1
sw $s0,0($s0)
#
# printf("Enter an integer: ");
# scanf("%d", &num);
la $a0,message3
li $v0,4
syscall
li $v0,5
syscall
move $t6,$v0
#trying to add the value obtainted to $t8
add $t8,$zero,$t6
#move in array
addi $s0,$s0,4
#i++
addi $t1,$t1,1
#bgt $t2,$s1,sumEvenOdd
j readArray
############################################################################
# Procedure sumEvenOdd
# Description: -----
# parameters: $a0 = address of array, $a1 = length, $a2 = even or odd
# return value: $v0 = sum
# registers to be used: $s3 and $s4 will be used.
############################################################################
sumEvenOdd:
move $t0,$zero
# printf("Specify how many numbers should be stored in the array (at most 8): ");
# scanf("%d", &howMany);
la $a0,message2
li $v0,4
syscall
li $v0,5
syscall
move $s4,$v0 # $s4
jal readArray
#blt $t0,$s4,sumEvenOdd_Loop1
j sumEvenOdd_Loop1
sumEvenOdd_Loop1:
#blt $t0,$s3,sumEvenOdd_Loop2
#la $a0,Test_msg
#li $v0,4
#syscall
#j Exit
#beq $t0,$s4,sumEvenOdd_Loop2
#beq $t0,$s1,sumEvenOdd_Loop2
#bgt $t0,$s4,sumEvenOdd_Loop2
#bgt $t0,$s1,sumEvenOdd_Loop2
#beq $s2,$zero,sumEvenOdd_EvenEqlZero
#j sumEvenOdd_Loop1
sumEvenOdd_Loop2:
sumEvenOdd_EvenEqlZero:
ArrayContent:
beq $t7,$s4,Exit
beq $t7,$s1,Exit
bgt $t7,$s4,Exit
bgt $t7,$s1,Exit
lw $t9,($s0)
move $a0,$t9
li $v0,4
syscall
#move in array
#addi $s0,$s0,4
#i++
addi $t7,$t7,1
j ArrayContent
Exit:
la $a0,Exit_msg
li $v0,4
syscall
li $v0,10
syscall